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,