MongoDB 文本搜索和多个搜索词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16902674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB Text Search AND multiple search words
提问by kmehta
I have an index on an array "keys" that I am using to provide full text functionality to my applicaiton.
我在数组“键”上有一个索引,我用它来为我的应用程序提供全文功能。
With the release of 2.4.3, I'd like to utilize the "text" index type. I insured a "text" index type on my array "keys" and it seems to work SUPER fast (faster than my old keywords full text method).
随着 2.4.3 的发布,我想使用“text”索引类型。我在我的数组“键”上投保了“文本”索引类型,它似乎工作得非常快(比我旧的关键字全文方法快)。
The problem is, my app assumes that fields are inclusive (AND). By default, the text search ORs my parameters.
问题是,我的应用程序假定字段是包含的(AND)。默认情况下,文本搜索或我的参数。
Does anyone know of a way to run a text search inclusively?
有谁知道一种全面运行文本搜索的方法?
For example:
例如:
db.supplies.runCommand("text", {search:"printer ink"})
should return results with both printer and ink, instead of all results with either printer or ink.
应该同时返回打印机和墨水的结果,而不是打印机或墨水的所有结果。
回答by alecxe
Give a try to:
试一试:
db.supplies.runCommand("text", {search:"\"printer\" \"ink\""})
Also, here's a quote from docs:
另外,这里引用了docs:
If the search string includes phrases, the search performs an AND with any other terms in the search string; e.g. search for "\"twinkle twinkle\" little star" searches for "twinkle twinkle" and ("little" or "star").
如果搜索字符串包含短语,搜索将与搜索字符串中的任何其他术语执行 AND;例如,搜索“\”twinkle twinkle\” little star” 搜索“twinkle twinkle”和(“little”或“star”)。
Hope that helps.
希望有帮助。
回答by chovy
You can wrap each word in double-quotes:
您可以将每个单词用双引号括起来:
let keywords = ctx.params.query.split(/\s+/).map(kw => `"${kw}"`).join(' ');
match.$text = { $search: keywords, $caseSensitive: false };
There is a downside if the user inputs a quoted string this will not work. You'd have to parse out quoted strings first.
如果用户输入带引号的字符串,则有一个缺点,这将不起作用。您必须首先解析带引号的字符串。
回答by Abhay Verma
As @alecxe pointed out earlier, to do AND search on text index column you need to double quote each search word. Below is a quick one-liner for your requirement.
正如@alecxe 之前指出的那样,要对文本索引列进行 AND 搜索,您需要对每个搜索词加双引号。以下是满足您要求的快速单线。
db.supplies.runCommand("text", {search: "printer ink".split(" ").map(str => "\""+str+"\"").join(' ')})
回答by Black Mamba
Here is a simple function I made to search using subwords in node. Hope it helps someone
这是我使用 node.js 中的子词进行搜索的一个简单函数。希望它可以帮助某人
Let's suppose a user search for pri nks
so it should satisfy printer and inks but $text search doesn't allow for this so here is my simple function:
让我们假设用户搜索pri nks
所以它应该满足打印机和墨水,但 $text 搜索不允许这样做,所以这是我的简单功能:
var makeTextFilter = (text) => {
var wordSplited = text.split(/\s+/);
/** Regex generation for words */
var regToMatch = new RegExp(wordSplited.join("|"), 'gi');
let filter = [];
searchFieldArray.map((item,i) => {
filter.push({});
filter[i][item] = {
$regex: regToMatch,
$options: 'i'
}
})
return filter;
}
and use it in your query like this
并像这样在您的查询中使用它
let query = {...query, $or: makeTextFilter(textInputFromUser)}
tableName.find(query, function (err, cargo_list)