Skip to content

Commit

Permalink
fix terrible bug where ANDed terms + phrases against the same field w…
Browse files Browse the repository at this point in the history
…ould be rewritten as ORs!
  • Loading branch information
eeeebbbbrrrr committed Jun 20, 2016
1 parent c7a47da commit 39e6da0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 68 deletions.
Expand Up @@ -92,13 +92,23 @@ private void pullOutComplexTokensFromArrays(QueryParserNode root) {
}
if (!complex.isEmpty()) {
root.renumber();
ASTOr or = new ASTOr(QueryParserTreeConstants.JJTOR);
for (QueryParserNode node : complex)
or.jjtAddChild(node, or.jjtGetNumChildren());
((QueryParserNode) root.parent).replaceChild(root, or);
QueryParserNode newNode;

if (((ASTArray) root).isAnd()) {
ASTAnd and = new ASTAnd(QueryParserTreeConstants.JJTAND);
for (QueryParserNode node : complex)
and.jjtAddChild(node, and.jjtGetNumChildren());
newNode = and;
} else {
ASTOr or = new ASTOr(QueryParserTreeConstants.JJTOR);
for (QueryParserNode node : complex)
or.jjtAddChild(node, or.jjtGetNumChildren());
newNode = or;
}

((QueryParserNode) root.parent).replaceChild(root, newNode);
if (root.jjtGetNumChildren() > 0)
or.jjtAddChild(root, or.jjtGetNumChildren());
newNode.jjtAddChild(root, newNode.jjtGetNumChildren());
}
} else {
for (QueryParserNode child : root)
Expand Down
Expand Up @@ -3056,7 +3056,7 @@ public void testIssue_20_ValidateParsing() throws Exception {
" \"bool\" : {\n" +
" \"must\" : [ {\n" +
" \"bool\" : {\n" +
" \"should\" : [ {\n" +
" \"must\" : [ {\n" +
" \"wildcard\" : {\n" +
" \"exact_field\" : \"phrase with *wildcard*\"\n" +
" }\n" +
Expand Down Expand Up @@ -3084,7 +3084,7 @@ public void testIssue_20_ValidateParsing() throws Exception {
" }\n" +
" }, {\n" +
" \"bool\" : {\n" +
" \"should\" : [ {\n" +
" \"must\" : [ {\n" +
" \"match\" : {\n" +
" \"phrase_field\" : {\n" +
" \"query\" : \"phrase value\",\n" +
Expand Down Expand Up @@ -3378,7 +3378,7 @@ public void testRandomStringsJson() throws Exception {
assertJson("phrase_field:(asdflkj234-132asdfuj asiodfja;sdf #487adqerydfskf0230 &@#$23)",
"{\n" +
" \"bool\" : {\n" +
" \"should\" : [ {\n" +
" \"must\" : [ {\n" +
" \"match\" : {\n" +
" \"phrase_field\" : {\n" +
" \"query\" : \"asdflkj234-132asdfuj\",\n" +
Expand Down Expand Up @@ -4724,5 +4724,16 @@ public void testRegexProximityWithAPhrase() throws Exception {
);
}

@Test
public void testComplexTokenPulloutWithAND() throws Exception {
assertAST("english_field:(\"I''ll see you later\" and darling)",
"QueryTree\n" +
" Expansion\n" +
" id=<db.schema.table.index>id\n" +
" And\n" +
" Phrase (fieldname=english_field, operator=CONTAINS, value=I''ll see you later, index=db.schema.table.index)\n" +
" Array (fieldname=english_field, operator=CONTAINS, index=db.schema.table.index) (AND)\n" +
" Word (fieldname=english_field, operator=CONTAINS, value=darl, index=db.schema.table.index)");
}
}

0 comments on commit 39e6da0

Please sign in to comment.