簡體   English   中英

具有多個字段的mongodb文本搜索

[英]mongodb text search with multiple fields

我正在嘗試使用多個字段進行 mongodb 全文搜索。 我在 3 個字段(名稱、描述、類別)上設置了索引,並使用

document.collection.getIndexes () ,返回 -

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "document.collection"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 2,
            "description" : 1,
            "category" : 1
        },
        "name" : "name_2_description_1_category_1",
        "ns" : "document.collection",
        "background" : true,
        "safe" : null
    }
]

現在,如果我嘗試使用以下命令執行文本搜索 -

db.collection.find(  {$text:{$search:'alias'}}  ).limit(10)

收到以下錯誤消息:

error: {
    "$err" : "Unable to execute query: error processing query: ns=document.collection limit=10 skip=0\nTree: TEXT : query=alias, language=, tag=NULL\nSort: {}\nProj: {}\n planner returned error: need exactly one text index for $text query",
    "code" : 17007
}

我試過谷歌和 mongodb 文檔,但我找不到任何東西。

您應該在要搜索的字段上創建文本索引

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

$text運算符的文檔中:

$text 對使用文本索引索引的字段的內容執行文本搜索。

您為三個字段創建的索引是復合索引,而不是文本索引。 文本索引將如下所示:

{
    "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
}

我正在嘗試使用多個字段進行 mongodb 全文搜索。 我已經在 3 個字段名稱、描述、類別上設置了索引,並驗證了

document.collection.getIndexes () ,它返回-

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "document.collection"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 2,
            "description" : 1,
            "category" : 1
        },
        "name" : "name_2_description_1_category_1",
        "ns" : "document.collection",
        "background" : true,
        "safe" : null
    }
]

現在,如果我嘗試執行文本搜索,請使用以下命令 -

db.collection.find(  {$text:{$search:'alias'}}  ).limit(10)

收到以下錯誤消息:

error: {
    "$err" : "Unable to execute query: error processing query: ns=document.collection limit=10 skip=0\nTree: TEXT : query=alias, language=, tag=NULL\nSort: {}\nProj: {}\n planner returned error: need exactly one text index for $text query",
    "code" : 17007
}

我嘗試了 google 和 mongodb 文檔,但找不到任何東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM