文字搜索不返回某些单词的任何内容 [英] Text search doesn't return anything for certain words
问题描述
我有一个数据库,其中包含条目"集合,其结构如下:
I have a database with an "entries" collection that is structured like so:
{
tags: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Tag"
}
],
body: String
}
我想要在我的网站上使用搜索功能,因此我在"body"属性上创建了一个文本索引,如下所示:
I wanted a search feature on my site, so I created a text index on the "body" property like this:
db.entries.createIndex( { body: "text" } )
效果很好.我创建了几个条目并运行db.entries.find()
.我知道了:
It worked fine. I created a couple of entries and ran db.entries.find()
. I got this:
{ "_id" : ObjectId("5c2e3d9fd1d5dd121ed85695"), "tags" : [ ], "body" : "some text", "__v" : 0 }
{ "_id" : ObjectId("5c2e3dadd1d5dd121ed85696"), "tags" : [ ], "body" : "text some", "__v" : 0 }
到目前为止,太好了.我搜索db.entries.find({$text: { $search: "text" }})
,并且两个条目都被返回.但是,当我搜索db.entries.find({$text: { $search: "some" }})
时,我什么也没得到.
So far, so good. I search db.entries.find({$text: { $search: "text" }})
and both of the entries are returned. But when I search db.entries.find({$text: { $search: "some" }})
I get nothing.
我用不同的单词再次尝试了. 教堂"工作正常,但更多"也不返回任何内容.为什么?
I tried this again with different words. "church" works fine, but "more" doesn't return anything either. Why???
推荐答案
MongoDB文本索引会删除一些特定于语言的单词,这些单词被视为建立索引-这样可以防止出现任何停用词,并且可以对整个内容建立索引:
MongoDB text index drops some language-specific words that are considered as Stopwords and some
is one of them. To fix that you can build your index with default_language: "none"
- this prevents any stopwords and whole content is getting indexed:
db.entries.createIndex(
{ body : "text" },
{ default_language: "none" }
)
完整的英语停用词列表此处.
Full list of English stopwords here.
这篇关于文字搜索不返回某些单词的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!