Skip to content

Commit

Permalink
Don't use smart query wrapping for span term query
Browse files Browse the repository at this point in the history
Lucenes span queries are a different family than 'ordinary' queries
in lucene. Spans only work with other spans such that smart query
wrapping doesn't work with span queries at all ie. we can't wrap
in filtered query.

Closes elastic#2994
  • Loading branch information
s1monw committed May 7, 2013
1 parent 992a40c commit c1e8d47
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars

SpanTermQuery query = new SpanTermQuery(new Term(fieldName, valueBytes));
query.setBoost(boost);
return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ public static void assertSearchHit(SearchResponse searchResponse, int number, Ma
assertThat(searchResponse.getHits().totalHits(), greaterThanOrEqualTo((long)number));
assertSearchHit(searchResponse.getHits().getAt(number-1), matcher);
}

public static void assertNoFailures(SearchResponse searchResponse) {
assertThat("Unexpectd ShardFailures: " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0));
}

public static void assertSearchHit(SearchHit searchHit, Matcher<SearchHit> matcher) {
assertThat(searchHit, matcher);
}

public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, Matcher<String> matcher) {
assertThat("Unexpectd ShardFailures: " + Arrays.toString(resp.getShardFailures()), resp.getShardFailures().length, equalTo(0));
assertNoFailures(resp);
assertThat("not enough hits", resp.getHits().hits().length, greaterThan(hit));
assertThat(resp.getHits().hits()[hit].getHighlightFields().get(field), notNullValue());
assertThat(resp.getHits().hits()[hit].getHighlightFields().get(field).fragments().length, greaterThan(fragment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -1298,5 +1299,57 @@ public void testMustNot() throws ElasticSearchException, IOException {
assertThat(response.getShardFailures().length, equalTo(0));
assertThat(response.getHits().totalHits(), equalTo(2l));
}

@Test // see #2994
public void testSimpleSpan() throws ElasticSearchException, IOException {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test").setSettings(
ImmutableSettings.settingsBuilder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
)
.execute().actionGet();

client.prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject()
.field("description", "foo other anything bar")
.endObject())
.execute().actionGet();

client.prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject()
.field("description", "foo other anything")
.endObject())
.execute().actionGet();

client.prepareIndex("test", "test", "3").setSource(jsonBuilder().startObject()
.field("description", "foo other")
.endObject())
.execute().actionGet();

client.prepareIndex("test", "test", "4").setSource(jsonBuilder().startObject()
.field("description", "foo")
.endObject())
.execute().actionGet();

client.admin().indices().prepareRefresh().execute().actionGet();

SearchResponse response = client.prepareSearch("test")
.setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("description", "bar")))
.execute().actionGet();
assertNoFailures(response);
assertHitCount(response, 1l);
response = client.prepareSearch("test")
.setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("test.description", "bar")))
.execute().actionGet();
assertNoFailures(response);
assertHitCount(response, 1l);

response = client.prepareSearch("test").setQuery(
QueryBuilders.spanNearQuery()
.clause(QueryBuilders.spanTermQuery("description", "foo"))
.clause(QueryBuilders.spanTermQuery("test.description", "other"))
.slop(3)).execute().actionGet();
assertNoFailures(response);
assertHitCount(response, 3l);
}

}

0 comments on commit c1e8d47

Please sign in to comment.