From 1d7cdd7d222b0c7cfb706aa2531f8262fbc971ff Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 24 Nov 2014 13:16:36 +0100 Subject: [PATCH] Applied PR, changed the way defaults are handled and updated the docs. Closes #4452 --- .../query-dsl/queries/span-not-query.asciidoc | 10 ++++++- .../index/query/SpanNotQueryBuilder.java | 27 +++++++------------ .../index/query/SpanNotQueryParser.java | 21 +++++++++------ .../search/query/SimpleQueryTests.java | 17 +++++------- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/docs/reference/query-dsl/queries/span-not-query.asciidoc b/docs/reference/query-dsl/queries/span-not-query.asciidoc index 466c0c6c7d3..ad6fd8ca4a4 100644 --- a/docs/reference/query-dsl/queries/span-not-query.asciidoc +++ b/docs/reference/query-dsl/queries/span-not-query.asciidoc @@ -30,4 +30,12 @@ The `include` and `exclude` clauses can be any span type query. The `exclude` clause is the span query whose matches must not overlap those returned. -In the above example all documents with the term hoya are filtered except the ones that have 'la' preceding them. +In the above example all documents with the term hoya are filtered except the ones that have 'la' preceeding them. + +Other top level options: + +[horizontal] +`pre`:: If set the amount of tokens before the include span can't have overlap with the exclude span. +`post`:: If set the amount of tokens after the include span can't have overlap with the exclude span. +`dist`:: If set the amount of tokens from within the include span can't have overlap with the exclude span. Equivalent + of setting both `pre` and `post`. \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java index e42260b7f9a..b3a0e5d0def 100644 --- a/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java @@ -29,19 +29,17 @@ import java.io.IOException; */ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder { - public static final int NOT_SET = -1; - private SpanQueryBuilder include; private SpanQueryBuilder exclude; - private int dist = NOT_SET; + private Integer dist; - private int pre = NOT_SET; + private Integer pre; - private int post = NOT_SET; + private Integer post; - private float boost = NOT_SET; + private Float boost; private String queryName; @@ -94,32 +92,25 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu throw new ElasticsearchIllegalArgumentException("Must specify exclude when using spanNot query"); } - if (dist != NOT_SET && (pre != NOT_SET || post != NOT_SET)) { + if (dist != null && (pre != null || post != null)) { throw new ElasticsearchIllegalArgumentException("spanNot can either use [dist] or [pre] & [post] (or none)"); } - // set appropriate defaults - if (pre != NOT_SET && post == NOT_SET) { - post = 0; - } else if (pre == NOT_SET && post != NOT_SET){ - pre = 0; - } - builder.startObject(SpanNotQueryParser.NAME); builder.field("include"); include.toXContent(builder, params); builder.field("exclude"); exclude.toXContent(builder, params); - if (dist != NOT_SET) { + if (dist != null) { builder.field("dist", dist); } - if (pre != NOT_SET) { + if (pre != null) { builder.field("pre", pre); } - if (post != NOT_SET) { + if (post != null) { builder.field("post", post); } - if (boost != NOT_SET) { + if (boost != null) { builder.field("boost", boost); } if (queryName != null) { diff --git a/src/main/java/org/elasticsearch/index/query/SpanNotQueryParser.java b/src/main/java/org/elasticsearch/index/query/SpanNotQueryParser.java index b099db72cb4..afadf4c68ef 100644 --- a/src/main/java/org/elasticsearch/index/query/SpanNotQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/SpanNotQueryParser.java @@ -35,8 +35,6 @@ public class SpanNotQueryParser implements QueryParser { public static final String NAME = "span_not"; - public static final int NOT_SET = -1; - @Inject public SpanNotQueryParser() { } @@ -55,9 +53,9 @@ public class SpanNotQueryParser implements QueryParser { SpanQuery include = null; SpanQuery exclude = null; - int dist = NOT_SET; - int pre = NOT_SET; - int post = NOT_SET; + Integer dist = null; + Integer pre = null; + Integer post = null; String queryName = null; @@ -104,14 +102,21 @@ public class SpanNotQueryParser implements QueryParser { if (exclude == null) { throw new QueryParsingException(parseContext.index(), "spanNot must have [exclude] span query clause"); } - if (dist != NOT_SET && (pre != NOT_SET || post != NOT_SET)) { + if (dist != null && (pre != null || post != null)) { throw new QueryParsingException(parseContext.index(), "spanNot can either use [dist] or [pre] & [post] (or none)"); } + // set appropriate defaults + if (pre != null && post == null) { + post = 0; + } else if (pre == null && post != null){ + pre = 0; + } + SpanNotQuery query; - if (pre != NOT_SET && post != NOT_SET) { + if (pre != null && post != null) { query = new SpanNotQuery(include, exclude, pre, post); - } else if (dist != NOT_SET) { + } else if (dist != null) { query = new SpanNotQuery(include, exclude, dist); } else { query = new SpanNotQuery(include, exclude); diff --git a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java index 00c7488d233..47b8a4f5a8e 100644 --- a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java @@ -1553,18 +1553,15 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { .clause(QueryBuilders.spanTermQuery("description", "fox")).slop(1)).exclude(spanTermQuery("description", "jumped")).pre(1).post(1)).get(); assertHitCount(searchResponse, 1l); - SearchRequestBuilder builder = client().prepareSearch("test") - .setQuery(spanNotQuery().include(spanNearQuery() - .clause(QueryBuilders.spanTermQuery("description", "quick")) - .clause(QueryBuilders.spanTermQuery("description", "fox")).slop(1)).exclude(spanTermQuery("description", "jumped")).dist(2).pre(2)); - boolean caught = false; try { - builder.execute(); + client().prepareSearch("test") + .setQuery(spanNotQuery().include(spanNearQuery() + .clause(QueryBuilders.spanTermQuery("description", "quick")) + .clause(QueryBuilders.spanTermQuery("description", "fox")).slop(1)).exclude(spanTermQuery("description", "jumped")).dist(2).pre(2) + ).get(); + fail("ElasticsearchIllegalArgumentException should have been caught"); } catch (ElasticsearchException e) { - assertTrue("ElasticsearchIllegalArgumentException should have been caught", e.getDetailedMessage().endsWith("spanNot can either use [dist] or [pre] & [post] (or none)")); - caught = true; - } finally { - assertTrue("ElasticsearchIllegalArgumentException should have been caught", caught); + assertThat("ElasticsearchIllegalArgumentException should have been caught", e.getDetailedMessage(), containsString("spanNot can either use [dist] or [pre] & [post] (or none)")); } }