Skip to content

Commit

Permalink
Applied PR, changed the way defaults are handled and updated the docs.
Browse files Browse the repository at this point in the history
Closes #4452
  • Loading branch information
martijnvg committed Nov 24, 2014
1 parent 5a001e1 commit 1d7cdd7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
10 changes: 9 additions & 1 deletion docs/reference/query-dsl/queries/span-not-query.asciidoc
Expand Up @@ -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`.
Expand Up @@ -29,19 +29,17 @@
*/
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;

Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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() {
}
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
17 changes: 7 additions & 10 deletions src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java
Expand Up @@ -1553,18 +1553,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)"));
}
}

Expand Down

0 comments on commit 1d7cdd7

Please sign in to comment.