mongodb text search with multiple fields

前端 未结 2 744
抹茶落季
抹茶落季 2020-12-05 19:37

I\'m trying to get mongodb fulltext search with multiple fields working. I\'ve set the index on 3 fields-name,description, category, and verified with

documen

相关标签:
2条回答
  • 2020-12-05 20:09

    You should create a text index on the fields you want to search:

    db.deals.ensureIndex({ name: "text", description : "text", category : "text" });
    

    From the documentation of the $text operator:

    $text performs a text search on the content of the fields indexed with a text index.

    The index you created for your three fields is a compound index, not a text index. The text index will look like this:

    {
        "v" : 1,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "name_text_description_text_category_text",
        "ns" : "test.deals",
        "weights" : {
            "category" : 1,
            "description" : 1,
            "name" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 2
    }
    
    0 讨论(0)
  • 2020-12-05 20:09

    using the example above

    db.deals.ensureIndex({ name: "text", description : "text", category : "text" });
    

    Does this mean that I am creating one index or 3, and also can I search just the name or description only, or do I have to always list all 3?

    Also, what if I wanted to add another index for phone number?

    0 讨论(0)
提交回复
热议问题