pymongo text search / regexp help

1,461 views
Skip to first unread message

kevin

unread,
Nov 4, 2010, 3:25:08 PM11/4/10
to mongod...@googlegroups.com
i want to find all documents with title field containing search terms, and i want search to be insensitive.
how do i do this?

search = 'word1 word2'
connection.db.docs.find({'title':_________})?

thanks a lot!

Alvin Richards

unread,
Nov 4, 2010, 3:31:37 PM11/4/10
to mongodb-user
db.docs.find({ "title" : /word1 word2/i })

You add the /i at the end of the regex to make this case-
insensitive. You do want to search to see if the text contains one
or more of the search terms?

-Alvin

kevin

unread,
Nov 4, 2010, 3:33:36 PM11/4/10
to mongod...@googlegroups.com
i want both word1 and word2, if that is empty either word1 or word2

is this for both word1 and word2  or -- word1 or word2

db.docs.find({ "title" : /word1 word2/i })

thanks a lot!

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Alvin Richards

unread,
Nov 4, 2010, 3:37:53 PM11/4/10
to mongodb-user
This will do a case insensitive search for the string "word1 word2".

There are some full text search approaches listed here, since I think
you want to search on a distinct list of phrases

http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo

-Alvin
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .

kevin

unread,
Nov 4, 2010, 9:28:22 PM11/4/10
to mongod...@googlegroups.com
hi the example you gave is not working. can you tell how to do in pymongo?
i have a document with name kevinC. i want to do a case insensitive search and it is not working.

>>> x.find_one({'name':'/kevinc/i'})
>>> x.find_one({'name':{'$regex':'/kevinc/i'}})
>>> x.find_one({'name':'kevinC'})._id
ObjectId('4cc08693b6ce0c502c16a71c')
>>>





To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

Roger

unread,
Nov 4, 2010, 10:18:35 PM11/4/10
to mongod...@googlegroups.com
Hi Kevin

> -----Ursprüngliche Nachricht-----
> Von: mongod...@googlegroups.com
> [mailto:mongod...@googlegroups.com] Im Auftrag von kevin
> Gesendet: Freitag, 5. November 2010 02:28
> An: mongod...@googlegroups.com
> Betreff: Re: [mongodb-user] Re: pymongo text search / regexp help


>
> hi the example you gave is not working. can you tell how to
> do in pymongo?
> i have a document with name kevinC. i want to do a case
> insensitive search and it is not working.
>
> >>> x.find_one({'name':'/kevinc/i'})
> >>> x.find_one({'name':{'$regex':'/kevinc/i'}})
> >>> x.find_one({'name':'kevinC'})._id
> ObjectId('4cc08693b6ce0c502c16a71c')
> >>>

Some thing like this?

x.find_one({'solrText': re.compile('|'.join(words), re.IGNORECASE)}


Here is what we use in our backend for simple text search:

import re
def getSearchQuery(searchText):
# simply search or OR search for strings with spaces
query = {}
if searchText:
words = searchText.split()
query['text'] = re.compile('|'.join(words), re.IGNORECASE)
return query


But I do not recommend to use this as a search concept
if you expect heavy use of it. I guess this is not very fast.

Hope that helps

Regards
Roger Ineichen

> <mailto:mongodb-user%2Bunsu...@googlegroups.com>

> <mongodb-user%2Bunsubscribe@google groups.com>
>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/mongodb-user?hl=en.
>
>
> --
>
> You received this message because you are subscribed to
> the Google Groups "mongodb-user" group.
> To post to this group, send email to
> mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com

> <mailto:mongodb-user%2Bunsu...@googlegroups.com> .

kevin

unread,
Nov 4, 2010, 10:39:06 PM11/4/10
to mongod...@googlegroups.com
Hi Roger!
Thanks a lot for this! this works great!!!
Reply all
Reply to author
Forward
0 new messages