From 1d240aaff66c6ed10cc46cb4b745cd8dd58d667d Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 2 Mar 2011 00:36:30 +0200 Subject: [PATCH] prefix inside a dis_max / bool query broken, closes #732. --- .../index/query/xcontent/PrefixQueryParser.java | 1 + .../xcontent/SimpleIndexQueryParserTests.java | 16 ++++++++++++++++ .../index/query/xcontent/disMax2.json | 14 ++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/disMax2.json diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/PrefixQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/PrefixQueryParser.java index 263458f2458..80fc3d58fe5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/PrefixQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/PrefixQueryParser.java @@ -77,6 +77,7 @@ public class PrefixQueryParser extends AbstractIndexComponent implements XConten } } } + parser.nextToken(); } else { value = parser.text(); parser.nextToken(); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java index 78fe59cab88..2d231a42973 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java @@ -252,6 +252,22 @@ public class SimpleIndexQueryParserTests { assertThat(((TermQuery) secondsQ).getTerm(), equalTo(new Term("name.last", "last"))); } + @Test public void testDisMax2() throws Exception { + IndexQueryParser queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/index/query/xcontent/disMax2.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(DisjunctionMaxQuery.class)); + DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) parsedQuery; + + List disjuncts = Queries.disMaxClauses(disjunctionMaxQuery); + assertThat(disjuncts.size(), equalTo(1)); + + PrefixQuery firstQ = (PrefixQuery) disjuncts.get(0); + // since age is automatically registered in data, we encode it as numeric + assertThat(firstQ.getPrefix(), equalTo(new Term("name.first", "sh"))); + assertThat((double) firstQ.getBoost(), closeTo(1.2, 0.00001)); + } + @Test public void testTermQueryBuilder() throws IOException { IndexQueryParser queryParser = queryParser(); Query parsedQuery = queryParser.parse(termQuery("age", 34).buildAsBytes()).query(); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/disMax2.json b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/disMax2.json new file mode 100644 index 00000000000..6325f15ae7e --- /dev/null +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/disMax2.json @@ -0,0 +1,14 @@ +{ + "dis_max" : { + "queries" : [ + { + "prefix" : { + "name.first" : { + "value" : "sh", + "boost" : 1.2 + } + } + } + ] + } +} \ No newline at end of file