4

I would like to create a text index in my MongoDB which allows users to search a field that is an array of string elements.

For example, my document looks like

{
    "stringfield": "hello",   
    "arrayfield": ["one", "two", "three"]
}

and I want a user who searches for "two" to find this document. Users do not have direct access to the DB. Currently, pymongo executes their query via

our_mongo.find({'$text':{'$search': user_input }}, {'score':{'$meta':"textScore"}})

According to the mongo documentation,

"text indexes can include any field whose value is a string or an array of string elements."

While string values are successfully searched, I could not get my text index to deal with arrays of string elements. My index was created with this command:

db.mydocs.createIndex(
    {
        '_id': 'text',
        'stringfield': 'text', // works
        'arrayfield': 'text', // does NOT work --> EDIT: DOES work
    },
    {
        weights: {
            'stringfield': 10,
            'arrayfield': 5
        },
        name: 'search'
    }
);
mareoraft
  • 3,474
  • 4
  • 26
  • 62

0 Answers0