Python notes--MongoDB fuzzy query

Source: Internet
Author: User
Tags bitwise operators

Recently, when using MongoDB, I encountered a scene with multiple keywords for fuzzy query. The bamboo wind uses the Mongoengine library.

Looked up all kinds of information, and finally summed up the more useful methods. First, the code, followed by detailed instructions. As follows:

1 #!/usr/bin/env python2 #Coding:utf-83 4 ImportRe5 ImportMongoengine6  fromMongoengineImport*7 8Mongoengine.register_connection ('default','Test', host='127.0.0.1')9 Ten classTestData (Document): OneName =Stringfield () AContent =Stringfield () -  -TestData.objects.get_or_create (name='World Xuan Huang', defaults={'content':'abc123'}) theTestData.objects.get_or_create (name='Cosmic Primitive', defaults={'content':'ABC123'}) -TestData.objects.get_or_create (name=' Day up', defaults={'content':'Abc123'}) -  - defPrint_arr (obj): +     Printobj.name,obj.content -  + deffuzzy_query_by_contains (): A     Print "\n## #使用mongoengine的contains进行查询" at     Print "#contains区分大小写:" -Test_data_list = Testdata.objects (content__contains='abc123') - map (print_arr,test_data_list) -  -     Print "#icontains不区分大小写:" -Test_data_list = Testdata.objects (content__icontains='abc123') in map (print_arr,test_data_list) -  to deffuzzy_query_by_q (): +     Print "\n## #使用Q来进行查询" -Test_data_list =Testdata.objects ( theQ (name__icontains=u'World') | Q (name__icontains=u'Universe')) * map (print_arr,test_data_list) $ Panax Notoginseng defFuzzy_query_by_pymongo (): -     Print "\n## #使用raw queries,new in version 0.4" the     Print "#单个查询条件" +Search = { A         '__raw__':{ the             'content':{'$regex':'a\s+\d+'}, +         }, -     } $Test_data_list = testdata.objects (* *search) $ map (print_arr,test_data_list) -  -     Print "#多个查询条件" theSearch = { -         '__raw__':{Wuyi             'name':{'$in': [Re.compile (U'every day'), Re.compile (U'Universe')]}, the         }, -     } WuTest_data_list = testdata.objects (* *search) - map (print_arr,test_data_list) About  $ if __name__=='__main__': - fuzzy_query_by_contains () - fuzzy_query_by_q () -Fuzzy_query_by_pymongo ()

Let's discuss the Fuzzy_query_by_contains method, which uses the contains operation provided by Mongoengine. It is important to note that contains is case-sensitive and icontains is not case-sensitive. This is especially handy when searching for a keyword in a fuzzy way.

Then there is the Fuzzy_query_by_q method, which combines contains and q to make a combination query. When you use Q () to combine queries, you must use the bitwise operators (| and &) instead of using Or,and for logical operations. This approach is more appropriate to determine the number of keywords. If the number of keywords is uncertain, this approach is slightly tangled.

Bamboo Wind in the dynamic keyword Fuzzy query problem is also tangled for a long, almost to each keyword separately query, and then take the intersection of results. Later found in the documentation, Mongoengine has __raw__ this parameter, you can perform Pymongo queries (new features provided in version 0.4). So after several experiments, the Fuzzy_query_by_pymongo method was baked.
Pymongo supports regular expressions, providing two methods, one using $regex and the other using Re.compile ().
In the example, a fuzzy query for a single keyword, the corresponding code is: {' $regex ': ' a\s+\d+ '}
Then is the query for multiple keywords, the corresponding code is: {' $in ': [Re.compile (U ' daily '), Re.compile (U ' Universe ')]}

Make some changes to the code to accept multiple keywords, the code is as follows:

1 defFuzzy_query_by_pymongo ():2     Print "#多个查询条件"3Keyword = u'Everyday Universe'4Search = {'__raw__': {'name':{'$in': Map (Re.compile,keyword.split ()) }}}5Test_data_list = testdata.objects (* *search)6Map (print_arr,test_data_list)

Incidentally, the example in which the data is created is the get_or_create, which returns a tuple, the first element is the object that creates or queries, and the second element is whether the creation succeeds. The recommended usage in the documentation is as follows:

1>>> A, created = User.objects.get_or_create (name='User A', defaults={' Age': -})2>>> B, created = User.objects.get_or_create (name='User A', defaults={' Age': +})3>>> A.name = = B.name and A.age = =B.age4True

Finally, the result of the example run, the order of the returned results may be slightly different, do not care.

1 $ python mongodb_test.py2 3 # # #使用mongoengine的contains进行查询4 #contains区分大小写:5 World Xuan Huang abc1236 #icontains不区分大小写:7 World Xuan Huang abc1238 Cosmic Primitive ABC1239 Day Abc123Ten  One # # #使用Q来进行查询 A World Xuan Huang abc123 - Cosmic Primitive ABC123 -  the# # #使用raw Queries,newinchVersion0.4 - #单个查询条件 - Cosmic Primitive ABC123 - Day Abc123 + #多个查询条件 - Cosmic Primitive ABC123 +Day Abc123

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.