From dfcb3ca2d4c44dec2242a945e906523c3757513f Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Wed, 30 Oct 2013 14:40:55 +0100 Subject: [PATCH] RegexpQueryBuilder now implements MultiTermQueryBuilder This allows the RegexpQueryBuilder to be used in span queries Added tests for all span multi term queries. Also updated the documentation and removed mentioning of numeric range queries for span queries (they have to be terms). Closes #3392 --- .../queries/span-multi-term-query.asciidoc | 2 +- .../index/query/RegexpQueryBuilder.java | 2 +- .../search/query/SimpleQueryTests.java | 68 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/docs/reference/query-dsl/queries/span-multi-term-query.asciidoc b/docs/reference/query-dsl/queries/span-multi-term-query.asciidoc index 7a6e4a688d7..76985faf61c 100644 --- a/docs/reference/query-dsl/queries/span-multi-term-query.asciidoc +++ b/docs/reference/query-dsl/queries/span-multi-term-query.asciidoc @@ -2,7 +2,7 @@ === Span Multi Term Query The `span_multi` query allows you to wrap a `multi term query` (one of -fuzzy, prefix, term range or numeric range query) as a `span query`, so +fuzzy, prefix, term range or regexp query) as a `span query`, so it can be nested. Example: [source,js] diff --git a/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java index 732768b23d9..d5096d4c594 100644 --- a/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -28,7 +28,7 @@ import java.io.IOException; * * */ -public class RegexpQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder { +public class RegexpQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder, MultiTermQueryBuilder { private final String name; private final String regexp; diff --git a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java index ccbd33e6a84..f0849c120dc 100644 --- a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java @@ -1505,6 +1505,74 @@ public class SimpleQueryTests extends AbstractIntegrationTest { assertHitCount(response, 3l); } + @Test + public void testSpanMultiTermQuery() throws ElasticSearchException, IOException { + + client().admin().indices().prepareCreate("test").setSettings( + ImmutableSettings.settingsBuilder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + ) + .execute().actionGet(); + ensureGreen(); + + client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() + .field("description", "foo other anything bar") + .field("count", 1) + .endObject()) + .execute().actionGet(); + + client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() + .field("description", "foo other anything") + .field("count", 2) + .endObject()) + .execute().actionGet(); + + client().prepareIndex("test", "test", "3").setSource(jsonBuilder().startObject() + .field("description", "foo other") + .field("count", 3) + .endObject()) + .execute().actionGet(); + + client().prepareIndex("test", "test", "4").setSource(jsonBuilder().startObject() + .field("description", "fop") + .field("count", 4) + .endObject()) + .execute().actionGet(); + + refresh(); + + SearchResponse response = client().prepareSearch("test") + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.fuzzyQuery("description", "fop")))) + .execute().actionGet(); + assertNoFailures(response); + assertHitCount(response, 4); + + response = client().prepareSearch("test") + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.prefixQuery("description", "fo")))) + .execute().actionGet(); + assertNoFailures(response); + assertHitCount(response, 4); + + response = client().prepareSearch("test") + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.wildcardQuery("description", "oth*")))) + .execute().actionGet(); + assertNoFailures(response); + assertHitCount(response, 3); + + response = client().prepareSearch("test") + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.rangeQuery("description").from("ffa").to("foo")))) + .execute().actionGet(); + assertNoFailures(response); + assertHitCount(response, 3); + + response = client().prepareSearch("test") + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.regexpQuery("description", "fo{2}")))) + .execute().actionGet(); + assertNoFailures(response); + assertHitCount(response, 3); + } + @Test public void testSimpleDFSQuery() throws ElasticSearchException, IOException { prepareCreate("test", -1,