Navigation Menu

Skip to content

Commit

Permalink
bq: support query expansion in phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 14, 2012
1 parent 1b79691 commit 71ff98e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/bq-translator.js
Expand Up @@ -296,7 +296,15 @@ BooleanQueryTranslator.prototype = {
value += character;
} else if (character == "\"") {
this.offset++;
return this.constructBinaryOperation(field, "@", "\"" + value + "\"");
var expandedPhrases = this.expandWord(value);
var expressions = expandedPhrases.map(function(phrase) {
return this.constructBinaryOperation(field, "@", "\"" + phrase + "\"");
}, this);
if (expressions.length >= 2) {
return "(" + expressions.join(" || ") + ")";
} else {
return expressions[0];
}
} else {
value += character;
}
Expand Down
25 changes: 21 additions & 4 deletions test/bq-translator.test.js
Expand Up @@ -350,20 +350,37 @@ suite('BoolanQueryTranslator', function() {
"'ModelName'||",
"no default field");

testSynonym("existent: 0 synonym",
testSynonym("keyword: existent: 0 synonym",
"'tokio'",
{ tokio: [] },
'');
testSynonym("existent: 1 synonym",
testSynonym("keyword: existent: 1 synonym",
"'tokio'",
{ tokio: ["tokyo"] },
'field @ "tokyo"');
testSynonym("existent: N synonyms",
testSynonym("keyword: existent: N synonyms",
"'tokio'",
{ tokio: ["tokio", "tokyo"] },
'(field @ "tokio" || field @ "tokyo")');
testSynonym("nonexistent",
testSynonym("keyword: nonexistent",
"'hokkaido'",
{ tokio: ["tokio", "tokyo"] },
'field @ "hokkaido"');

testSynonym("phrase: existent: 0 synonym",
"'\"tokyo disney land\"'",
{ "tokyo disney land": [] },
'');
testSynonym("phrase: existent: 1 synonym",
"'\"tokyo disney land\"'",
{ "tokyo disney land": ["TDL"] },
'field @ "TDL"');
testSynonym("phrase: existent: N synonyms",
"'\"tokyo disney land\"'",
{ "tokyo disney land": ["tokyo disney land", "TDL"] },
'(field @ "tokyo disney land" || field @ "TDL")');
testSynonym("phrase: nonexistent",
"'\"tokyo disney land\"'",
{ tokio: ["tokio", "tokyo"] },
'field @ "tokyo disney land"');
});

0 comments on commit 71ff98e

Please sign in to comment.