, , :
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) {
var output = "";
var ignoreCurrentWord = false;
var withinCustomPhrase = false;
var openedDoubleQuote = false;
while (query.indexOf(' ') != -1) {
query = query.replace(/ /g, ' ');
}
query = query.trim();
for (var i = 0; i < query.length; i++) {
var char = query[i];
if (!openedDoubleQuote) {
if (!ignoreCurrentWord && !withinCustomPhrase) {
if (char != '"' && char != '-') {
output += '"';
openedDoubleQuote = true;
}
else {
if (char == '"') {
withinCustomPhrase = !withinCustomPhrase;
}
else if (char == '-') {
ignoreCurrentWord = true;
}
}
}
else {
if (char == ' ') {
ignoreCurrentWord = false;
if (withinCustomPhrase && i > 0 && query[i - 1] == '"') {
withinCustomPhrase = false;
}
}
}
}
else {
if (char == ' ') {
output += '"';
openedDoubleQuote = false;
}
}
output += char;
}
if (openedDoubleQuote) {
output += '"';
}
return output;
}