From 59305dc384b52b730c9c8d3d9428041d2509ad68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 21 May 2015 17:55:06 +0200 Subject: [PATCH] Moving TermsLookupQueryBuilder to TermsQueryBuilder so we have 1:1 relation to existing parser, leaving old class as deprecated stub extending TermsQueryBuilder --- .../index/query/QueryBuilders.java | 8 +- .../index/query/TermsLookupQueryBuilder.java | 99 +------------------ .../index/query/TermsQueryBuilder.java | 91 ++++++++++++++--- .../count/query/CountQueryTests.java | 20 ++-- .../search/query/SearchQueryTests.java | 20 ++-- .../ContextAndHeaderTransportTests.java | 2 +- 6 files changed, 107 insertions(+), 133 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/QueryBuilders.java b/src/main/java/org/elasticsearch/index/query/QueryBuilders.java index fe2e572522b7e..078dac38137e2 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryBuilders.java +++ b/src/main/java/org/elasticsearch/index/query/QueryBuilders.java @@ -587,9 +587,9 @@ public static TypeQueryBuilder typeQuery(String type) { * A terms lookup filter for the provided field name. A lookup terms filter can * extract the terms to filter by from another doc in an index. */ - public static TermsLookupQueryBuilder termsLookupQuery(String name) { - return new TermsLookupQueryBuilder(name); - } + public static TermsQueryBuilder termsLookupQuery(String name, String lookupIndex, String lookupType, String lookupId, String lookupPath) { + return new TermsQueryBuilder(name, (Object[]) null).lookupIndex(lookupIndex).lookupType(lookupType).lookupId(lookupId).lookupPath(lookupPath); + } /** * A builder for filter based on a script. @@ -674,7 +674,7 @@ public static GeohashCellQuery.Builder geoHashCellQuery(String name, GeoPoint po public static GeohashCellQuery.Builder geoHashCellQuery(String name, String geohash, boolean neighbors) { return new GeohashCellQuery.Builder(name, geohash, neighbors); } - + /** * A filter to filter based on a polygon defined by a set of locations / points. * diff --git a/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java index c7e6f06e27f03..333e4ee8772dc 100644 --- a/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java @@ -19,109 +19,20 @@ package org.elasticsearch.index.query; -import org.elasticsearch.common.xcontent.XContentBuilder; - -import java.io.IOException; /** * A filter for a field based on several terms matching on any of them. + * @deprecated use {@link TermsQueryBuilder#TermsQueryBuilder(name, lookupIndex, lookupType, lookupId)} instead. */ -public class TermsLookupQueryBuilder extends QueryBuilder { - - public static final String NAME = "terms_lookup"; - - private final String name; - private String lookupIndex; - private String lookupType; - private String lookupId; - private String lookupRouting; - private String lookupPath; - private Boolean lookupCache; - - private String queryName; +@Deprecated +public class TermsLookupQueryBuilder extends TermsQueryBuilder { public TermsLookupQueryBuilder(String name) { - this.name = name; - } - - /** - * Sets the filter name for the filter that can be used when searching for matched_filters per hit. - */ - public TermsLookupQueryBuilder queryName(String queryName) { - this.queryName = queryName; - return this; - } - - /** - * Sets the index name to lookup the terms from. - */ - public TermsLookupQueryBuilder lookupIndex(String lookupIndex) { - this.lookupIndex = lookupIndex; - return this; - } - - /** - * Sets the index type to lookup the terms from. - */ - public TermsLookupQueryBuilder lookupType(String lookupType) { - this.lookupType = lookupType; - return this; - } - - /** - * Sets the doc id to lookup the terms from. - */ - public TermsLookupQueryBuilder lookupId(String lookupId) { - this.lookupId = lookupId; - return this; - } - - /** - * Sets the path within the document to lookup the terms from. - */ - public TermsLookupQueryBuilder lookupPath(String lookupPath) { - this.lookupPath = lookupPath; - return this; - } - - public TermsLookupQueryBuilder lookupRouting(String lookupRouting) { - this.lookupRouting = lookupRouting; - return this; - } - - public TermsLookupQueryBuilder lookupCache(boolean lookupCache) { - this.lookupCache = lookupCache; - return this; - } - - @Override - public void doXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(NAME); - - builder.startObject(name); - if (lookupIndex != null) { - builder.field("index", lookupIndex); - } - builder.field("type", lookupType); - builder.field("id", lookupId); - if (lookupRouting != null) { - builder.field("routing", lookupRouting); - } - if (lookupCache != null) { - builder.field("cache", lookupCache); - } - builder.field("path", lookupPath); - builder.endObject(); - - if (queryName != null) { - builder.field("_name", queryName); - } - - builder.endObject(); + super(name, (Object[]) null); } @Override public String queryId() { - return NAME; + return TermsQueryBuilder.NAME; } } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java index d82d00249999a..720027aec8cf5 100644 --- a/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java @@ -24,9 +24,9 @@ import java.io.IOException; /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. */ -public class TermsQueryBuilder extends QueryBuilder { +public class TermsQueryBuilder extends QueryBuilder { public static final String NAME = "terms"; @@ -38,8 +38,15 @@ public class TermsQueryBuilder extends QueryBuilder { private String execution; + private String lookupIndex; + private String lookupType; + private String lookupId; + private String lookupRouting; + private String lookupPath; + private Boolean lookupCache; + /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -49,7 +56,7 @@ public TermsQueryBuilder(String name, String... values) { } /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -60,7 +67,7 @@ public TermsQueryBuilder(String name, int... values) { } /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -71,7 +78,7 @@ public TermsQueryBuilder(String name, long... values) { } /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -82,7 +89,7 @@ public TermsQueryBuilder(String name, float... values) { } /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -93,7 +100,7 @@ public TermsQueryBuilder(String name, double... values) { } /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -104,7 +111,7 @@ public TermsQueryBuilder(String name, Object... values) { } /** - * A filer for a field based on several terms matching on any of them. + * A filter for a field based on several terms matching on any of them. * * @param name The field name * @param values The terms @@ -133,19 +140,75 @@ public TermsQueryBuilder queryName(String queryName) { return this; } + /** + * Sets the index name to lookup the terms from. + */ + public TermsQueryBuilder lookupIndex(String lookupIndex) { + this.lookupIndex = lookupIndex; + return this; + } + + /** + * Sets the index type to lookup the terms from. + */ + public TermsQueryBuilder lookupType(String lookupType) { + this.lookupType = lookupType; + return this; + } + + /** + * Sets the doc id to lookup the terms from. + */ + public TermsQueryBuilder lookupId(String lookupId) { + this.lookupId = lookupId; + return this; + } + + /** + * Sets the path within the document to lookup the terms from. + */ + public TermsQueryBuilder lookupPath(String lookupPath) { + this.lookupPath = lookupPath; + return this; + } + + public TermsQueryBuilder lookupRouting(String lookupRouting) { + this.lookupRouting = lookupRouting; + return this; + } + + public TermsQueryBuilder lookupCache(boolean lookupCache) { + this.lookupCache = lookupCache; + return this; + } + @Override public void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.field(name, values); - + if (values == null) { + builder.startObject(name); + if (lookupIndex != null) { + builder.field("index", lookupIndex); + } + builder.field("type", lookupType); + builder.field("id", lookupId); + if (lookupRouting != null) { + builder.field("routing", lookupRouting); + } + if (lookupCache != null) { + builder.field("cache", lookupCache); + } + builder.field("path", lookupPath); + builder.endObject(); + } else { + builder.field(name, values); + } if (execution != null) { builder.field("execution", execution); } - if (queryName != null) { - builder.field("_name", queryName); + builder.field("_name", queryName); } - builder.endObject(); } diff --git a/src/test/java/org/elasticsearch/count/query/CountQueryTests.java b/src/test/java/org/elasticsearch/count/query/CountQueryTests.java index e06b8fa607d1f..61f99e702426d 100644 --- a/src/test/java/org/elasticsearch/count/query/CountQueryTests.java +++ b/src/test/java/org/elasticsearch/count/query/CountQueryTests.java @@ -639,47 +639,47 @@ public void testTermsLookupFilter() throws Exception { client().prepareIndex("test", "type", "4").setSource("term", "4")); CountResponse countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "1", "terms"))).get(); assertHitCount(countResponse, 2l); // same as above, just on the _id... countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("_id").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("_id", "lookup", "type", "1", "terms"))).get(); assertHitCount(countResponse, 2l); // another search with same parameters... countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "1", "terms"))).get(); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("2").lookupPath("terms"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "2", "terms"))).get(); assertHitCount(countResponse, 1l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("3").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "3", "terms")) ).get(); assertNoFailures(countResponse); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("4").lookupPath("terms"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup", "type", "4", "terms"))).get(); assertHitCount(countResponse, 0l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("1").lookupPath("arr.term"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup2", "type", "1", "arr.term"))).get(); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("2").lookupPath("arr.term"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup2", "type", "2", "arr.term"))).get(); assertHitCount(countResponse, 1l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term", "lookup2", "type", "3", "arr.term"))).get(); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term"))).get(); + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("not_exists", "lookup2", "type", "3", "arr.term"))).get(); assertHitCount(countResponse, 0l); } diff --git a/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java b/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java index 64ea1c2f3caf7..21eb8d3724bdf 100644 --- a/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java @@ -1195,62 +1195,62 @@ public void testTermsLookupFilter() throws Exception { client().prepareIndex("test", "type", "4").setSource("term", "4") ); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup","type","1","terms")) ).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "1", "3"); // same as above, just on the _id... searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("_id").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("_id","lookup","type","1","terms")) ).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "1", "3"); // another search with same parameters... searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup","type","1","terms")) ).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "1", "3"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("2").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup","type","2","terms")) ).get(); assertHitCount(searchResponse, 1l); assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("3").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup","type","3","terms")) ).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "2", "4"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup").lookupType("type").lookupId("4").lookupPath("terms")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup","type","4","terms")) ).get(); assertHitCount(searchResponse, 0l); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("1").lookupPath("arr.term")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup2","type","1","arr.term")) ).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "1", "3"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("2").lookupPath("arr.term")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup2","type","2","arr.term")) ).get(); assertHitCount(searchResponse, 1l); assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("term","lookup2","type","3","arr.term")) ).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "2", "4"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) + .setQuery(filteredQuery(matchAllQuery(), termsLookupQuery("not_exists","lookup2","type","3","arr.term")) ).get(); assertHitCount(searchResponse, 0l); } diff --git a/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java b/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java index bc506de6bdc62..4357c397a3aae 100644 --- a/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java +++ b/src/test/java/org/elasticsearch/transport/ContextAndHeaderTransportTests.java @@ -122,7 +122,7 @@ public void testThatTermsLookupGetRequestContainsContextAndHeaders() throws Exce .setSource(jsonBuilder().startObject().field("username", "foo").endObject()).get(); transportClient().admin().indices().prepareRefresh(queryIndex, lookupIndex).get(); - TermsLookupQueryBuilder termsLookupFilterBuilder = QueryBuilders.termsLookupQuery("username").lookupIndex(lookupIndex).lookupType("type").lookupId("1").lookupPath("followers"); + TermsQueryBuilder termsLookupFilterBuilder = QueryBuilders.termsLookupQuery("username", lookupIndex, "type", "1", "followers"); BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(termsLookupFilterBuilder); SearchResponse searchResponse = transportClient()