Mongodb Shortest Introduction (2) I searched for tens of thousands

Create a search API for MongoDB + python

Mongodb shortest introduction (1) Install & start on EC2 & suddenly put tens of thousands of items Continued

Search with MongoDB + python

Even though there were tens of thousands, I was able to search by partial match in Japanese. In (1), I tried to put data of about 1G of text amount with t2.micro of EC2. Pretty light

Pass a python regular expression as an object

--On the command line, use jsavascript regular expressions. --If you pass it as an object with a python regular expression, you can use partial match and regular expression --Since it is included in bson instead of json, use the utility of bson.

python search.py

search.py


# coding: utf-8
import pymongo
from bson.json_util import loads
from bson.json_util import dumps
import re

client=pymongo.MongoClient()
#database
db=client.test

regx = re.compile("pink", re.IGNORECASE)
cursor = db.honyarara.find({"title":regx})

for document in cursor:
    print dumps(document)

Let's make it an API server.

I will search the data put in MongoDB and make it a CGI that returns the result with json

Start python CGI ** Tturn! **

python -m CGIHTTPServer &

See here ↓ CGI server (1) python in one line

Access the URL and check if JSON appears

curl http://127.0.0.1:8000/cgi-bin/search.py?q=test

This time, the word passed by q = <> is ** OR searched ** in two fields called title or kana.

Assuming that there is such data → {"title ":" test mongo! "," Kana ":" "}, {}, {} ,,

search.py


#!/usr/bin/python
# coding: utf-8

import pymongo
from bson.json_util import dumps
import re
import json
import cgi

print "Content-type: text/html\n\n"

client=pymongo.MongoClient()
#database test
db=client.test
#Get query
form = cgi.FieldStorage()
#If there is a request variable called q
if(form.has_key("q")):
    q = form["q"].value
    regx = re.compile(q, re.IGNORECASE)
#Set the regular expression in the field and get it with mongo's find method
    cursor = db.honyarara.find({
    "$or": [
    {"title":regx},
    {"kana":regx}
    ]
    })

else:
    print "{}"
    exit()

#Put it in the list, bson → json character string → convert json to variable
items=[]
for document in cursor:
    items.append(json.loads(dumps(document)))

#Convert from array to json
print json.dumps({"items": items }) 

MongoDB initialization

Don't imitate a good boy, but when you want to start over

service mongod stop

--Check the data directory in /etc/mongo.conf`` / var / lib / mongo --Roughly erase the contents. Or recreate the directory with mongod user privileges --Or point to another location in conf --So, reboot

service mongod start

It seems that the paths etc. are the same according to the method of MySQL. With my old MyISAM, it's okay to roughly delete or move files, and I miss it. ..

Recommended Posts

Mongodb Shortest Introduction (2) I searched for tens of thousands
Mongodb Shortest Introduction (1) Installed & started on EC2 & suddenly put tens of thousands
I searched for the contents of CloudWatch Logs Agent
I searched for CD commands.
Mongodb Shortest Introduction (3) I tried to speed up even millions
Record of Python introduction for newcomers
I searched for prime numbers in python
[Python] I searched for various types! (Typing)
I searched for railway senryu from the data
[Python] I searched for the longest Pokemon Shiritori