Navigation Menu

Skip to content

Commit

Permalink
bq: support (not ...)
Browse files Browse the repository at this point in the history
Note that it is heavy query. You should use it carefully.
  • Loading branch information
kou committed Aug 28, 2012
1 parent 201da52 commit de5b3c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/bq-translator.js
Expand Up @@ -8,9 +8,6 @@
Script syntax grn_expr:
http://groonga.org/docs/reference/grn_expr/script_syntax.html
Unsupported syntaxes of Boolean Queries:
* (not ...)
*/

var IndexField = require('./database').IndexField;
Expand Down Expand Up @@ -110,6 +107,9 @@ BooleanQueryTranslator.prototype = {
case "and":
expression = this.translateGroupSetOperation(operator, "&&");
break;
case "not":
expression = this.translateGroupNot();
break;
case "or":
expression = this.translateGroupSetOperation(operator, "||");
break;
Expand Down Expand Up @@ -208,6 +208,16 @@ BooleanQueryTranslator.prototype = {
this.throwSyntaxError("close parenthesis is missing: " +
"operator:<" + label + ">");
},
translateGroupNot: function() {
var expression = this.translateExpression();
this.skipSpaces();
if (this.query[this.offset] == ")") {
this.offset++;
return "(all_records() &! " + expression + ")";
} else {
this.throwSyntaxError("close parenthesis is missing: operator:<not>");
}
},
translateExpression: function() {
if (this.query[this.offset] == "(") {
return this.translateGroup();
Expand Down
8 changes: 8 additions & 0 deletions test/bq-translator.test.js
Expand Up @@ -238,10 +238,18 @@ suite('BoolanQueryTranslator', function() {
"(or field1:'keyword1' field2:'keyword2') (other group)",
"(or field1:'keyword1' field2:'keyword2')".length,
"(field1 @ \"keyword1\" || field2 @ \"keyword2\")");
testGroup("not",
"(not field1:'keyword1') (other group)",
"(not field1:'keyword1')".length,
"(all_records() &! field1 @ \"keyword1\")");
testGroup("nested",
"(and (or field1:'k1' field2:'k2') field3:'k3') (other group)",
"(and (or field1:'k1' field2:'k2') field3:'k3')".length,
"((field1 @ \"k1\" || field2 @ \"k2\") && field3 @ \"k3\")");
testGroup("not and",
"(not (and field1:'k1' field2:'k2')) (other group)",
"(not (and field1:'k1' field2:'k2'))".length,
"(all_records() &! (field1 @ \"k1\" && field2 @ \"k2\"))");

testGroupError("missing open parentheis",
"and field1:'k1' field2:'k2')",
Expand Down

0 comments on commit de5b3c1

Please sign in to comment.