I want to enable text-search at a specific field in my Mongo DB. I want to implement this search in python (-> pymongo). When I follow the instructions given in the internet:
db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index")
I get the following error message:
Traceback (most recent call last): File "CVE_search.py", line 8, in <module> db.foo.ensure_index(('field_i_want_to_index', 'text'), name="search_index") File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1599, in ensure_index return self.create_index(key_or_list, cache_for, **kwargs) File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1466, in create_index index_doc = helpers._index_document(keys) File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 100, in _index_document for (key, value) in index_list: ValueError: too many values to unpack
Is there a different/better way to create an index in pymongo?
Create an index for a MongoDB collection, and specify the order. When you use PyMongo to create an index, you can pair the index name with either a 1 or a -1 integer value to explicitly set the sort order to ascending or descending, respectively.
You can create multiple indexes on the same key(s) with different collations.
MongoDB can use the intersection of multiple indexes to fulfill queries. In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.
Use the create_index
method where you pass in the keys as an array and TEXT
as the index direction :
collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With