ear*_*Lon 6 full-text-search mongodb pymongo
当元素数组中的数组包含应与我的搜索匹配的文本时,我无法检索文档.
这是两个示例文档:
{
_id: ...,
'foo': [
{
'name': 'Thing1',
'data': {
'text': ['X', 'X']
}
},{
'name': 'Thing2',
'data': {
'text': ['X', 'Y']
}
}
]
}
{
_id: ...,
'foo': [
{
'name': 'Thing3',
'data': {
'text': ['X', 'X']
}
},{
'name': 'Thing4',
'data': {
'text': ['X', 'Y']
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
通过使用以下查询,我可以返回两个文档:
db.collection.find({'foo.data.text': {'$in': ['Y']}}
但是,我无法使用全文命令/索引返回这些结果:
db.collection.runCommand("text", {search" "Y"})
我确信全文搜索工作正常,因为对"Thing1"发出搜索的同一命令将返回第一个文档,"Thing3"返回第二个文档.
我确信foo.data.text和foo.name在使用时都在文本索引中db.collection.getIndexes().
我使用以下方法创建了索引:db.collection.ensureIndex({'foo.name': 'text', 'foo.data.text': 'text'}).以下是上述命令所示的索引:
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"ns" : "testing.collection",
"background" : true,
"name" : "my_text_search",
"weights" : {
"foo.data.text" : 1,
"foo.name" : 1,
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 1
}
Run Code Online (Sandbox Code Playgroud)
关于如何使用mongo的全文搜索的任何建议?
小智 8
文本搜索当前不支持嵌套数组的索引字段(至少没有明确指定的字段)."foo.name"的索引工作正常,因为它只有一个数组深,但文本搜索不会通过"foo.data.text"的子数组递归.请注意,此行为可能会在2.6版本中更改.
但是不要害怕,与此同时,嵌套数组可以进行文本索引,而不是单独指定的字段.您可以使用通配符说明符$**来递归索引集合中的所有字符串字段,即
db.collection.ensureIndex({"$**": "text" }
Run Code Online (Sandbox Code Playgroud)
如http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/中所述.但要小心,因为这将索引每个字符串字段,并可能产生负面的存储和性能影响.您描述的简单文档结构应该可以正常工作.希望有所帮助.
| 归档时间: |
|
| 查看次数: |
5843 次 |
| 最近记录: |