Make slop optional when parsing `span_near` query (#25677)

The slop parameter defaults to 0 in the Lucene SpanNearQuery, so we can set it
to this default value also and don't have to require it being specified in the
query when using the Rest API. Leaving `slop` a ctro arg in the Java API as it
should normally be specified and we can keep it `final` that way.

Closes #25642
This commit is contained in:
Christoph Büscher 2017-07-13 09:21:49 +02:00 committed by GitHub
parent 02e9ad6d6f
commit 97c4c43fb7
2 changed files with 26 additions and 6 deletions

View File

@ -45,6 +45,8 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
/** Default for flag controlling whether matches are required to be in-order */ /** Default for flag controlling whether matches are required to be in-order */
public static boolean DEFAULT_IN_ORDER = true; public static boolean DEFAULT_IN_ORDER = true;
/** Default slop value, this is the same that lucene {@link SpanNearQuery} uses if no slop is provided */
public static int DEFAULT_SLOP = 0;
private static final ParseField SLOP_FIELD = new ParseField("slop"); private static final ParseField SLOP_FIELD = new ParseField("slop");
private static final ParseField CLAUSES_FIELD = new ParseField("clauses"); private static final ParseField CLAUSES_FIELD = new ParseField("clauses");
@ -145,8 +147,8 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
public static SpanNearQueryBuilder fromXContent(XContentParser parser) throws IOException { public static SpanNearQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST; float boost = AbstractQueryBuilder.DEFAULT_BOOST;
Integer slop = null; int slop = DEFAULT_SLOP;
boolean inOrder = SpanNearQueryBuilder.DEFAULT_IN_ORDER; boolean inOrder = DEFAULT_IN_ORDER;
String queryName = null; String queryName = null;
List<SpanQueryBuilder> clauses = new ArrayList<>(); List<SpanQueryBuilder> clauses = new ArrayList<>();
@ -189,10 +191,6 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
throw new ParsingException(parser.getTokenLocation(), "span_near must include [clauses]"); throw new ParsingException(parser.getTokenLocation(), "span_near must include [clauses]");
} }
if (slop == null) {
throw new ParsingException(parser.getTokenLocation(), "span_near must include [slop]");
}
SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(clauses.get(0), slop); SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(clauses.get(0), slop);
for (int i = 1; i < clauses.size(); i++) { for (int i = 1; i < clauses.size(); i++) {
queryBuilder.addClause(clauses.get(i)); queryBuilder.addClause(clauses.get(i));

View File

@ -113,6 +113,28 @@ public class SpanNearQueryBuilderTests extends AbstractQueryTestCase<SpanNearQue
assertEquals(json, false, parsed.inOrder()); assertEquals(json, false, parsed.inOrder());
} }
public void testParsingSlopDefault() throws IOException {
String json =
"{\n" +
" \"span_near\" : {\n" +
" \"clauses\" : [ {\n" +
" \"span_term\" : {\n" +
" \"field\" : {\n" +
" \"value\" : \"value1\",\n" +
" \"boost\" : 1.0\n" +
" }\n" +
" }\n" +
" }]\n" +
" }\n" +
"}";
SpanNearQueryBuilder parsed = (SpanNearQueryBuilder) parseQuery(json);
assertEquals(json, 1, parsed.clauses().size());
assertEquals(json, SpanNearQueryBuilder.DEFAULT_SLOP, parsed.slop());
assertEquals(json, SpanNearQueryBuilder.DEFAULT_BOOST, parsed.boost(), 0.0);
assertEquals(json, SpanNearQueryBuilder.DEFAULT_IN_ORDER, parsed.inOrder());
}
public void testCollectPayloadsNoLongerSupported() throws Exception { public void testCollectPayloadsNoLongerSupported() throws Exception {
String json = String json =
"{\n" + "{\n" +