mongodb text search with multiple fields

前端 未结 2 747
抹茶落季
抹茶落季 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
    }
    

提交回复
热议问题