From a4794c135b6852fa8e86e3314d7cbded80d1129a Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sun, 30 Oct 2011 23:14:50 +0000 Subject: [PATCH] LUCENE-3542: Group expanded query terms to preserve parent boolean operator in StandardQueryParser git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1195275 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/contrib/CHANGES.txt | 3 +++ .../processors/AnalyzerQueryNodeProcessor.java | 8 ++------ .../queryparser/flexible/standard/TestQPHelper.java | 12 ++++++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lucene/contrib/CHANGES.txt b/lucene/contrib/CHANGES.txt index 598c6e6b2ad..31ca0f3f164 100644 --- a/lucene/contrib/CHANGES.txt +++ b/lucene/contrib/CHANGES.txt @@ -139,6 +139,9 @@ Bug Fixes * LUCENE-3508: Decompounders based on CompoundWordTokenFilterBase can now be used with custom attributes. All those attributes are preserved and set on all added decompounded tokens. (Spyros Kapnissis, Uwe Schindler) + + * LUCENE-3542: Group expanded query terms to preserve parent boolean operator + in StandartQueryParser. (Simon Willnauer) API Changes diff --git a/modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java b/modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java index 384adf63719..dad54691c9f 100644 --- a/modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java +++ b/modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AnalyzerQueryNodeProcessor.java @@ -203,12 +203,8 @@ public class AnalyzerQueryNodeProcessor extends QueryNodeProcessorImpl { children.add(new FieldQueryNode(field, term, -1, -1)); } - if (positionCount == 1) - return new GroupQueryNode( - new StandardBooleanQueryNode(children, true)); - else - return new StandardBooleanQueryNode(children, false); - + return new GroupQueryNode( + new StandardBooleanQueryNode(children, positionCount==1)); } else { // phrase query: MultiPhraseQueryNode mpq = new MultiPhraseQueryNode(); diff --git a/modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java b/modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java index a132341c280..55d155b8f40 100644 --- a/modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java +++ b/modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java @@ -359,8 +359,16 @@ public class TestQPHelper extends LuceneTestCase { BooleanQuery expected = new BooleanQuery(); expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.SHOULD); expected.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD); - assertEquals(expected, getQuery("中国", analyzer)); + + expected = new BooleanQuery(); + expected.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.MUST); + BooleanQuery inner = new BooleanQuery(); + inner.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.SHOULD); + inner.add(new TermQuery(new Term("field", "国")), BooleanClause.Occur.SHOULD); + expected.add(inner, BooleanClause.Occur.MUST); + assertEquals(expected, getQuery("中 AND 中国", new SimpleCJKAnalyzer())); + } public void testCJKBoostedTerm() throws Exception { @@ -609,7 +617,7 @@ public class TestQPHelper extends LuceneTestCase { assertQueryEquals("drop AND stop AND roll", qpAnalyzer, "+drop +roll"); assertQueryEquals("term phrase term", qpAnalyzer, - "term phrase1 phrase2 term"); + "term (phrase1 phrase2) term"); assertQueryEquals("term AND NOT phrase term", qpAnalyzer, "+term -(phrase1 phrase2) term");