Finding text in Mongoose with the AND operator

So I'm trying to do an AND search using mongoose to mongoDb, but the results are taken from the OR search. Is there any setting for this? Here's what the 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); 
});

Suppose Params = restaurant london;

This will produce results in documents with the words “restaurant” OR “London”. I want him to give results in documents with the words "restaurant" and "London".

+1
source share
2 answers

Place quotation marks around search terms to change the default behavior to 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); 
});
+1

, , :

apple banana       =>      "apple" "banana"

AND , Elasticsearch.

, , , (-exclude) ("several words wrapped in double quotes"), $text.

, , , , .

:

apple banana "huge orange trees" -pineapple "lemon"

:

"apple" "banana" "huge orange trees" -pineapple "lemon"

. $search MongoDB find().

function wrapSingleTermsWithDoubleQuotes(query) {
    // Output variable
    var output = "";

    // Keep track of whether to ignore the current word due to the negation operator (-minus)
    var ignoreCurrentWord = false;

    // Keep track of whether we're inside a custom phrase in the query ("exact match")
    var withinCustomPhrase = false;

    // Keep track of whether we need to close a double quote that we opened for the current word
    var openedDoubleQuote = false;

    // Remove all double spacing from the query (we may need a few iterations for this to work)
    while (query.indexOf('  ') != -1) {
        // Replace all occurrences of double spacing with single spacing
        query = query.replace(/  /g, ' ');
    }

    // Trim leading and trailing spaces from the query to normalize the input
    query = query.trim();

    // Start traversing query characters
    for (var i = 0; i < query.length; i++) {
        // Comfort variable
        var char = query[i];

        // Not yet wrapping a term with double quotes?
        if (!openedDoubleQuote) {
            // Not ignoring the current word already (due to an operator) and not within a custom phrase?
            if (!ignoreCurrentWord && !withinCustomPhrase) {
                // Char is not a quote or negation operator?
                if (char != '"' && char != '-') {
                    // This is most likely a single term, let insert an opening double quote
                    output += '"';

                    // Mark this as true so we remember to close the double-quote when the word done
                    openedDoubleQuote = true;
                }
                else {
                    // Char is a quote
                    if (char == '"') {
                        // Avoid adding quotes until we encounter the phrase closing quote
                        withinCustomPhrase = !withinCustomPhrase;
                    }
                    // Char is a negation operator
                    else if (char == '-') {                    
                        // Ignore the current word (don't try to wrap it with double quotes)
                        ignoreCurrentWord = true;
                    }
                }
            }
            else {
                // Ignoring the current word or phrase -- check if we reached the end of the current word (space)
                if (char == ' ') {
                    // In case this was a negative word, it over now
                    ignoreCurrentWord = false;

                    // Were we inside a custom phrase, the current char is a space, and the previous char was a double quote?
                    if (withinCustomPhrase && i > 0 && query[i - 1] == '"') {
                        // No longer inside a the custom phrase (since we encountered a closing double-quote)
                        withinCustomPhrase = false;
                    }
                }
            }
        }
        else {
            // Within a single term currently -- is current char a space, indicating the end of the single term?
            if (char == ' ') {
                // Add a closing double quote to finish wrapping the single term
                output += '"';

                // We closed our own double-quote
                openedDoubleQuote = false;
            }
        }

        // Add current char to output (we never omit any query chars, only append double-quotes where necessary)
        output += char;
    }

    // Reached the end of the string but still got a quote to close?
    if (openedDoubleQuote) {
        // Add the final double quote
        output += '"';
    }

    // Return algorithm output
    return output;
}
+1

Source: https://habr.com/ru/post/1542861/


All Articles