MongoDB文本跨字段搜索

时间:2019-01-04 04:14:29

标签: mongodb search text

正在使用

db.version()
4.0.0

我有以下收藏,

db.items.find( {}, {name:1, description: 1, _id: 0} );

{ "name" : "test", "description" : "this is a test" }
{ "name" : "foo", "description" : "this is another test" }
{ "name" : "test2", "description" : "this is something" }
{ "name" : "bar", "description" : "this is something" }

我确保2个字段上都有一个文本索引,

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

输出为

{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 2,
    "note" : "all indexes already exist",
    "ok" : 1
}

运行此

db.items.find({$text: {$search: "something" }}, {_id:0});

显示

{ "name" : "bar", "description" : "this is something" }
{ "name" : "test2", "description" : "this is something" }

运行此

db.items.find({$text: {$search: "bar" }}, {_id:0});

显示

{ "name" : "bar", "description" : "this is something" }

但是,运行它,

db.items.find({$text: {$search: "is" }}, {_id:0});

什么也不显示。为什么会这样?

重新索引没有影响,

db.items.reindex()

1 个答案:

答案 0 :(得分:0)

“ is”一词被忽略,因为它是英语中的“停用词”。

  

MongoDB支持多种语言的文本搜索。文字索引下降   特定于语言的停用词(例如英语,the,an,a和   等),并使用简单的特定于语言的后缀。

https://docs.mongodb.com/manual/core/index-text/#supported-languages-and-stop-words

请注意,与文本索引关联的默认语言是English

指定default_language中的none将使用带有no list of stop words and no stemming.的简单标记化

例如:

> db.items.ensureIndex({ "name" : "text", "description" : "text" }, { "default_language" : "none" } )
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.items.find({$text: {$search: "is" }}, {_id:0});
{ "name" : "bar", "description" : "this is something" }
{ "name" : "test2", "description" : "this is something" }
{ "name" : "foo", "description" : "this is another test" }
{ "name" : "foo", "description" : "this is another test" }
{ "name" : "test", "description" : "this is a test" }