Query DSL: span_containing and span_within override default boost coming from lucene

SpanContainingQueryParser and SpanWithinQueryParser always set the boost to the parsed lucene query, even if it is the default one. The default boost of the main query though is the boost coming from the inner little query, value that we end up overriding all the time. We should instead set the boost to the main query only if it differs from the default, to mimic lucene's behaviour.

Relates to #13272
Closes #13339
This commit is contained in:
javanna 2015-09-04 09:32:32 +02:00 committed by Luca Cavanna
parent 37c90b1047
commit 873d69f157
3 changed files with 38 additions and 14 deletions

View File

@ -91,7 +91,9 @@ public class SpanContainingQueryParser implements QueryParser {
} }
Query query = new SpanContainingQuery(big, little); Query query = new SpanContainingQuery(big, little);
if (boost != 1.0F) {
query.setBoost(boost); query.setBoost(boost);
}
if (queryName != null) { if (queryName != null) {
parseContext.addNamedQuery(queryName, query); parseContext.addNamedQuery(queryName, query);
} }

View File

@ -91,7 +91,9 @@ public class SpanWithinQueryParser implements QueryParser {
} }
Query query = new SpanWithinQuery(big, little); Query query = new SpanWithinQuery(big, little);
if (boost != 1.0F) {
query.setBoost(boost); query.setBoost(boost);
}
if (queryName != null) { if (queryName != null) {
parseContext.addNamedQuery(queryName, query); parseContext.addNamedQuery(queryName, query);
} }

View File

@ -1410,12 +1410,22 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
@Test @Test
public void testSpanWithinQueryBuilder() throws IOException { public void testSpanWithinQueryBuilder() throws IOException {
IndexQueryParserService queryParser = queryParser(); IndexQueryParserService queryParser = queryParser();
Query expectedQuery = new SpanWithinQuery(new SpanTermQuery(new Term("age", longToPrefixCoded(34, 0))), SpanTermQuery big = new SpanTermQuery(new Term("age", longToPrefixCoded(34, 0)));
new SpanTermQuery(new Term("age", longToPrefixCoded(35, 0)))); big.setBoost(2);
Query actualQuery = queryParser.parse(spanWithinQuery() SpanTermQuery little = new SpanTermQuery(new Term("age", longToPrefixCoded(35, 0)));
.big(spanTermQuery("age", 34)) little.setBoost(3);
.little(spanTermQuery("age", 35))) Query expectedQuery = new SpanWithinQuery(big, little);
.query();
SpanWithinQueryBuilder spanWithinQueryBuilder = spanWithinQuery()
.big(spanTermQuery("age", 34).boost(2))
.little(spanTermQuery("age", 35).boost(3));
Query actualQuery = queryParser.parse(spanWithinQueryBuilder).query();
assertEquals(expectedQuery, actualQuery);
float boost = randomFloat();
expectedQuery.setBoost(boost);
spanWithinQueryBuilder.boost(boost);
actualQuery = queryParser.parse(spanWithinQueryBuilder).query();
assertEquals(expectedQuery, actualQuery); assertEquals(expectedQuery, actualQuery);
} }
@ -1432,12 +1442,22 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
@Test @Test
public void testSpanContainingQueryBuilder() throws IOException { public void testSpanContainingQueryBuilder() throws IOException {
IndexQueryParserService queryParser = queryParser(); IndexQueryParserService queryParser = queryParser();
Query expectedQuery = new SpanContainingQuery(new SpanTermQuery(new Term("age", longToPrefixCoded(34, 0))), SpanTermQuery big = new SpanTermQuery(new Term("age", longToPrefixCoded(34, 0)));
new SpanTermQuery(new Term("age", longToPrefixCoded(35, 0)))); big.setBoost(2);
Query actualQuery = queryParser.parse(spanContainingQuery() SpanTermQuery little = new SpanTermQuery(new Term("age", longToPrefixCoded(35, 0)));
.big(spanTermQuery("age", 34)) little.setBoost(3);
.little(spanTermQuery("age", 35))) Query expectedQuery = new SpanContainingQuery(big, little);
.query();
SpanContainingQueryBuilder spanContainingQueryBuilder = spanContainingQuery()
.big(spanTermQuery("age", 34).boost(2))
.little(spanTermQuery("age", 35).boost(3));
Query actualQuery = queryParser.parse(spanContainingQueryBuilder).query();
assertEquals(expectedQuery, actualQuery);
float boost = randomFloat();
expectedQuery.setBoost(boost);
spanContainingQueryBuilder.boost(boost);
actualQuery = queryParser.parse(spanContainingQueryBuilder).query();
assertEquals(expectedQuery, actualQuery); assertEquals(expectedQuery, actualQuery);
} }