MongoDB 索引文本搜索仅适用于完全匹配

问题描述

我在字段“user_name”中填充了数据。 这段代码没有给我任何结果:

history = db.history
history.create_index([('user_name','text')])
history.find({'$text' : {'$search' : 'a'}})

但是当我指定确切的名称时,它会起作用

history.find({'$text' : {'$search' : 'exact name'}})

这是针对“a”搜索的解释()的输出

{
    "executionSuccess": true,"nReturned": 0,"executionTimeMillis": 0,"totalKeysexamined": 0,"totalDocsexamined": 0,"executionStages": {
        "stage": "TEXT","executionTimeMillisEstimate": 0,"works": 1,"advanced": 0,"needTime": 0,"needYield": 0,"saveState": 0,"restoreState": 0,"iSEOF": 1,"indexPrefix": {},"indexName": "user_name_text","parsedTextQuery": { "terms": [],"negatedTerms": [],"phrases": [],"negatedPhrases": [] },"textIndexVersion": 3,"inputStage": {
            "stage": "TEXT_MATCH","works": 0,"docsRejected": 0,"inputStage": {
                "stage": "FETCH","docsexamined": 0,"alreadyHasObj": 0,"inputStage": { "stage": "OR","dupsTested": 0,"dupsDropped": 0 }
            }
        }
    },"allPlansExecution": []
}

这里是用户名('akkcess')完全匹配的explain()的输出

{
    "executionSuccess": true,"nReturned": 39,"executionTimeMillis": 1,"totalKeysexamined": 39,"totalDocsexamined": 39,"works": 40,"advanced": 39,"parsedTextQuery": { "terms": ["akkcess"],"docsexamined": 39,"inputStage": {
                    "stage": "OR","dupsTested": 39,"dupsDropped": 0,"inputStage": {
                        "stage": "IXSCAN","keyPattern": { "_fts": "text","_ftsx": 1 },"ismultikey": false,"isUnique": false,"issparse": false,"isPartial": false,"indexVersion": 2,"direction": "backward","indexBounds": {},"keysexamined": 39,"seeks": 1,"dupsDropped": 0
                    }
                }
            }
        }
    },"allPlansExecution": []
}

你知道它为什么会这样吗? 根据文档和教程,这应该可以工作。

解决方法

"a" 几乎肯定是一个 stop word。几乎每个自然语言文本都会包含它。因此,如果搜索它,您将获得结果集中的每个文档。由于这不是很有用,因此文本搜索会从查询中删除诸如“a”之类的停用词。

另外,MongoDB 文本搜索确实包括精确匹配功能,但它需要引用您尚未完成的查询,因此您使用的是常规词干匹配,而不是您发布的精确匹配查询。