Don't use smart query wrapping for span term query

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 #2994
This commit is contained in:
Simon Willnauer 2013-05-07 22:25:42 +02:00
parent 992a40cbd8
commit c1e8d4787a
3 changed files with 59 additions and 2 deletions

View File

@ -105,6 +105,6 @@ public class SpanTermQueryParser implements QueryParser {
SpanTermQuery query = new SpanTermQuery(new Term(fieldName, valueBytes));
query.setBoost(boost);
return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
return query;
}
}

View File

@ -57,13 +57,17 @@ public class ElasticsearchAssertions {
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));

View File

@ -43,6 +43,7 @@ import java.util.Arrays;
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;
@ -1298,5 +1299,57 @@ public class SimpleQueryTests extends AbstractNodesTests {
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);
}
}