From 392003591d5cc758c88aaedc4e741b457b390b37 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 | 8 ++++++ .../index/query/SpanNotQueryBuilder.java | 27 +++++++------------ .../index/query/SpanNotQueryParser.java | 21 +++++++++------ .../search/query/SimpleQueryTests.java | 17 +++++------- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/docs/reference/query-dsl/queries/span-not-query.asciidoc b/docs/reference/query-dsl/queries/span-not-query.asciidoc index d49d3ef116a90..ad6fd8ca4a4fc 100644 --- a/docs/reference/query-dsl/queries/span-not-query.asciidoc +++ b/docs/reference/query-dsl/queries/span-not-query.asciidoc @@ -31,3 +31,11 @@ The `include` and `exclude` clauses can be any span type query. The returned. 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 e42260b7f9a90..b3a0e5d0def84 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 @@ */ 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 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep 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 b099db72cb4cc..afadf4c68ef0f 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 Query parse(QueryParseContext parseContext) throws IOException, QueryPars 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 Query parse(QueryParseContext parseContext) throws IOException, QueryPars 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 db2494bfbcd7c..1a66359904a5c 100644 --- a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java @@ -1536,18 +1536,15 @@ public void testSpanNot() throws ElasticsearchException, IOException, ExecutionE .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)")); } }