parent
db431b7cb3
commit
23d2b1ea7b
|
@ -87,7 +87,7 @@ include::request/fields.asciidoc[]
|
|||
|
||||
include::request/script-fields.asciidoc[]
|
||||
|
||||
include::request/filter.asciidoc[]
|
||||
include::request/post-filter.asciidoc[]
|
||||
|
||||
include::request/highlighting.asciidoc[]
|
||||
|
||||
|
|
|
@ -1,10 +1,21 @@
|
|||
[[search-request-filter]]
|
||||
=== Filter
|
||||
[[search-request-post-filter]]
|
||||
=== Post filter
|
||||
|
||||
When doing things like facet navigation, sometimes only the hits are
|
||||
needed to be filtered by the chosen facet, and all the facets should
|
||||
continue to be calculated based on the original query. The `filter`
|
||||
element within the search request can be used to accomplish it.
|
||||
The `post_filter` allows any filter that it holds to be executed as last filter, because
|
||||
of this the `post_filter` only has affect on the search hits and not facets.
|
||||
|
||||
There are several reasons why to specify filters as `post_filter`. One reason is to force
|
||||
expensive filters to be executed as last filter, so that these filters only operate on the
|
||||
docs that match with the rest of the query. An example of for what filter a post_filter
|
||||
should be used for this reason is the `geo_distance` filter. The `geo_distance` filter is
|
||||
in general an expensive filter to execute and to reduce the execution time for this filter,
|
||||
one can choose to specify it as `post_filter`, so it runs on documents that are very likely
|
||||
to be end up as matching documents.
|
||||
|
||||
Another important reason is when doing things like facet navigation,
|
||||
sometimes only the hits are needed to be filtered by the chosen facet,
|
||||
and all the facets should continue to be calculated based on the original query.
|
||||
The `post_filter` element within the search request can be used to accomplish it.
|
||||
|
||||
Note, this is different compared to creating a `filtered` query with the
|
||||
filter, since this will cause the facets to only process the filtered
|
||||
|
@ -60,7 +71,7 @@ curl -XPOST 'localhost:9200/twitter/_search?pretty=true' -d '
|
|||
"query" : {
|
||||
"term" : { "message" : "something" }
|
||||
},
|
||||
"filter" : {
|
||||
"post_filter" : {
|
||||
"term" : { "tag" : "green" }
|
||||
},
|
||||
"facets" : {
|
||||
|
@ -76,3 +87,5 @@ And now, we get only 1 hit back, but the facets remain the same.
|
|||
|
||||
Note, if additional filters are required on specific facets, they can be
|
||||
added as a `facet_filter` to the relevant facets.
|
||||
|
||||
deprecated[0.90.8, The top level `filter` has been renamed to `post_filter`]
|
|
@ -32,7 +32,7 @@ curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query' -d '{
|
|||
"query" : "*:*"
|
||||
}
|
||||
},
|
||||
"filter" : {
|
||||
"post_filter" : {
|
||||
"term" : { "user" : "kimchy" }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -239,11 +239,11 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
* Sets a filter that will be executed after the query has been executed and only has affect on the search hits
|
||||
* (not aggregations or facets). This filter is always executed as last filtering mechanism.
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(FilterBuilder filter) {
|
||||
sourceBuilder().filter(filter);
|
||||
public SearchRequestBuilder setPostFilter(FilterBuilder postFilter) {
|
||||
sourceBuilder().postFilter(postFilter);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -251,8 +251,8 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(String filter) {
|
||||
sourceBuilder().filter(filter);
|
||||
public SearchRequestBuilder setPostFilter(String postFilter) {
|
||||
sourceBuilder().postFilter(postFilter);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -260,8 +260,8 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(BytesReference filter) {
|
||||
sourceBuilder().filter(filter);
|
||||
public SearchRequestBuilder setPostFilter(BytesReference postFilter) {
|
||||
sourceBuilder().postFilter(postFilter);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -269,8 +269,8 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(byte[] filter) {
|
||||
sourceBuilder().filter(filter);
|
||||
public SearchRequestBuilder setPostFilter(byte[] postFilter) {
|
||||
sourceBuilder().postFilter(postFilter);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -278,8 +278,8 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(byte[] filter, int filterOffset, int filterLength) {
|
||||
sourceBuilder().filter(filter, filterOffset, filterLength);
|
||||
public SearchRequestBuilder setPostFilter(byte[] postFilter, int postFilterOffset, int postFilterLength) {
|
||||
sourceBuilder().postFilter(postFilter, postFilterOffset, postFilterLength);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -287,8 +287,8 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(XContentBuilder filter) {
|
||||
sourceBuilder().filter(filter);
|
||||
public SearchRequestBuilder setPostFilter(XContentBuilder postFilter) {
|
||||
sourceBuilder().postFilter(postFilter);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -296,8 +296,8 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchRequestBuilder setFilter(Map filter) {
|
||||
sourceBuilder().filter(filter);
|
||||
public SearchRequestBuilder setPostFilter(Map postFilter) {
|
||||
sourceBuilder().postFilter(postFilter);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -529,12 +529,12 @@ public class PercolateContext extends SearchContext {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SearchContext parsedFilter(ParsedFilter filter) {
|
||||
public SearchContext parsedPostFilter(ParsedFilter postFilter) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParsedFilter parsedFilter() {
|
||||
public ParsedFilter parsedPostFilter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ public class SearchSourceBuilder implements ToXContent {
|
|||
|
||||
private BytesReference queryBinary;
|
||||
|
||||
private FilterBuilder filterBuilder;
|
||||
private FilterBuilder postFilterBuilder;
|
||||
|
||||
private BytesReference filterBinary;
|
||||
|
||||
|
@ -186,11 +186,11 @@ public class SearchSourceBuilder implements ToXContent {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
* Sets a filter that will be executed after the query has been executed and only has affect on the search hits
|
||||
* (not aggregations or facets). This filter is always executed as last filtering mechanism.
|
||||
*/
|
||||
public SearchSourceBuilder filter(FilterBuilder filter) {
|
||||
this.filterBuilder = filter;
|
||||
public SearchSourceBuilder postFilter(FilterBuilder postFilter) {
|
||||
this.postFilterBuilder = postFilter;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -198,52 +198,52 @@ public class SearchSourceBuilder implements ToXContent {
|
|||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchSourceBuilder filter(String filterString) {
|
||||
return filter(filterString.getBytes(Charsets.UTF_8));
|
||||
public SearchSourceBuilder postFilter(String postFilterString) {
|
||||
return postFilter(postFilterString.getBytes(Charsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchSourceBuilder filter(byte[] filter) {
|
||||
return filter(filter, 0, filter.length);
|
||||
public SearchSourceBuilder postFilter(byte[] postFilter) {
|
||||
return postFilter(postFilter, 0, postFilter.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchSourceBuilder filter(byte[] filterBinary, int filterBinaryOffset, int filterBinaryLength) {
|
||||
return filter(new BytesArray(filterBinary, filterBinaryOffset, filterBinaryLength));
|
||||
public SearchSourceBuilder postFilter(byte[] postFilterBinary, int postFilterBinaryOffset, int postFilterBinaryLength) {
|
||||
return postFilter(new BytesArray(postFilterBinary, postFilterBinaryOffset, postFilterBinaryLength));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a filter on the query executed that only applies to the search query
|
||||
* (and not facets for example).
|
||||
*/
|
||||
public SearchSourceBuilder filter(BytesReference filterBinary) {
|
||||
this.filterBinary = filterBinary;
|
||||
public SearchSourceBuilder postFilter(BytesReference postFilterBinary) {
|
||||
this.filterBinary = postFilterBinary;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new search source builder with a query from a builder.
|
||||
*/
|
||||
public SearchSourceBuilder filter(XContentBuilder filter) {
|
||||
return filter(filter.bytes());
|
||||
public SearchSourceBuilder postFilter(XContentBuilder postFilter) {
|
||||
return postFilter(postFilter.bytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new search source builder with a query from a map.
|
||||
*/
|
||||
public SearchSourceBuilder filter(Map filter) {
|
||||
public SearchSourceBuilder postFilter(Map postFilter) {
|
||||
try {
|
||||
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
|
||||
builder.map(filter);
|
||||
return filter(builder);
|
||||
builder.map(postFilter);
|
||||
return postFilter(builder);
|
||||
} catch (IOException e) {
|
||||
throw new ElasticSearchGenerationException("Failed to generate [" + filter + "]", e);
|
||||
throw new ElasticSearchGenerationException("Failed to generate [" + postFilter + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -714,9 +714,9 @@ public class SearchSourceBuilder implements ToXContent {
|
|||
}
|
||||
}
|
||||
|
||||
if (filterBuilder != null) {
|
||||
builder.field("filter");
|
||||
filterBuilder.toXContent(builder, params);
|
||||
if (postFilterBuilder != null) {
|
||||
builder.field("post_filter");
|
||||
postFilterBuilder.toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (filterBinary != null) {
|
||||
|
|
|
@ -58,7 +58,7 @@ public class MatchedQueriesFetchSubPhase implements FetchSubPhase {
|
|||
@Override
|
||||
public boolean hitExecutionNeeded(SearchContext context) {
|
||||
return !context.parsedQuery().namedFilters().isEmpty()
|
||||
|| (context.parsedFilter() !=null && !context.parsedFilter().namedFilters().isEmpty());
|
||||
|| (context.parsedPostFilter() !=null && !context.parsedPostFilter().namedFilters().isEmpty());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -67,8 +67,8 @@ public class MatchedQueriesFetchSubPhase implements FetchSubPhase {
|
|||
|
||||
addMatchedQueries(hitContext, context.parsedQuery().namedFilters(), matchedQueries);
|
||||
|
||||
if (context.parsedFilter() != null) {
|
||||
addMatchedQueries(hitContext, context.parsedFilter().namedFilters(), matchedQueries);
|
||||
if (context.parsedPostFilter() != null) {
|
||||
addMatchedQueries(hitContext, context.parsedPostFilter().namedFilters(), matchedQueries);
|
||||
}
|
||||
|
||||
hitContext.hit().matchedQueries(matchedQueries.toArray(new String[matchedQueries.size()]));
|
||||
|
|
|
@ -145,11 +145,11 @@ public class ContextIndexSearcher extends IndexSearcher {
|
|||
// TODO should we create a cache of segment->docIdSets so we won't create one each time?
|
||||
collector = this.mainDocIdSetCollector = new DocIdSetCollector(searchContext.docSetCache(), collector);
|
||||
}
|
||||
if (searchContext.parsedFilter() != null) {
|
||||
if (searchContext.parsedPostFilter() != null) {
|
||||
// this will only get applied to the actual search collector and not
|
||||
// to any scoped collectors, also, it will only be applied to the main collector
|
||||
// since that is where the filter should only work
|
||||
collector = new FilteredCollector(collector, searchContext.parsedFilter().filter());
|
||||
collector = new FilteredCollector(collector, searchContext.parsedPostFilter().filter());
|
||||
}
|
||||
if (queryCollectors != null && !queryCollectors.isEmpty()) {
|
||||
collector = new MultiCollector(collector, queryCollectors.toArray(new Collector[queryCollectors.size()]));
|
||||
|
|
|
@ -137,7 +137,7 @@ public class DefaultSearchContext extends SearchContext {
|
|||
|
||||
private Query query;
|
||||
|
||||
private ParsedFilter filter;
|
||||
private ParsedFilter postFilter;
|
||||
|
||||
private Filter aliasFilter;
|
||||
|
||||
|
@ -471,13 +471,13 @@ public class DefaultSearchContext extends SearchContext {
|
|||
return this.trackScores;
|
||||
}
|
||||
|
||||
public SearchContext parsedFilter(ParsedFilter filter) {
|
||||
this.filter = filter;
|
||||
public SearchContext parsedPostFilter(ParsedFilter postFilter) {
|
||||
this.postFilter = postFilter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParsedFilter parsedFilter() {
|
||||
return this.filter;
|
||||
public ParsedFilter parsedPostFilter() {
|
||||
return this.postFilter;
|
||||
}
|
||||
|
||||
public Filter aliasFilter() {
|
||||
|
|
|
@ -199,9 +199,9 @@ public abstract class SearchContext implements Releasable {
|
|||
|
||||
public abstract boolean trackScores();
|
||||
|
||||
public abstract SearchContext parsedFilter(ParsedFilter filter);
|
||||
public abstract SearchContext parsedPostFilter(ParsedFilter postFilter);
|
||||
|
||||
public abstract ParsedFilter parsedFilter();
|
||||
public abstract ParsedFilter parsedPostFilter();
|
||||
|
||||
public abstract Filter aliasFilter();
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ public class FilterBinaryParseElement implements SearchParseElement {
|
|||
try {
|
||||
ParsedFilter filter = context.queryParserService().parseInnerFilter(fSourceParser);
|
||||
if (filter != null) {
|
||||
context.parsedFilter(filter);
|
||||
context.parsedPostFilter(filter);
|
||||
}
|
||||
} finally {
|
||||
fSourceParser.close();
|
||||
|
|
|
@ -27,13 +27,13 @@ import org.elasticsearch.search.internal.SearchContext;
|
|||
/**
|
||||
*
|
||||
*/
|
||||
public class FilterParseElement implements SearchParseElement {
|
||||
public class PostFilterParseElement implements SearchParseElement {
|
||||
|
||||
@Override
|
||||
public void parse(XContentParser parser, SearchContext context) throws Exception {
|
||||
ParsedFilter filter = context.queryParserService().parseInnerFilter(parser);
|
||||
if (filter != null) {
|
||||
context.parsedFilter(filter);
|
||||
ParsedFilter postFilter = context.queryParserService().parseInnerFilter(parser);
|
||||
if (postFilter != null) {
|
||||
context.parsedPostFilter(postFilter);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -66,7 +66,9 @@ public class QueryPhase implements SearchPhase {
|
|||
.put("query", new QueryParseElement())
|
||||
.put("queryBinary", new QueryBinaryParseElement())
|
||||
.put("query_binary", new QueryBinaryParseElement())
|
||||
.put("filter", new FilterParseElement())
|
||||
.put("filter", new PostFilterParseElement()) // For bw comp reason, should be removed in version 1.1
|
||||
.put("post_filter", new PostFilterParseElement())
|
||||
.put("postFilter", new PostFilterParseElement())
|
||||
.put("filterBinary", new FilterBinaryParseElement())
|
||||
.put("filter_binary", new FilterBinaryParseElement())
|
||||
.put("sort", new SortParseElement())
|
||||
|
|
|
@ -353,12 +353,12 @@ class TestSearchContext extends SearchContext {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SearchContext parsedFilter(ParsedFilter filter) {
|
||||
public SearchContext parsedPostFilter(ParsedFilter postFilter) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParsedFilter parsedFilter() {
|
||||
public ParsedFilter parsedPostFilter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -141,11 +141,11 @@ public class CacheTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
// sort to load it to field data and filter to load filter cache
|
||||
client().prepareSearch()
|
||||
.setFilter(FilterBuilders.termFilter("field", "value1"))
|
||||
.setPostFilter(FilterBuilders.termFilter("field", "value1"))
|
||||
.addSort("field", SortOrder.ASC)
|
||||
.execute().actionGet();
|
||||
client().prepareSearch()
|
||||
.setFilter(FilterBuilders.termFilter("field", "value2"))
|
||||
.setPostFilter(FilterBuilders.termFilter("field", "value2"))
|
||||
.addSort("field", SortOrder.ASC)
|
||||
.execute().actionGet();
|
||||
|
||||
|
|
|
@ -671,7 +671,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
searchResponse = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(termFilter("tag", "blue"))
|
||||
.setPostFilter(termFilter("tag", "blue"))
|
||||
.addFacet(termsFacet("facet1").field("tag").size(10))
|
||||
.execute().actionGet();
|
||||
|
||||
|
|
|
@ -630,7 +630,7 @@ public class GeoDistanceTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
SearchResponse result = client().prepareSearch("locations")
|
||||
.setQuery(QueryBuilders.matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoDistanceFilter("pin")
|
||||
.setPostFilter(FilterBuilders.geoDistanceFilter("pin")
|
||||
.geoDistance(GeoDistance.ARC)
|
||||
.lat(lat).lon(lon)
|
||||
.distance("1m"))
|
||||
|
|
|
@ -255,7 +255,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
// Point in polygon
|
||||
SearchResponse result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(3, 3)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(3, 3)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
assertFirstHit(result, hasId("1"));
|
||||
|
@ -263,7 +263,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
// Point in polygon hole
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(4.5, 4.5)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(4.5, 4.5)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 0);
|
||||
|
||||
|
@ -274,7 +274,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
// Point on polygon border
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(10.0, 5.0)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(10.0, 5.0)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
assertFirstHit(result, hasId("1"));
|
||||
|
@ -282,7 +282,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
// Point on hole border
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(5.0, 2.0)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(5.0, 2.0)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
assertFirstHit(result, hasId("1"));
|
||||
|
@ -291,14 +291,14 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
// Point not in polygon
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoDisjointFilter("area", ShapeBuilder.newPoint(3, 3)))
|
||||
.setPostFilter(FilterBuilders.geoDisjointFilter("area", ShapeBuilder.newPoint(3, 3)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 0);
|
||||
|
||||
// Point in polygon hole
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoDisjointFilter("area", ShapeBuilder.newPoint(4.5, 4.5)))
|
||||
.setPostFilter(FilterBuilders.geoDisjointFilter("area", ShapeBuilder.newPoint(4.5, 4.5)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
assertFirstHit(result, hasId("1"));
|
||||
|
@ -319,7 +319,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
// re-check point on polygon hole
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(4.5, 4.5)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(4.5, 4.5)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
assertFirstHit(result, hasId("2"));
|
||||
|
@ -339,7 +339,7 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoWithinFilter("area", builder))
|
||||
.setPostFilter(FilterBuilders.geoWithinFilter("area", builder))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 2);
|
||||
}
|
||||
|
@ -365,25 +365,25 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(174, -4)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(174, -4)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(-174, -4)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(-174, -4)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(180, -4)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(180, -4)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 0);
|
||||
|
||||
result = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(180, -6)))
|
||||
.setPostFilter(FilterBuilders.geoIntersectionFilter("area", ShapeBuilder.newPoint(180, -6)))
|
||||
.execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
}
|
||||
|
@ -497,15 +497,15 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
client().admin().indices().prepareRefresh("locations").execute().actionGet();
|
||||
|
||||
// Result of this geohash search should contain the geohash only
|
||||
SearchResponse results1 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setFilter("{\"geohash_cell\": {\"pin\": \"" + geohash + "\", \"neighbors\": false}}").execute().actionGet();
|
||||
SearchResponse results1 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter("{\"geohash_cell\": {\"pin\": \"" + geohash + "\", \"neighbors\": false}}").execute().actionGet();
|
||||
assertHitCount(results1, 1);
|
||||
|
||||
// test the same, just with the builder
|
||||
results1 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setFilter(geoHashCellFilter("pin", geohash, false)).execute().actionGet();
|
||||
results1 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(geoHashCellFilter("pin", geohash, false)).execute().actionGet();
|
||||
assertHitCount(results1, 1);
|
||||
|
||||
// Result of the parent query should contain the parent it self, its neighbors, the child and all its neighbors
|
||||
SearchResponse results2 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setFilter("{\"geohash_cell\": {\"pin\": \"" + geohash.substring(0, geohash.length() - 1) + "\", \"neighbors\": true}}").execute().actionGet();
|
||||
SearchResponse results2 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter("{\"geohash_cell\": {\"pin\": \"" + geohash.substring(0, geohash.length() - 1) + "\", \"neighbors\": true}}").execute().actionGet();
|
||||
assertHitCount(results2, 2 + neighbors.size() + parentNeighbors.size());
|
||||
|
||||
// Testing point formats and precision
|
||||
|
@ -514,17 +514,17 @@ public class GeoFilterTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
logger.info("Testing lat/lon format");
|
||||
String pointTest1 = "{\"geohash_cell\": {\"pin\": {\"lat\": " + point.lat() + ",\"lon\": " + point.lon() + "},\"precision\": " + precision + ",\"neighbors\": true}}";
|
||||
SearchResponse results3 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setFilter(pointTest1).execute().actionGet();
|
||||
SearchResponse results3 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(pointTest1).execute().actionGet();
|
||||
assertHitCount(results3, neighbors.size() + 1);
|
||||
|
||||
logger.info("Testing String format");
|
||||
String pointTest2 = "{\"geohash_cell\": {\"pin\": \"" + point.lat() + "," + point.lon() + "\",\"precision\": " + precision + ",\"neighbors\": true}}";
|
||||
SearchResponse results4 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setFilter(pointTest2).execute().actionGet();
|
||||
SearchResponse results4 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(pointTest2).execute().actionGet();
|
||||
assertHitCount(results4, neighbors.size() + 1);
|
||||
|
||||
logger.info("Testing Array format");
|
||||
String pointTest3 = "{\"geohash_cell\": {\"pin\": [" + point.lon() + "," + point.lat() + "],\"precision\": " + precision + ",\"neighbors\": true}}";
|
||||
SearchResponse results5 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setFilter(pointTest3).execute().actionGet();
|
||||
SearchResponse results5 = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(pointTest3).execute().actionGet();
|
||||
assertHitCount(results5, neighbors.size() + 1);
|
||||
}
|
||||
|
||||
|
|
|
@ -251,7 +251,7 @@ public class GeoShapeIntegrationTests extends ElasticsearchIntegrationTest {
|
|||
+ "\"shape_field_name\": \"location2\""
|
||||
+ "}}}}";
|
||||
|
||||
SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setFilter(filter).execute().actionGet();
|
||||
SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).execute().actionGet();
|
||||
assertHitCount(result, 1);
|
||||
}
|
||||
|
||||
|
|
|
@ -1337,7 +1337,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
|
|||
.setSource("field4", "a quick fast blue car").get();
|
||||
refresh();
|
||||
|
||||
source = searchSource().filter(FilterBuilders.typeFilter("type2")).query(matchPhrasePrefixQuery("field3", "fast bro")).from(0).size(60).explain(true)
|
||||
source = searchSource().postFilter(FilterBuilders.typeFilter("type2")).query(matchPhrasePrefixQuery("field3", "fast bro")).from(0).size(60).explain(true)
|
||||
.highlight(highlight().field("field3").order("score").preTags("<x>").postTags("</x>"));
|
||||
|
||||
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet();
|
||||
|
@ -1345,7 +1345,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
|
|||
assertHighlight(searchResponse, 0, "field3", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"));
|
||||
|
||||
logger.info("--> highlighting and searching on field4");
|
||||
source = searchSource().filter(FilterBuilders.typeFilter("type2")).query(matchPhrasePrefixQuery("field4", "the fast bro")).from(0).size(60).explain(true)
|
||||
source = searchSource().postFilter(FilterBuilders.typeFilter("type2")).query(matchPhrasePrefixQuery("field4", "the fast bro")).from(0).size(60).explain(true)
|
||||
.highlight(highlight().field("field4").order("score").preTags("<x>").postTags("</x>"));
|
||||
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet();
|
||||
|
||||
|
@ -1353,7 +1353,7 @@ public class HighlighterSearchTests extends ElasticsearchIntegrationTest {
|
|||
assertHighlight(searchResponse, 1, "field4", 0, 1, equalTo("<x>The quick brown</x> fox jumps over the lazy dog"));
|
||||
|
||||
logger.info("--> highlighting and searching on field4");
|
||||
source = searchSource().filter(FilterBuilders.typeFilter("type2")).query(matchPhrasePrefixQuery("field4", "a fast quick blue ca")).from(0).size(60).explain(true)
|
||||
source = searchSource().postFilter(FilterBuilders.typeFilter("type2")).query(matchPhrasePrefixQuery("field4", "a fast quick blue ca")).from(0).size(60).explain(true)
|
||||
.highlight(highlight().field("field4").order("score").preTags("<x>").postTags("</x>"));
|
||||
searchResponse = client().search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH)).actionGet();
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
SearchResponse searchResponse = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(orFilter(
|
||||
.setPostFilter(orFilter(
|
||||
termFilter("name", "test").filterName("name"),
|
||||
termFilter("title", "title1").filterName("title"))).get();
|
||||
assertHitCount(searchResponse, 3l);
|
||||
|
@ -107,7 +107,7 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
searchResponse = client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(queryFilter(boolQuery()
|
||||
.setPostFilter(queryFilter(boolQuery()
|
||||
.should(termQuery("name", "test").queryName("name"))
|
||||
.should(termQuery("title", "title1").queryName("title")))).get();
|
||||
|
||||
|
@ -138,7 +138,7 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
SearchResponse searchResponse = client().prepareSearch()
|
||||
.setQuery(filteredQuery(matchAllQuery(), termsFilter("title", "title1", "title2", "title3").filterName("title")))
|
||||
.setFilter(termFilter("name", "test").filterName("name")).get();
|
||||
.setPostFilter(termFilter("name", "test").filterName("name")).get();
|
||||
assertHitCount(searchResponse, 3l);
|
||||
for (SearchHit hit : searchResponse.getHits()) {
|
||||
if (hit.id().equals("1") || hit.id().equals("2") || hit.id().equals("3")) {
|
||||
|
@ -152,7 +152,7 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
searchResponse = client().prepareSearch()
|
||||
.setQuery(termsQuery("title", "title1", "title2", "title3").queryName("title"))
|
||||
.setFilter(queryFilter(matchQuery("name", "test").queryName("name"))).get();
|
||||
.setPostFilter(queryFilter(matchQuery("name", "test").queryName("name"))).get();
|
||||
assertHitCount(searchResponse, 3l);
|
||||
for (SearchHit hit : searchResponse.getHits()) {
|
||||
if (hit.id().equals("1") || hit.id().equals("2") || hit.id().equals("3")) {
|
||||
|
|
|
@ -71,7 +71,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
assertHitCount(
|
||||
client().prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.setFilter(
|
||||
.setPostFilter(
|
||||
andFilter(
|
||||
queryFilter(matchAllQuery()),
|
||||
notFilter(andFilter(queryFilter(termQuery("field1", "value1")),
|
||||
|
@ -87,7 +87,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
queryFilter(termQuery("field1", "value2")))))).get(),
|
||||
3l);
|
||||
assertHitCount(
|
||||
client().prepareSearch().setQuery(matchAllQuery()).setFilter(notFilter(termFilter("field1", "value3"))).get(),
|
||||
client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(notFilter(termFilter("field1", "value3"))).get(),
|
||||
2l);
|
||||
}
|
||||
|
||||
|
@ -551,7 +551,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
assertHitCount(client().prepareSearch().setQuery(bool).get(), 1l);
|
||||
|
||||
WrapperFilterBuilder wrapperFilter = new WrapperFilterBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");
|
||||
assertHitCount(client().prepareSearch().setFilter(wrapperFilter).get(), 1l);
|
||||
assertHitCount(client().prepareSearch().setPostFilter(wrapperFilter).get(), 1l);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1026,7 +1026,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get();
|
||||
refresh();
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(idsFilter("type1").ids("1")).get();
|
||||
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(idsFilter("type1").ids("1")).get();
|
||||
assertHitCount(searchResponse, 1l);
|
||||
assertThat(searchResponse.getHits().hits().length, equalTo(1));
|
||||
|
||||
|
@ -1034,11 +1034,11 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
assertHitCount(searchResponse, 2l);
|
||||
assertThat(searchResponse.getHits().hits().length, equalTo(2));
|
||||
|
||||
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(idsFilter().ids("1")).get();
|
||||
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(idsFilter().ids("1")).get();
|
||||
assertHitCount(searchResponse, 1l);
|
||||
assertThat(searchResponse.getHits().hits().length, equalTo(1));
|
||||
|
||||
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(idsFilter().ids("1", "2")).get();
|
||||
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(idsFilter().ids("1", "2")).get();
|
||||
assertHitCount(searchResponse, 2l);
|
||||
assertThat(searchResponse.getHits().hits().length, equalTo(2));
|
||||
|
||||
|
@ -1220,7 +1220,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
client().prepareIndex("test", "type1", "4").setSource("field1", "test2", "num_long", 4).get();
|
||||
refresh();
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("test").setFilter(
|
||||
SearchResponse searchResponse = client().prepareSearch("test").setPostFilter(
|
||||
boolFilter()
|
||||
.should(rangeFilter("num_long", 1, 2))
|
||||
.should(rangeFilter("num_long", 3, 4))
|
||||
|
@ -1228,7 +1228,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
assertHitCount(searchResponse, 4l);
|
||||
|
||||
// This made 2826 fail! (only with bit based filters)
|
||||
searchResponse = client().prepareSearch("test").setFilter(
|
||||
searchResponse = client().prepareSearch("test").setPostFilter(
|
||||
boolFilter()
|
||||
.should(rangeFilter("num_long", 1, 2))
|
||||
.should(rangeFilter("num_long", 3, 4))
|
||||
|
@ -1236,7 +1236,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
assertHitCount(searchResponse, 4l);
|
||||
|
||||
// This made #2979 fail!
|
||||
searchResponse = client().prepareSearch("test").setFilter(
|
||||
searchResponse = client().prepareSearch("test").setPostFilter(
|
||||
boolFilter()
|
||||
.must(termFilter("field1", "test1"))
|
||||
.should(rangeFilter("num_long", 1, 2))
|
||||
|
@ -1248,7 +1248,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
@Test
|
||||
public void testEmptyTopLevelFilter() {
|
||||
client().prepareIndex("test", "type", "1").setSource("field", "value").setRefresh(true).get();
|
||||
SearchResponse searchResponse = client().prepareSearch().setFilter("{}").get();
|
||||
SearchResponse searchResponse = client().prepareSearch().setPostFilter("{}").get();
|
||||
assertHitCount(searchResponse, 1l);
|
||||
}
|
||||
|
||||
|
@ -1597,25 +1597,25 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
refresh();
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("index1", "index2", "index3")
|
||||
.setFilter(indicesFilter(termFilter("text", "value1"), "index1")
|
||||
.setPostFilter(indicesFilter(termFilter("text", "value1"), "index1")
|
||||
.noMatchFilter(termFilter("text", "value2"))).get();
|
||||
assertHitCount(searchResponse, 2l);
|
||||
assertSearchHits(searchResponse, "1", "2");
|
||||
|
||||
//default no match filter is "all"
|
||||
searchResponse = client().prepareSearch("index1", "index2", "index3")
|
||||
.setFilter(indicesFilter(termFilter("text", "value1"), "index1")).get();
|
||||
.setPostFilter(indicesFilter(termFilter("text", "value1"), "index1")).get();
|
||||
assertHitCount(searchResponse, 3l);
|
||||
assertSearchHits(searchResponse, "1", "2", "3");
|
||||
|
||||
searchResponse = client().prepareSearch("index1", "index2", "index3")
|
||||
.setFilter(indicesFilter(termFilter("text", "value1"), "index1")
|
||||
.setPostFilter(indicesFilter(termFilter("text", "value1"), "index1")
|
||||
.noMatchFilter("all")).get();
|
||||
assertHitCount(searchResponse, 3l);
|
||||
assertSearchHits(searchResponse, "1", "2", "3");
|
||||
|
||||
searchResponse = client().prepareSearch("index1", "index2", "index3")
|
||||
.setFilter(indicesFilter(termFilter("text", "value1"), "index1")
|
||||
.setPostFilter(indicesFilter(termFilter("text", "value1"), "index1")
|
||||
.noMatchFilter("none")).get();
|
||||
assertHitCount(searchResponse, 1l);
|
||||
assertFirstHit(searchResponse, hasId("1"));
|
||||
|
@ -1670,7 +1670,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
//has_child fails if executed on "simple" index
|
||||
try {
|
||||
client().prepareSearch("simple")
|
||||
.setFilter(hasChildFilter("child", termFilter("text", "value1"))).get();
|
||||
.setPostFilter(hasChildFilter("child", termFilter("text", "value1"))).get();
|
||||
fail("Should have failed as has_child query can only be executed against parent-child types");
|
||||
} catch (SearchPhaseExecutionException e) {
|
||||
assertThat(e.shardFailures().length, greaterThan(0));
|
||||
|
@ -1680,7 +1680,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|||
}
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("related", "simple")
|
||||
.setFilter(indicesFilter(hasChildFilter("child", termFilter("text", "value2")), "related")
|
||||
.setPostFilter(indicesFilter(hasChildFilter("child", termFilter("text", "value2")), "related")
|
||||
.noMatchFilter(termFilter("text", "value1"))).get();
|
||||
assertHitCount(searchResponse, 2l);
|
||||
assertSearchHits(searchResponse, "1", "2");
|
||||
|
|
|
@ -169,7 +169,7 @@ public class SimpleSortTests extends ElasticsearchIntegrationTest {
|
|||
if (!sparseBytes.isEmpty()) {
|
||||
int size = between(1, sparseBytes.size());
|
||||
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery())
|
||||
.setFilter(FilterBuilders.existsFilter("sparse_bytes")).setSize(size).addSort("sparse_bytes", SortOrder.ASC).execute()
|
||||
.setPostFilter(FilterBuilders.existsFilter("sparse_bytes")).setSize(size).addSort("sparse_bytes", SortOrder.ASC).execute()
|
||||
.actionGet();
|
||||
assertNoFailures(searchResponse);
|
||||
assertThat(searchResponse.getHits().getTotalHits(), equalTo((long) sparseBytes.size()));
|
||||
|
|
|
@ -69,16 +69,16 @@ public class RefreshStressTest1 {
|
|||
// Thread.sleep(100);
|
||||
|
||||
System.out.println("searching " + loop);
|
||||
SearchResponse result = client.prepareSearch(indexName).setFilter(FilterBuilders.termFilter("name", name)).execute().actionGet();
|
||||
SearchResponse result = client.prepareSearch(indexName).setPostFilter(FilterBuilders.termFilter("name", name)).execute().actionGet();
|
||||
if (result.getHits().hits().length != 1) {
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
System.out.println("retry " + loop + ", " + i + ", previous total hits: " + result.getHits().getTotalHits());
|
||||
client.admin().indices().prepareRefresh(indexName).execute().actionGet();
|
||||
Thread.sleep(100);
|
||||
result = client.prepareSearch(indexName).setFilter(FilterBuilders.termFilter("name", name)).execute().actionGet();
|
||||
result = client.prepareSearch(indexName).setPostFilter(FilterBuilders.termFilter("name", name)).execute().actionGet();
|
||||
if (result.getHits().hits().length == 1) {
|
||||
client.admin().indices().prepareRefresh(indexName).execute().actionGet();
|
||||
result = client.prepareSearch(indexName).setFilter(FilterBuilders.termFilter("name", name)).execute().actionGet();
|
||||
result = client.prepareSearch(indexName).setPostFilter(FilterBuilders.termFilter("name", name)).execute().actionGet();
|
||||
throw new RuntimeException("Record found after " + (i * 100) + " ms, second go: " + result.getHits().hits().length);
|
||||
} else if (i == 100) {
|
||||
if (client.prepareGet(indexName, typeName, id).execute().actionGet().isExists())
|
||||
|
|
Loading…
Reference in New Issue