如何在MongoDB $ text搜索中进行“与非"操作 [英] How to AND and NOT in MongoDB $text search

查看:77
本文介绍了如何在MongoDB $ text搜索中进行“与非"操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人提出这个问题,我事先表示歉意,我没有找到答案就进行搜索.

I apologise in advance if this question has been asked, I searched without finding an answer.

我想在MongoDB中进行搜索,然后创建索引并执行

I want to do a search in MongoDB, and I create the index and do something like

db.myCollection.runCommand( "text", { search: "myWord" } )

这很好.

我也可以

db.myCollection.runCommand( "text", { search: "myWord1 myWord2" } )

,它将同时搜索两个单词.

and it will search both words.

我可以通过要求找到两个单词来进行上述搜索吗? (我知道我可以对第一个单词进行搜索,然后对结果进行第二个搜索,但是我想知道是否有更好的方法.)

Can I do the above search by asking that BOTH words be found? (I know I can do a search on the first word and then a search on the second on the result, but I wonder if there is a better way).

还可以指定要拒绝的单词(例如,搜索单词"test",而不是"testing").

Also, is it possible to specify words to be rejected (for example, search for the word "test", but not for "testing").

我想知道是否可以在不使用外部工具的情况下在mongoDB中做到这一点.

I would like to know if I can do this in mongoDB without using external tools.

推荐答案

您可以在文本搜索中使用一些选项来满足这些需求.请考虑以下文件:

There are a few options that you can use with text search to suit those needs. Consider the following documents:

{ "text" : "cake" }
{ "text" : "sale" }
{ "text" : "sale cake" }
{ "text" : "cake sale" }
{ "text" : "dress sale" }
{ "text" : "cake sale monday" }

默认的单词列表"是包含,但是如果您不想使用包含,则可以引用"这些单词:

The default "list" of words is an or inclusion, but if you wan't an and inclusion you "quote" the words:

db.words.find( { "$text": { "$search": "\"cake\" \"sale\"" } },{_id: 0})
{ "text" : "cake sale" }
{ "text" : "sale cake" }
{ "text" : "cake sale monday" }

如果您要排除一个单词,请以-作为前缀:

If you want to exclude a word then you prefix with -:

db.words.find( { "$text": { "$search": "\"cake\" \"sale\" -monday" } },{_id: 0})
{ "text" : "cake sale" }
{ "text" : "sale cake" }

如果您希望将其中一部分作为精确短语,则可以引用"整个短语:

And if you wanted part of that as an exact phrase then you "quote" the whole phrase:

db.words.find( { "$text": { "$search": "\"cake sale\" -monday" } },{_id: 0})
{ "text" : "cake sale" }

使用词干确实有问题,所以:

Stemming words though does have an issue, so :

db.words.find( { "$text": { "$search": "test -testing" } },{_id: 0})

实际上不会返回结果.

请参阅 $text 上的文档运算符.目前还没有全部,但是越来越好.

See the documentation on the $text operator for examples. Not all are there at the moment, but it's getting better.

这篇关于如何在MongoDB $ text搜索中进行“与非"操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆