Applied PR, changed the way defaults are handled and updated the docs.

Closes #4452
This commit is contained in:
Martijn van Groningen 2014-11-24 13:16:36 +01:00
parent 5a001e1357
commit 1d7cdd7d22
4 changed files with 38 additions and 37 deletions

View File

@ -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`.

View File

@ -29,19 +29,17 @@ import java.io.IOException;
*/
public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNotQueryBuilder> {
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) {

View File

@ -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);

View File

@ -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)"));
}
}