Skip to content

Commit 21de386

Browse files
committed
Applied PR, changed the way defaults are handled and updated the docs.
Closes #4452
1 parent 2e0db12 commit 21de386

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

docs/reference/query-dsl/queries/span-not-query.asciidoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ The `include` and `exclude` clauses can be any span type query. The
3131
returned.
3232

3333
In the above example all documents with the term hoya are filtered except the ones that have 'la' preceeding them.
34+
35+
Other top level options:
36+
37+
[horizontal]
38+
`pre`:: If set the amount of tokens before the include span can't have overlap with the exclude span.
39+
`post`:: If set the amount of tokens after the include span can't have overlap with the exclude span.
40+
`dist`:: If set the amount of tokens from within the include span can't have overlap with the exclude span. Equivalent
41+
of setting both `pre` and `post`.

src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@
2929
*/
3030
public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNotQueryBuilder> {
3131

32-
public static final int NOT_SET = -1;
33-
3432
private SpanQueryBuilder include;
3533

3634
private SpanQueryBuilder exclude;
3735

38-
private int dist = NOT_SET;
36+
private Integer dist;
3937

40-
private int pre = NOT_SET;
38+
private Integer pre;
4139

42-
private int post = NOT_SET;
40+
private Integer post;
4341

44-
private float boost = NOT_SET;
42+
private Float boost;
4543

4644
private String queryName;
4745

@@ -94,32 +92,25 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
9492
throw new ElasticsearchIllegalArgumentException("Must specify exclude when using spanNot query");
9593
}
9694

97-
if (dist != NOT_SET && (pre != NOT_SET || post != NOT_SET)) {
95+
if (dist != null && (pre != null || post != null)) {
9896
throw new ElasticsearchIllegalArgumentException("spanNot can either use [dist] or [pre] & [post] (or none)");
9997
}
10098

101-
// set appropriate defaults
102-
if (pre != NOT_SET && post == NOT_SET) {
103-
post = 0;
104-
} else if (pre == NOT_SET && post != NOT_SET){
105-
pre = 0;
106-
}
107-
10899
builder.startObject(SpanNotQueryParser.NAME);
109100
builder.field("include");
110101
include.toXContent(builder, params);
111102
builder.field("exclude");
112103
exclude.toXContent(builder, params);
113-
if (dist != NOT_SET) {
104+
if (dist != null) {
114105
builder.field("dist", dist);
115106
}
116-
if (pre != NOT_SET) {
107+
if (pre != null) {
117108
builder.field("pre", pre);
118109
}
119-
if (post != NOT_SET) {
110+
if (post != null) {
120111
builder.field("post", post);
121112
}
122-
if (boost != NOT_SET) {
113+
if (boost != null) {
123114
builder.field("boost", boost);
124115
}
125116
if (queryName != null) {

src/main/java/org/elasticsearch/index/query/SpanNotQueryParser.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public class SpanNotQueryParser implements QueryParser {
3535

3636
public static final String NAME = "span_not";
3737

38-
public static final int NOT_SET = -1;
39-
4038
@Inject
4139
public SpanNotQueryParser() {
4240
}
@@ -55,9 +53,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
5553
SpanQuery include = null;
5654
SpanQuery exclude = null;
5755

58-
int dist = NOT_SET;
59-
int pre = NOT_SET;
60-
int post = NOT_SET;
56+
Integer dist = null;
57+
Integer pre = null;
58+
Integer post = null;
6159

6260
String queryName = null;
6361

@@ -104,14 +102,21 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
104102
if (exclude == null) {
105103
throw new QueryParsingException(parseContext.index(), "spanNot must have [exclude] span query clause");
106104
}
107-
if (dist != NOT_SET && (pre != NOT_SET || post != NOT_SET)) {
105+
if (dist != null && (pre != null || post != null)) {
108106
throw new QueryParsingException(parseContext.index(), "spanNot can either use [dist] or [pre] & [post] (or none)");
109107
}
110108

109+
// set appropriate defaults
110+
if (pre != null && post == null) {
111+
post = 0;
112+
} else if (pre == null && post != null){
113+
pre = 0;
114+
}
115+
111116
SpanNotQuery query;
112-
if (pre != NOT_SET && post != NOT_SET) {
117+
if (pre != null && post != null) {
113118
query = new SpanNotQuery(include, exclude, pre, post);
114-
} else if (dist != NOT_SET) {
119+
} else if (dist != null) {
115120
query = new SpanNotQuery(include, exclude, dist);
116121
} else {
117122
query = new SpanNotQuery(include, exclude);

src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,18 +1536,15 @@ public void testSpanNot() throws ElasticsearchException, IOException, ExecutionE
15361536
.clause(QueryBuilders.spanTermQuery("description", "fox")).slop(1)).exclude(spanTermQuery("description", "jumped")).pre(1).post(1)).get();
15371537
assertHitCount(searchResponse, 1l);
15381538

1539-
SearchRequestBuilder builder = client().prepareSearch("test")
1540-
.setQuery(spanNotQuery().include(spanNearQuery()
1541-
.clause(QueryBuilders.spanTermQuery("description", "quick"))
1542-
.clause(QueryBuilders.spanTermQuery("description", "fox")).slop(1)).exclude(spanTermQuery("description", "jumped")).dist(2).pre(2));
1543-
boolean caught = false;
15441539
try {
1545-
builder.execute();
1540+
client().prepareSearch("test")
1541+
.setQuery(spanNotQuery().include(spanNearQuery()
1542+
.clause(QueryBuilders.spanTermQuery("description", "quick"))
1543+
.clause(QueryBuilders.spanTermQuery("description", "fox")).slop(1)).exclude(spanTermQuery("description", "jumped")).dist(2).pre(2)
1544+
).get();
1545+
fail("ElasticsearchIllegalArgumentException should have been caught");
15461546
} catch (ElasticsearchException e) {
1547-
assertTrue("ElasticsearchIllegalArgumentException should have been caught", e.getDetailedMessage().endsWith("spanNot can either use [dist] or [pre] & [post] (or none)"));
1548-
caught = true;
1549-
} finally {
1550-
assertTrue("ElasticsearchIllegalArgumentException should have been caught", caught);
1547+
assertThat("ElasticsearchIllegalArgumentException should have been caught", e.getDetailedMessage(), containsString("spanNot can either use [dist] or [pre] & [post] (or none)"));
15511548
}
15521549
}
15531550

0 commit comments

Comments
 (0)