用AND运算符进行猫鼬文本搜索

用AND运算符进行猫鼬文本搜索

本文介绍了用AND运算符进行猫鼬文本搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想做的是用猫鼬对mongoDb进行"AND"搜索,但是结果来自"OR"搜索.有这个设置吗?这是查询的样子:

So what I'm trying to do is making a "AND" search with mongoose to mongoDb, but the results is from a "OR" search. Is there a setting for this? This is how query looks like:

exampleModel.find({
    $text: {
        $search: searchParams
    }
}, {
    score: {
        $meta: "textScore"
    }
}, { lean: true }).select(exampleViewModel).limit(1000).sort({
    score: {
        $meta: 'textScore'
    }
}).exec(function (err, results) {
   next(null, results);
});

让我们假设搜索帕拉姆斯=伦敦餐厅;

Let's asume searchParams = restaurant london;

这将在带有"restaurant"或"london"字样的文档中产生结果. 我希望它在包含"restaurant"和"london"字样的文档中产生结果.

This will yield results within documents with the words "restaurant" OR "london". I want it to yield results within documents with the words "restaurant" AND "london".

推荐答案

在搜索词前后加上引号,以将默认行为更改为AND.

Put quotation marks around the search terms to change the default behaviour to an AND.

https://docs.mongodb.org/manual/reference /operator/query/text/#phrases

exampleModel.find({
    $text: {
        $search: "\"restaurant\" \"london\""
    }
}, {
    score: {
        $meta: "textScore"
    }
}, { lean: true }).select(exampleViewModel).limit(1000).sort({
    score: {
        $meta: 'textScore'
    }
}).exec(function (err, results) {
   next(null, results);
});

这篇关于用AND运算符进行猫鼬文本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:07