From d7abb12100adb3c77c6eb0d61e691d9fbba5bcba Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 9 Apr 2015 18:33:27 +0200 Subject: [PATCH] Replace deprecated filters with equivalent queries. In Lucene 5.1 lots of filters got deprecated in favour of equivalent queries. Additionally, random-access to filters is now replaced with approximations on scorers. This commit - replaces the deprecated NumericRangeFilter, PrefixFilter, TermFilter and TermsFilter with NumericRangeQuery, PrefixQuery, TermQuery and TermsQuery, wrapped in a QueryWrapperFilter - replaces XBooleanFilter, AndFilter and OrFilter with a BooleanQuery in a QueryWrapperFilter - removes DocIdSets.isBroken: the new two-phase iteration API will now help execute slow filters efficiently - replaces FilterCachingPolicy with QueryCachingPolicy Close #8960 --- dev-tools/forbidden/all-signatures.txt | 5 + docs/reference/migration/migrate_2_0.asciidoc | 9 + .../query-dsl/filters/and-filter.asciidoc | 2 + .../query-dsl/filters/or-filter.asciidoc | 2 + .../query-dsl/filters/terms-filter.asciidoc | 65 -- .../test/indices.validate_query/10_basic.yaml | 2 +- .../vectorhighlight/CustomFieldQuery.java | 40 +- .../query/TransportValidateQueryAction.java | 3 +- .../common/lucene/docset/AndDocIdSet.java | 19 +- .../common/lucene/docset/DocIdSets.java | 28 - .../common/lucene/docset/NotDocIdSet.java | 181 ------ .../common/lucene/docset/OrDocIdSet.java | 263 -------- .../common/lucene/search/AndFilter.java | 99 --- .../lucene/search/MatchAllDocsFilter.java | 66 -- .../lucene/search/MatchNoDocsFilter.java | 64 -- .../common/lucene/search/NotFilter.java | 78 --- .../common/lucene/search/OrFilter.java | 108 ---- .../common/lucene/search/Queries.java | 53 +- .../common/lucene/search/RegexpFilter.java | 110 ---- .../common/lucene/search/XBooleanFilter.java | 377 ----------- .../lucene/search/XDocIdSetIterator.java | 40 -- .../index/aliases/IndexAliasesService.java | 16 +- .../cache/filter/AutoFilterCachingPolicy.java | 103 --- .../index/cache/filter/FilterCache.java | 4 +- .../index/cache/filter/FilterCacheModule.java | 12 +- .../cache/filter/none/NoneFilterCache.java | 4 +- .../filter/weighted/WeightedFilterCache.java | 10 +- .../index/mapper/MapperService.java | 51 +- .../mapper/core/AbstractFieldMapper.java | 22 +- .../index/mapper/core/BooleanFieldMapper.java | 5 +- .../index/mapper/core/ByteFieldMapper.java | 14 +- .../index/mapper/core/DateFieldMapper.java | 14 +- .../index/mapper/core/DoubleFieldMapper.java | 16 +- .../index/mapper/core/FloatFieldMapper.java | 14 +- .../index/mapper/core/IntegerFieldMapper.java | 14 +- .../index/mapper/core/LongFieldMapper.java | 14 +- .../index/mapper/core/ShortFieldMapper.java | 18 +- .../index/mapper/internal/IdFieldMapper.java | 36 +- .../mapper/internal/ParentFieldMapper.java | 14 +- .../mapper/internal/TypeFieldMapper.java | 9 +- .../index/mapper/ip/IpFieldMapper.java | 10 +- .../index/mapper/object/ObjectMapper.java | 5 +- .../percolator/PercolatorQueriesRegistry.java | 7 +- .../index/query/AndFilterBuilder.java | 4 +- .../index/query/AndFilterParser.java | 14 +- .../index/query/BoolFilterParser.java | 26 +- .../index/query/ConstantScoreQueryParser.java | 4 +- .../index/query/ExistsFilterParser.java | 18 +- .../index/query/FQueryFilterParser.java | 4 +- .../index/query/FilterBuilders.java | 8 + .../index/query/FilteredQueryParser.java | 87 +-- .../query/GeoBoundingBoxFilterParser.java | 4 +- .../index/query/GeoDistanceFilterParser.java | 4 +- .../query/GeoDistanceRangeFilterParser.java | 4 +- .../index/query/GeoPolygonFilterParser.java | 4 +- .../index/query/GeoShapeFilterParser.java | 11 +- .../index/query/GeoShapeQueryParser.java | 4 +- .../index/query/GeohashCellFilter.java | 4 +- .../index/query/HasParentQueryParser.java | 11 +- .../index/query/IdsFilterParser.java | 7 +- .../index/query/IdsQueryParser.java | 8 +- .../index/query/IndexQueryParserService.java | 8 +- .../index/query/IndicesFilterParser.java | 6 +- .../index/query/LimitFilterParser.java | 2 +- .../index/query/MatchAllFilterParser.java | 2 +- .../index/query/MissingFilterParser.java | 25 +- .../index/query/MoreLikeThisQueryParser.java | 7 +- .../index/query/NotFilterParser.java | 5 +- .../index/query/OrFilterBuilder.java | 4 +- .../index/query/OrFilterParser.java | 14 +- .../index/query/PrefixFilterParser.java | 9 +- .../index/query/QueryParseContext.java | 10 +- .../index/query/RangeFilterParser.java | 9 +- .../index/query/RegexpFilterParser.java | 9 +- .../index/query/ScriptFilterParser.java | 7 +- .../index/query/TermFilterParser.java | 9 +- .../index/query/TermsFilterBuilder.java | 2 + .../index/query/TermsFilterParser.java | 148 +---- .../index/query/TypeFilterParser.java | 5 +- .../FunctionScoreQueryParser.java | 6 +- .../index/query/support/QueryParsers.java | 1 - .../child/ChildrenConstantScoreQuery.java | 7 +- .../index/search/child/ChildrenQuery.java | 5 +- .../child/ParentConstantScoreQuery.java | 6 +- .../index/search/child/ParentIdsFilter.java | 30 +- .../index/search/child/ParentQuery.java | 4 +- .../geo/IndexedGeoBoundingBoxFilter.java | 12 +- .../search/nested/NonNestedDocsFilter.java | 17 +- .../percolator/PercolatorService.java | 7 +- .../search/aggregations/AggregationPhase.java | 3 +- .../bucket/filter/FilterParser.java | 4 +- .../bucket/filters/FiltersParser.java | 6 +- .../bucket/nested/NestedAggregator.java | 11 +- .../fetch/innerhits/InnerHitsContext.java | 26 +- .../MatchedQueriesFetchSubPhase.java | 1 + .../search/internal/DefaultSearchContext.java | 26 +- .../common/lucene/docset/DocIdSetsTests.java | 168 ----- .../lucene/index/FreqTermsEnumTests.java | 31 +- .../search/MatchAllDocsFilterTests.java | 2 +- .../lucene/search/TermsFilterTests.java | 118 ---- .../search/XBooleanFilterLuceneTests.java | 424 ------------- .../lucene/search/XBooleanFilterTests.java | 591 ------------------ .../index/TermsFilterIntegrationTests.java | 71 --- .../aliases/IndexAliasesServiceTests.java | 12 +- .../cache/bitset/BitSetFilterCacheTest.java | 5 +- .../AbstractStringFieldDataTests.java | 9 +- .../mapper/date/SimpleDateMappingTests.java | 17 +- .../string/SimpleStringMappingTests.java | 11 +- .../query/SimpleIndexQueryParserTests.java | 402 +++++------- .../index/query/TemplateQueryParserTest.java | 5 +- .../query/fquery-with-empty-bool-query.json | 6 +- .../ChildrenConstantScoreQueryTests.java | 26 +- .../search/child/ChildrenQueryTests.java | 43 +- .../child/ParentConstantScoreQueryTests.java | 24 +- .../index/search/child/ParentQueryTests.java | 29 +- .../AbstractNumberNestedSortingTests.java | 11 +- .../nested/DoubleNestedSortingTests.java | 4 +- .../nested/FloatNestedSortingTests.java | 4 +- .../search/nested/NestedSortingTests.java | 20 +- .../indices/stats/IndexStatsTests.java | 17 +- .../nested/SimpleNestedTests.java | 2 +- .../bucket/nested/NestedAggregatorTest.java | 22 +- .../child/SimpleChildQuerySearchTests.java | 10 +- .../innerhits/NestedChildrenFilterTest.java | 13 +- .../functionscore/FunctionScoreTests.java | 2 +- .../highlight/HighlighterSearchTests.java | 41 +- .../scriptfilter/ScriptFilterSearchTests.java | 8 +- .../test/InternalTestCluster.java | 16 +- .../validate/SimpleValidateQueryTests.java | 155 +---- 129 files changed, 923 insertions(+), 4101 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/common/lucene/docset/NotDocIdSet.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/docset/OrDocIdSet.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/MatchNoDocsFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/RegexpFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java delete mode 100644 src/main/java/org/elasticsearch/common/lucene/search/XDocIdSetIterator.java delete mode 100644 src/main/java/org/elasticsearch/index/cache/filter/AutoFilterCachingPolicy.java delete mode 100644 src/test/java/org/elasticsearch/common/lucene/docset/DocIdSetsTests.java delete mode 100644 src/test/java/org/elasticsearch/common/lucene/search/TermsFilterTests.java delete mode 100644 src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterLuceneTests.java delete mode 100644 src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterTests.java delete mode 100644 src/test/java/org/elasticsearch/index/TermsFilterIntegrationTests.java diff --git a/dev-tools/forbidden/all-signatures.txt b/dev-tools/forbidden/all-signatures.txt index 5e893e537f3..8c82fee5239 100644 --- a/dev-tools/forbidden/all-signatures.txt +++ b/dev-tools/forbidden/all-signatures.txt @@ -33,6 +33,11 @@ java.nio.file.Path#toFile() @defaultMessage Don't use deprecated lucene apis org.apache.lucene.index.DocsEnum org.apache.lucene.index.DocsAndPositionsEnum +org.apache.lucene.queries.TermFilter +org.apache.lucene.queries.TermsFilter +org.apache.lucene.search.TermRangeFilter +org.apache.lucene.search.NumericRangeFilter +org.apache.lucene.search.PrefixFilter java.nio.file.Paths @ Use PathUtils.get instead. java.nio.file.FileSystems#getDefault() @ use PathUtils.getDefault instead. diff --git a/docs/reference/migration/migrate_2_0.asciidoc b/docs/reference/migration/migrate_2_0.asciidoc index c37e4222ed6..b20e1960fee 100644 --- a/docs/reference/migration/migrate_2_0.asciidoc +++ b/docs/reference/migration/migrate_2_0.asciidoc @@ -374,9 +374,18 @@ http.cors.allow-origin: /https?:\/\/localhost(:[0-9]+)?/ The cluster state api doesn't return the `routing_nodes` section anymore when `routing_table` is requested. The newly introduced `routing_nodes` flag can be used separately to control whether `routing_nodes` should be returned. + === Query DSL The `fuzzy_like_this` and `fuzzy_like_this_field` queries have been removed. The `limit` filter is deprecated and becomes a no-op. You can achieve similar behaviour using the <> parameter. + +`or` and `and` on the one hand and `bool` on the other hand used to have +different performance characteristics depending on the wrapped filters. This is +fixed now, as a consequence the `or` and `and` filters are now deprecated in +favour or `bool`. + +The `execution` option of the `terms` filter is now deprecated and ignored if +provided. diff --git a/docs/reference/query-dsl/filters/and-filter.asciidoc b/docs/reference/query-dsl/filters/and-filter.asciidoc index 2915f94b65c..043a62e68bf 100644 --- a/docs/reference/query-dsl/filters/and-filter.asciidoc +++ b/docs/reference/query-dsl/filters/and-filter.asciidoc @@ -1,6 +1,8 @@ [[query-dsl-and-filter]] === And Filter +deprecated[2.0.0, Use the `bool` filter instead] + A filter that matches documents using the `AND` boolean operator on other filters. Can be placed within queries that accept a filter. diff --git a/docs/reference/query-dsl/filters/or-filter.asciidoc b/docs/reference/query-dsl/filters/or-filter.asciidoc index 771233a26bc..c7c845c33ee 100644 --- a/docs/reference/query-dsl/filters/or-filter.asciidoc +++ b/docs/reference/query-dsl/filters/or-filter.asciidoc @@ -1,6 +1,8 @@ [[query-dsl-or-filter]] === Or Filter +deprecated[2.0.0, Use the `bool` filter instead] + A filter that matches documents using the `OR` boolean operator on other filters. Can be placed within queries that accept a filter. diff --git a/docs/reference/query-dsl/filters/terms-filter.asciidoc b/docs/reference/query-dsl/filters/terms-filter.asciidoc index 3e8b2b3e767..19e9358a4dd 100644 --- a/docs/reference/query-dsl/filters/terms-filter.asciidoc +++ b/docs/reference/query-dsl/filters/terms-filter.asciidoc @@ -18,71 +18,6 @@ Filters documents that have fields that match any of the provided terms The `terms` filter is also aliased with `in` as the filter name for simpler usage. -[float] -==== Execution Mode - -The way terms filter executes is by iterating over the terms provided -and finding matches docs (loading into a bitset) and caching it. -Sometimes, we want a different execution model that can still be -achieved by building more complex queries in the DSL, but we can support -them in the more compact model that terms filter provides. - -The `execution` option now has the following options : - -[horizontal] -`plain`:: - The default. Works as today. Iterates over all the terms, - building a bit set matching it, and filtering. The total filter is - cached. - -`fielddata`:: - Generates a terms filters that uses the fielddata cache to - compare terms. This execution mode is great to use when filtering - on a field that is already loaded into the fielddata cache from - aggregating, sorting, or index warmers. When filtering on - a large number of terms, this execution can be considerably faster - than the other modes. The total filter is not cached unless - explicitly configured to do so. - -`bool`:: - Generates a term filter (which is cached) for each term, and - wraps those in a bool filter. The bool filter itself is not cached as it - can operate very quickly on the cached term filters. - -`and`:: - Generates a term filter (which is cached) for each term, and - wraps those in an and filter. The and filter itself is not cached. - -`or`:: - Generates a term filter (which is cached) for each term, and - wraps those in an or filter. The or filter itself is not cached. - Generally, the `bool` execution mode should be preferred. - -If you don't want the generated individual term queries to be cached, -you can use: `bool_nocache`, `and_nocache` or `or_nocache` instead, but -be aware that this will affect performance. - -The "total" terms filter caching can still be explicitly controlled -using the `_cache` option. Note the default value for it depends on the -execution value. - -For example: - -[source,js] --------------------------------------------------- -{ - "constant_score" : { - "filter" : { - "terms" : { - "user" : ["kimchy", "elasticsearch"], - "execution" : "bool", - "_cache": true - } - } - } -} --------------------------------------------------- - [float] ==== Caching diff --git a/rest-api-spec/test/indices.validate_query/10_basic.yaml b/rest-api-spec/test/indices.validate_query/10_basic.yaml index 39afa47f25d..2a9ed19221f 100644 --- a/rest-api-spec/test/indices.validate_query/10_basic.yaml +++ b/rest-api-spec/test/indices.validate_query/10_basic.yaml @@ -32,5 +32,5 @@ - is_true: valid - match: {_shards.failed: 0} - match: {explanations.0.index: 'testing'} - - match: {explanations.0.explanation: 'ConstantScore(*:*)'} + - match: {explanations.0.explanation: '*:*'} diff --git a/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java b/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java index ecdb7d6def1..37e1f7a6df1 100644 --- a/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java +++ b/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java @@ -22,25 +22,20 @@ package org.apache.lucene.search.vectorhighlight; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queries.BlendedTermQuery; -import org.apache.lucene.queries.FilterClause; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.MultiPhraseQuery; -import org.apache.lucene.search.MultiTermQueryWrapperFilter; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.spans.SpanTermQuery; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; -import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import java.io.IOException; -import java.lang.reflect.Field; import java.util.Collection; import java.util.List; @@ -48,19 +43,9 @@ import java.util.List; * */ // LUCENE MONITOR +// TODO: remove me! public class CustomFieldQuery extends FieldQuery { - private static Field multiTermQueryWrapperFilterQueryField; - - static { - try { - multiTermQueryWrapperFilterQueryField = MultiTermQueryWrapperFilter.class.getDeclaredField("query"); - multiTermQueryWrapperFilterQueryField.setAccessible(true); - } catch (NoSuchFieldException e) { - // ignore - } - } - public static final ThreadLocal highlightFilters = new ThreadLocal<>(); public CustomFieldQuery(Query query, IndexReader reader, FastVectorHighlighter highlighter) throws IOException { @@ -140,25 +125,8 @@ public class CustomFieldQuery extends FieldQuery { if (highlight == null || highlight.equals(Boolean.FALSE)) { return; } - if (sourceFilter instanceof TermFilter) { - // TermFilter is just a deprecated wrapper over QWF - TermQuery actualQuery = (TermQuery) ((TermFilter) sourceFilter).getQuery(); - flatten(new TermQuery(actualQuery.getTerm()), reader, flatQueries); - } else if (sourceFilter instanceof MultiTermQueryWrapperFilter) { - if (multiTermQueryWrapperFilterQueryField != null) { - try { - flatten((Query) multiTermQueryWrapperFilterQueryField.get(sourceFilter), reader, flatQueries); - } catch (IllegalAccessException e) { - // ignore - } - } - } else if (sourceFilter instanceof XBooleanFilter) { - XBooleanFilter booleanFilter = (XBooleanFilter) sourceFilter; - for (FilterClause clause : booleanFilter.clauses()) { - if (clause.getOccur() == BooleanClause.Occur.MUST || clause.getOccur() == BooleanClause.Occur.SHOULD) { - flatten(clause.getFilter(), reader, flatQueries); - } - } + if (sourceFilter instanceof QueryWrapperFilter) { + flatten(((QueryWrapperFilter) sourceFilter).getQuery(), reader, flatQueries); } } } diff --git a/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java b/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java index dc3a00ce81d..9325ba9f87a 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java @@ -37,7 +37,6 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.routing.GroupShardsIterator; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.search.MatchNoDocsFilter; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.index.IndexService; @@ -219,7 +218,7 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct private String getRewrittenQuery(IndexSearcher searcher, Query query) throws IOException { Query queryRewrite = searcher.rewrite(query); - if (queryRewrite instanceof MatchNoDocsQuery || queryRewrite instanceof MatchNoDocsFilter) { + if (queryRewrite instanceof MatchNoDocsQuery) { return query.toString(); } else { return queryRewrite.toString(); diff --git a/src/main/java/org/elasticsearch/common/lucene/docset/AndDocIdSet.java b/src/main/java/org/elasticsearch/common/lucene/docset/AndDocIdSet.java index c9d9f8b513a..37666416e4b 100644 --- a/src/main/java/org/elasticsearch/common/lucene/docset/AndDocIdSet.java +++ b/src/main/java/org/elasticsearch/common/lucene/docset/AndDocIdSet.java @@ -19,21 +19,18 @@ package org.elasticsearch.common.lucene.docset; -import com.google.common.collect.Iterables; - import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.BitSet; import org.apache.lucene.util.Bits; import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.lucene.search.XDocIdSetIterator; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; /** @@ -93,7 +90,7 @@ public class AndDocIdSet extends DocIdSet { return DocIdSetIterator.empty(); } Bits bit = set.bits(); - if (bit != null && DocIdSets.isBroken(it)) { + if (bit != null && bit instanceof BitSet == false) { bits.add(bit); } else { iterators.add(it); @@ -138,7 +135,7 @@ public class AndDocIdSet extends DocIdSet { } } - static class IteratorBasedIterator extends XDocIdSetIterator { + static class IteratorBasedIterator extends DocIdSetIterator { private int doc = -1; private final DocIdSetIterator lead; private final DocIdSetIterator[] otherIterators; @@ -174,16 +171,6 @@ public class AndDocIdSet extends DocIdSet { this.otherIterators = Arrays.copyOfRange(sortedIterators, 1, sortedIterators.length); } - @Override - public boolean isBroken() { - for (DocIdSetIterator it : Iterables.concat(Collections.singleton(lead), Arrays.asList(otherIterators))) { - if (DocIdSets.isBroken(it)) { - return true; - } - } - return false; - } - @Override public final int docID() { return doc; diff --git a/src/main/java/org/elasticsearch/common/lucene/docset/DocIdSets.java b/src/main/java/org/elasticsearch/common/lucene/docset/DocIdSets.java index bffa699bc31..82b57e4e452 100644 --- a/src/main/java/org/elasticsearch/common/lucene/docset/DocIdSets.java +++ b/src/main/java/org/elasticsearch/common/lucene/docset/DocIdSets.java @@ -22,8 +22,6 @@ package org.elasticsearch.common.lucene.docset; import org.apache.lucene.index.LeafReader; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.search.DocValuesDocIdSet; -import org.apache.lucene.search.FilteredDocIdSetIterator; import org.apache.lucene.util.BitDocIdSet; import org.apache.lucene.util.BitSet; import org.apache.lucene.util.Bits; @@ -33,7 +31,6 @@ import org.apache.lucene.util.SparseFixedBitSet; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.ElasticsearchIllegalStateException; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.lucene.search.XDocIdSetIterator; import java.io.IOException; @@ -55,31 +52,6 @@ public class DocIdSets { return set == null || set == DocIdSet.EMPTY; } - /** - * Check if the given iterator can nextDoc() or advance() in sub-linear time - * of the number of documents. For instance, an iterator that would need to - * iterate one document at a time to check for its value would be considered - * broken. - */ - public static boolean isBroken(DocIdSetIterator iterator) { - while (iterator instanceof FilteredDocIdSetIterator) { - // this iterator is filtered (likely by some bits) - // unwrap in order to check if the underlying iterator is fast - iterator = ((FilteredDocIdSetIterator) iterator).getDelegate(); - } - if (iterator instanceof XDocIdSetIterator) { - return ((XDocIdSetIterator) iterator).isBroken(); - } - if (iterator instanceof MatchDocIdSetIterator) { - return true; - } - // DocValuesDocIdSet produces anonymous slow iterators - if (iterator != null && DocValuesDocIdSet.class.equals(iterator.getClass().getEnclosingClass())) { - return true; - } - return false; - } - /** * Converts to a cacheable {@link DocIdSet} *

diff --git a/src/main/java/org/elasticsearch/common/lucene/docset/NotDocIdSet.java b/src/main/java/org/elasticsearch/common/lucene/docset/NotDocIdSet.java deleted file mode 100644 index 04d556e8290..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/docset/NotDocIdSet.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.docset; - -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.RamUsageEstimator; - -import java.io.IOException; - -/** - * A {@link DocIdSet} that matches the "inverse" of the provided doc id set. - */ -public class NotDocIdSet extends DocIdSet { - - private final DocIdSet set; - private final int maxDoc; - - public NotDocIdSet(DocIdSet set, int maxDoc) { - this.maxDoc = maxDoc; - this.set = set; - } - - @Override - public boolean isCacheable() { - return set.isCacheable(); - } - - @Override - public long ramBytesUsed() { - return RamUsageEstimator.NUM_BYTES_OBJECT_REF + RamUsageEstimator.NUM_BYTES_INT + set.ramBytesUsed(); - } - - @Override - public Bits bits() throws IOException { - Bits bits = set.bits(); - if (bits == null) { - return null; - } - return new NotBits(bits); - } - - @Override - public DocIdSetIterator iterator() throws IOException { - DocIdSetIterator it = set.iterator(); - if (it == null) { - return new AllDocIdSet.Iterator(maxDoc); - } - // TODO: can we optimize for the FixedBitSet case? - // if we have bits, its much faster to just check on the flipped end potentially - // really depends on the nature of the Bits, specifically with FixedBitSet, where - // most of the docs are set? - Bits bits = set.bits(); - if (bits != null) { - return new BitsBasedIterator(bits); - } - return new IteratorBasedIterator(maxDoc, it); - } - - public static class NotBits implements Bits { - - private final Bits bits; - - public NotBits(Bits bits) { - this.bits = bits; - } - - @Override - public boolean get(int index) { - return !bits.get(index); - } - - @Override - public int length() { - return bits.length(); - } - } - - public static class BitsBasedIterator extends MatchDocIdSetIterator { - - private final Bits bits; - - public BitsBasedIterator(Bits bits) { - super(bits.length()); - this.bits = bits; - } - - @Override - protected boolean matchDoc(int doc) { - return !bits.get(doc); - } - - @Override - public long cost() { - return bits.length(); - } - } - - public static class IteratorBasedIterator extends DocIdSetIterator { - private final int max; - private DocIdSetIterator it1; - private int lastReturn = -1; - private int innerDocid = -1; - private final long cost; - - IteratorBasedIterator(int max, DocIdSetIterator it) throws IOException { - this.max = max; - this.it1 = it; - this.cost = it1.cost(); - if ((innerDocid = it1.nextDoc()) == DocIdSetIterator.NO_MORE_DOCS) { - it1 = null; - } - } - - @Override - public int docID() { - return lastReturn; - } - - @Override - public int nextDoc() throws IOException { - return advance(0); - } - - @Override - public int advance(int target) throws IOException { - - if (lastReturn == DocIdSetIterator.NO_MORE_DOCS) { - return DocIdSetIterator.NO_MORE_DOCS; - } - - if (target <= lastReturn) target = lastReturn + 1; - - if (it1 != null && innerDocid < target) { - if ((innerDocid = it1.advance(target)) == DocIdSetIterator.NO_MORE_DOCS) { - it1 = null; - } - } - - while (it1 != null && innerDocid == target) { - target++; - if (target >= max) { - return (lastReturn = DocIdSetIterator.NO_MORE_DOCS); - } - if ((innerDocid = it1.advance(target)) == DocIdSetIterator.NO_MORE_DOCS) { - it1 = null; - } - } - - // ADDED THIS, bug in original code - if (target >= max) { - return (lastReturn = DocIdSetIterator.NO_MORE_DOCS); - } - - return (lastReturn = target); - } - - @Override - public long cost() { - return cost; - } - } -} diff --git a/src/main/java/org/elasticsearch/common/lucene/docset/OrDocIdSet.java b/src/main/java/org/elasticsearch/common/lucene/docset/OrDocIdSet.java deleted file mode 100644 index 8324e444267..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/docset/OrDocIdSet.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.docset; - -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.common.lucene.search.XDocIdSetIterator; - -import java.io.IOException; - -/** - * - */ -public class OrDocIdSet extends DocIdSet { - - private final DocIdSet[] sets; - - public OrDocIdSet(DocIdSet[] sets) { - this.sets = sets; - } - - @Override - public boolean isCacheable() { - for (DocIdSet set : sets) { - if (!set.isCacheable()) { - return false; - } - } - return true; - } - - @Override - public long ramBytesUsed() { - long ramBytesUsed = RamUsageEstimator.NUM_BYTES_OBJECT_REF + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER; - for (DocIdSet set : sets) { - ramBytesUsed += RamUsageEstimator.NUM_BYTES_OBJECT_REF + set.ramBytesUsed(); - } - return ramBytesUsed; - } - - @Override - public Bits bits() throws IOException { - Bits[] bits = new Bits[sets.length]; - for (int i = 0; i < sets.length; i++) { - bits[i] = sets[i].bits(); - if (bits[i] == null) { - return null; - } - } - return new OrBits(bits); - } - - @Override - public DocIdSetIterator iterator() throws IOException { - return new IteratorBasedIterator(sets); - } - - /** A disjunction between several {@link Bits} instances with short-circuit logic. */ - public static class OrBits implements Bits { - - private final Bits[] bits; - - public OrBits(Bits[] bits) { - this.bits = bits; - } - - @Override - public boolean get(int index) { - for (Bits bit : bits) { - if (bit.get(index)) { - return true; - } - } - return false; - } - - @Override - public int length() { - return bits[0].length(); - } - } - - static class IteratorBasedIterator extends XDocIdSetIterator { - - final class Item { - public final DocIdSetIterator iter; - public int doc; - - public Item(DocIdSetIterator iter) { - this.iter = iter; - this.doc = -1; - } - } - - private int _curDoc; - private final Item[] _heap; - private int _size; - private final long cost; - private final boolean broken; - - IteratorBasedIterator(DocIdSet[] sets) throws IOException { - _curDoc = -1; - _heap = new Item[sets.length]; - _size = 0; - long cost = 0; - boolean broken = false; - for (DocIdSet set : sets) { - DocIdSetIterator iterator = set.iterator(); - broken |= DocIdSets.isBroken(iterator); - if (iterator != null) { - _heap[_size++] = new Item(iterator); - cost += iterator.cost(); - } - } - this.cost = cost; - this.broken = broken; - if (_size == 0) _curDoc = DocIdSetIterator.NO_MORE_DOCS; - } - - @Override - public boolean isBroken() { - return broken; - } - - @Override - public final int docID() { - return _curDoc; - } - - @Override - public final int nextDoc() throws IOException { - if (_curDoc == DocIdSetIterator.NO_MORE_DOCS) return DocIdSetIterator.NO_MORE_DOCS; - - Item top = _heap[0]; - while (true) { - DocIdSetIterator topIter = top.iter; - int docid; - if ((docid = topIter.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { - top.doc = docid; - heapAdjust(); - } else { - heapRemoveRoot(); - if (_size == 0) return (_curDoc = DocIdSetIterator.NO_MORE_DOCS); - } - top = _heap[0]; - int topDoc = top.doc; - if (topDoc > _curDoc) { - return (_curDoc = topDoc); - } - } - } - - @Override - public final int advance(int target) throws IOException { - if (_curDoc == DocIdSetIterator.NO_MORE_DOCS) return DocIdSetIterator.NO_MORE_DOCS; - - if (target <= _curDoc) target = _curDoc + 1; - - Item top = _heap[0]; - while (true) { - DocIdSetIterator topIter = top.iter; - int docid; - if ((docid = topIter.advance(target)) != DocIdSetIterator.NO_MORE_DOCS) { - top.doc = docid; - heapAdjust(); - } else { - heapRemoveRoot(); - if (_size == 0) return (_curDoc = DocIdSetIterator.NO_MORE_DOCS); - } - top = _heap[0]; - int topDoc = top.doc; - if (topDoc >= target) { - return (_curDoc = topDoc); - } - } - } - -// Organize subScorers into a min heap with scorers generating the earlest document on top. - /* - private final void heapify() { - int size = _size; - for (int i=(size>>1)-1; i>=0; i--) - heapAdjust(i); - } - */ - /* The subtree of subScorers at root is a min heap except possibly for its root element. - * Bubble the root down as required to make the subtree a heap. - */ - - private final void heapAdjust() { - final Item[] heap = _heap; - final Item top = heap[0]; - final int doc = top.doc; - final int size = _size; - int i = 0; - - while (true) { - int lchild = (i << 1) + 1; - if (lchild >= size) break; - - Item left = heap[lchild]; - int ldoc = left.doc; - - int rchild = lchild + 1; - if (rchild < size) { - Item right = heap[rchild]; - int rdoc = right.doc; - - if (rdoc <= ldoc) { - if (doc <= rdoc) break; - - heap[i] = right; - i = rchild; - continue; - } - } - - if (doc <= ldoc) break; - - heap[i] = left; - i = lchild; - } - heap[i] = top; - } - - // Remove the root Scorer from subScorers and re-establish it as a heap - - private void heapRemoveRoot() { - _size--; - if (_size > 0) { - Item tmp = _heap[0]; - _heap[0] = _heap[_size]; - _heap[_size] = tmp; // keep the finished iterator at the end for debugging - heapAdjust(); - } - } - - @Override - public long cost() { - return cost; - } - - } -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java deleted file mode 100644 index aa03eca7cd9..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.BitsFilteredDocIdSet; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.Bits; -import org.elasticsearch.common.lucene.docset.AndDocIdSet; -import org.elasticsearch.common.lucene.docset.DocIdSets; - -import java.io.IOException; -import java.util.List; - -/** - * - */ -public class AndFilter extends Filter { - - private final List filters; - - public AndFilter(List filters) { - this.filters = filters; - } - - public List filters() { - return filters; - } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - if (filters.size() == 1) { - return filters.get(0).getDocIdSet(context, acceptDocs); - } - DocIdSet[] sets = new DocIdSet[filters.size()]; - for (int i = 0; i < filters.size(); i++) { - DocIdSet set = filters.get(i).getDocIdSet(context, null); - if (DocIdSets.isEmpty(set)) { // none matching for this filter, we AND, so return EMPTY - return null; - } - sets[i] = set; - } - return BitsFilteredDocIdSet.wrap(new AndDocIdSet(sets), acceptDocs); - } - - @Override - public int hashCode() { - int hash = 7; - hash = 31 * hash + (null == filters ? 0 : filters.hashCode()); - return hash; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - - if ((obj == null) || (obj.getClass() != this.getClass())) - return false; - - AndFilter other = (AndFilter) obj; - return equalFilters(filters, other.filters); - } - - @Override - public String toString(String field) { - StringBuilder builder = new StringBuilder(); - for (Filter filter : filters) { - if (builder.length() > 0) { - builder.append(' '); - } - builder.append('+'); - builder.append(filter); - } - return builder.toString(); - } - - private boolean equalFilters(List filters1, List filters2) { - return (filters1 == filters2) || ((filters1 != null) && filters1.equals(filters2)); - } -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java deleted file mode 100644 index edb462a26e6..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.BitsFilteredDocIdSet; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.Bits; -import org.elasticsearch.common.lucene.docset.AllDocIdSet; - -import java.io.IOException; - -/** - * A filter that matches on all docs. - */ -public class MatchAllDocsFilter extends Filter { - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - return BitsFilteredDocIdSet.wrap(new AllDocIdSet(context.reader().maxDoc()), acceptDocs); - } - - @Override - public int hashCode() { - return this.getClass().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null) { - return false; - } - - if (obj.getClass() == this.getClass()) { - return true; - } - - return false; - } - - @Override - public String toString(String field) { - return "*:*"; - } -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/MatchNoDocsFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/MatchNoDocsFilter.java deleted file mode 100644 index 33c68eb0e39..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/MatchNoDocsFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.Bits; - -import java.io.IOException; - -/** - * A filter that matches no docs. - */ -public class MatchNoDocsFilter extends Filter { - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - return null; - } - - @Override - public int hashCode() { - return this.getClass().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null) { - return false; - } - - if (obj.getClass() == this.getClass()) { - return true; - } - - return false; - } - - @Override - public String toString(String field) { - return "MatchNoDocsFilter"; - } -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java deleted file mode 100644 index d485bb98ae4..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.BitsFilteredDocIdSet; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.Bits; -import org.elasticsearch.common.lucene.docset.AllDocIdSet; -import org.elasticsearch.common.lucene.docset.DocIdSets; -import org.elasticsearch.common.lucene.docset.NotDocIdSet; - -import java.io.IOException; - -/** - * - */ -public class NotFilter extends Filter { - - private final Filter filter; - - public NotFilter(Filter filter) { - this.filter = filter; - } - - public Filter filter() { - return filter; - } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - DocIdSet set = filter.getDocIdSet(context, null); - DocIdSet notSet; - if (DocIdSets.isEmpty(set)) { - notSet = new AllDocIdSet(context.reader().maxDoc()); - } else { - notSet = new NotDocIdSet(set, context.reader().maxDoc()); - } - return BitsFilteredDocIdSet.wrap(notSet, acceptDocs); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - NotFilter notFilter = (NotFilter) o; - return !(filter != null ? !filter.equals(notFilter.filter) : notFilter.filter != null); - } - - @Override - public String toString(String field) { - return "NotFilter(" + filter + ")"; - } - - @Override - public int hashCode() { - return filter != null ? filter.hashCode() : 0; - } -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java deleted file mode 100644 index 3bad9e83900..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.BitsFilteredDocIdSet; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.Bits; -import org.elasticsearch.common.lucene.docset.DocIdSets; -import org.elasticsearch.common.lucene.docset.OrDocIdSet; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -/** - * - */ -public class OrFilter extends Filter { - - private final List filters; - - public OrFilter(List filters) { - this.filters = filters; - } - - public List filters() { - return filters; - } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - if (filters.size() == 1) { - return filters.get(0).getDocIdSet(context, acceptDocs); - } - List sets = new ArrayList<>(filters.size()); - for (int i = 0; i < filters.size(); i++) { - DocIdSet set = filters.get(i).getDocIdSet(context, null); - if (DocIdSets.isEmpty(set)) { // none matching for this filter, continue - continue; - } - sets.add(set); - } - if (sets.size() == 0) { - return null; - } - DocIdSet set; - if (sets.size() == 1) { - set = sets.get(0); - } else { - set = new OrDocIdSet(sets.toArray(new DocIdSet[sets.size()])); - } - return BitsFilteredDocIdSet.wrap(set, acceptDocs); - } - - @Override - public int hashCode() { - int hash = 7; - hash = 31 * hash + (null == filters ? 0 : filters.hashCode()); - return hash; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - - if ((obj == null) || (obj.getClass() != this.getClass())) - return false; - - OrFilter other = (OrFilter) obj; - return equalFilters(filters, other.filters); - } - - @Override - public String toString(String field) { - StringBuilder builder = new StringBuilder(); - for (Filter filter : filters) { - if (builder.length() > 0) { - builder.append(' '); - } - builder.append(filter); - } - return builder.toString(); - } - - private boolean equalFilters(List filters1, List filters2) { - return (filters1 == filters2) || ((filters1 != null) && filters1.equals(filters2)); - } -} \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/common/lucene/search/Queries.java b/src/main/java/org/elasticsearch/common/lucene/search/Queries.java index 8f61f02a2da..b64758ee592 100644 --- a/src/main/java/org/elasticsearch/common/lucene/search/Queries.java +++ b/src/main/java/org/elasticsearch/common/lucene/search/Queries.java @@ -19,7 +19,15 @@ package org.elasticsearch.common.lucene.search; -import org.apache.lucene.search.*; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.Filter; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.index.query.QueryParseContext; @@ -33,17 +41,8 @@ import java.util.regex.Pattern; */ public class Queries { - /** - * A match all docs filter. Note, requires no caching!. - */ - public final static Filter MATCH_ALL_FILTER = new MatchAllDocsFilter(); - public final static Filter MATCH_NO_FILTER = new MatchNoDocsFilter(); - public static Query newMatchAllQuery() { - // We don't use MatchAllDocsQuery, its slower than the one below ... (much slower) - // NEVER cache this XConstantScore Query it's not immutable and based on #3521 - // some code might set a boost on this query. - return new ConstantScoreQuery(MATCH_ALL_FILTER); + return new MatchAllDocsQuery(); } /** Return a query that matches no document. */ @@ -51,6 +50,22 @@ public class Queries { return new BooleanQuery(); } + public static Filter newMatchAllFilter() { + return wrap(newMatchAllQuery()); + } + + public static Filter newMatchNoDocsFilter() { + return wrap(newMatchNoDocsQuery()); + } + + /** Return a query that matches all documents but those that match the given query. */ + public static Query not(Query q) { + BooleanQuery bq = new BooleanQuery(); + bq.add(new MatchAllDocsQuery(), Occur.MUST); + bq.add(q, Occur.MUST_NOT); + return bq; + } + public static boolean isNegativeQuery(Query q) { if (!(q instanceof BooleanQuery)) { return false; @@ -76,10 +91,11 @@ public class Queries { public static boolean isConstantMatchAllQuery(Query query) { if (query instanceof ConstantScoreQuery) { - ConstantScoreQuery scoreQuery = (ConstantScoreQuery) query; - if (scoreQuery.getQuery() instanceof MatchAllDocsFilter || scoreQuery.getQuery() instanceof MatchAllDocsQuery) { - return true; - } + return isConstantMatchAllQuery(((ConstantScoreQuery) query).getQuery()); + } else if (query instanceof QueryWrapperFilter) { + return isConstantMatchAllQuery(((QueryWrapperFilter) query).getQuery()); + } else if (query instanceof MatchAllDocsQuery) { + return true; } return false; } @@ -151,10 +167,15 @@ public class Queries { */ @SuppressForbidden(reason = "QueryWrapperFilter cachability") public static Filter wrap(Query query, QueryParseContext context) { - if (context.requireCustomQueryWrappingFilter() || CustomQueryWrappingFilter.shouldUseCustomQueryWrappingFilter(query)) { + if ((context != null && context.requireCustomQueryWrappingFilter()) || CustomQueryWrappingFilter.shouldUseCustomQueryWrappingFilter(query)) { return new CustomQueryWrappingFilter(query); } else { return new QueryWrapperFilter(query); } } + + /** Wrap as a {@link Filter}. */ + public static Filter wrap(Query query) { + return wrap(query, null); + } } diff --git a/src/main/java/org/elasticsearch/common/lucene/search/RegexpFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/RegexpFilter.java deleted file mode 100644 index 10225b1c66a..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/RegexpFilter.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.Term; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.search.MultiTermQueryWrapperFilter; -import org.apache.lucene.search.RegexpQuery; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.automaton.Operations; -import org.apache.lucene.util.automaton.RegExp; - -import java.io.IOException; - -/** - * A lazy regexp filter which only builds the automaton on the first call to {@link #getDocIdSet(LeafReaderContext, Bits)}. - * It is not thread safe (so can't be applied on multiple segments concurrently) - */ -public class RegexpFilter extends Filter { - - private final Term term; - private final int flags; - - // use delegation here to support efficient implementation of equals & hashcode for this - // filter (as it will be used as the filter cache key) - private final InternalFilter filter; - - public RegexpFilter(Term term) { - this(term, RegExp.ALL); - } - - public RegexpFilter(Term term, int flags) { - this(term, flags, Operations.DEFAULT_MAX_DETERMINIZED_STATES); - } - - public RegexpFilter(Term term, int flags, int maxDeterminizedStates) { - filter = new InternalFilter(term, flags, maxDeterminizedStates); - this.term = term; - this.flags = flags; - } - - public String field() { - return term.field(); - } - - public String regexp() { - return term.text(); - } - - public int flags() { - return flags; - } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - return filter.getDocIdSet(context, acceptDocs); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - org.elasticsearch.common.lucene.search.RegexpFilter that = (org.elasticsearch.common.lucene.search.RegexpFilter) o; - - if (flags != that.flags) return false; - if (term != null ? !term.equals(that.term) : that.term != null) return false; - - return true; - } - - @Override - public int hashCode() { - int result = term != null ? term.hashCode() : 0; - result = 31 * result + flags; - return result; - } - - @Override - public String toString(String field) { - // todo should we also show the flags? - return term.field() + ":" + term.text(); - } - - static class InternalFilter extends MultiTermQueryWrapperFilter { - - public InternalFilter(Term term, int flags, int maxDeterminizedStates) { - super(new RegexpQuery(term, flags, maxDeterminizedStates)); - } - } - -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java deleted file mode 100644 index d33705da8b1..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java +++ /dev/null @@ -1,377 +0,0 @@ -package org.elasticsearch.common.lucene.search; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.queries.FilterClause; -import org.apache.lucene.search.BitsFilteredDocIdSet; -import org.apache.lucene.search.BooleanClause.Occur; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.BitDocIdSet; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.CollectionUtil; -import org.elasticsearch.common.lucene.docset.AllDocIdSet; -import org.elasticsearch.common.lucene.docset.AndDocIdSet; -import org.elasticsearch.common.lucene.docset.DocIdSets; -import org.elasticsearch.common.lucene.docset.NotDocIdSet; -import org.elasticsearch.common.lucene.docset.OrDocIdSet.OrBits; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; - -/** - * Similar to {@link org.apache.lucene.queries.BooleanFilter}. - *

- * Our own variance mainly differs by the fact that we pass the acceptDocs down to the filters - * and don't filter based on them at the end. Our logic is a bit different, and we filter based on that - * at the top level filter chain. - */ -public class XBooleanFilter extends Filter implements Iterable { - - private static final Comparator COST_DESCENDING = new Comparator() { - @Override - public int compare(DocIdSetIterator o1, DocIdSetIterator o2) { - return Long.compare(o2.cost(), o1.cost()); - } - }; - private static final Comparator COST_ASCENDING = new Comparator() { - @Override - public int compare(DocIdSetIterator o1, DocIdSetIterator o2) { - return Long.compare(o1.cost(), o2.cost()); - } - }; - - final List clauses = new ArrayList<>(); - - /** - * Returns the a DocIdSetIterator representing the Boolean composition - * of the filters that have been added. - */ - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - final int maxDoc = context.reader().maxDoc(); - - // the 0-clauses case is ambiguous because an empty OR filter should return nothing - // while an empty AND filter should return all docs, so we handle this case explicitely - if (clauses.isEmpty()) { - return null; - } - - // optimize single case... - if (clauses.size() == 1) { - FilterClause clause = clauses.get(0); - DocIdSet set = clause.getFilter().getDocIdSet(context, acceptDocs); - if (clause.getOccur() == Occur.MUST_NOT) { - if (DocIdSets.isEmpty(set)) { - return new AllDocIdSet(maxDoc); - } else { - return new NotDocIdSet(set, maxDoc); - } - } - // SHOULD or MUST, just return the set... - if (DocIdSets.isEmpty(set)) { - return null; - } - return set; - } - - // We have several clauses, try to organize things to make it easier to process - List shouldIterators = new ArrayList<>(); - List shouldBits = new ArrayList<>(); - boolean hasShouldClauses = false; - - List requiredIterators = new ArrayList<>(); - List excludedIterators = new ArrayList<>(); - - List requiredBits = new ArrayList<>(); - List excludedBits = new ArrayList<>(); - - for (FilterClause clause : clauses) { - DocIdSet set = clause.getFilter().getDocIdSet(context, null); - DocIdSetIterator it = null; - Bits bits = null; - if (DocIdSets.isEmpty(set) == false) { - it = set.iterator(); - if (it != null) { - bits = set.bits(); - } - } - - switch (clause.getOccur()) { - case SHOULD: - hasShouldClauses = true; - if (it == null) { - // continue, but we recorded that there is at least one should clause - // so that if all iterators are null we know that nothing matches this - // filter since at least one SHOULD clause needs to match - } else if (bits != null && DocIdSets.isBroken(it)) { - shouldBits.add(bits); - } else { - shouldIterators.add(it); - } - break; - case MUST: - if (it == null) { - // no documents matched a clause that is compulsory, then nothing matches at all - return null; - } else if (bits != null && DocIdSets.isBroken(it)) { - requiredBits.add(bits); - } else { - requiredIterators.add(it); - } - break; - case MUST_NOT: - if (it == null) { - // ignore - } else if (bits != null && DocIdSets.isBroken(it)) { - excludedBits.add(bits); - } else { - excludedIterators.add(it); - } - break; - default: - throw new AssertionError(); - } - } - - // Since BooleanFilter requires that at least one SHOULD clause matches, - // transform the SHOULD clauses into a MUST clause - - if (hasShouldClauses) { - if (shouldIterators.isEmpty() && shouldBits.isEmpty()) { - // we had should clauses, but they all produced empty sets - // yet BooleanFilter requires that at least one clause matches - // so it means we do not match anything - return null; - } else if (shouldIterators.size() == 1 && shouldBits.isEmpty()) { - requiredIterators.add(shouldIterators.get(0)); - } else { - // apply high-cardinality should clauses first - CollectionUtil.timSort(shouldIterators, COST_DESCENDING); - - BitDocIdSet.Builder shouldBuilder = null; - for (DocIdSetIterator it : shouldIterators) { - if (shouldBuilder == null) { - shouldBuilder = new BitDocIdSet.Builder(maxDoc); - } - shouldBuilder.or(it); - } - - if (shouldBuilder != null && shouldBits.isEmpty() == false) { - // we have both iterators and bits, there is no way to compute - // the union efficiently, so we just transform the iterators into - // bits - // add first since these are fast bits - shouldBits.add(0, shouldBuilder.build().bits()); - shouldBuilder = null; - } - - if (shouldBuilder == null) { - // only bits - assert shouldBits.size() >= 1; - if (shouldBits.size() == 1) { - requiredBits.add(shouldBits.get(0)); - } else { - requiredBits.add(new OrBits(shouldBits.toArray(new Bits[shouldBits.size()]))); - } - } else { - assert shouldBits.isEmpty(); - // only iterators, we can add the merged iterator to the list of required iterators - requiredIterators.add(shouldBuilder.build().iterator()); - } - } - } else { - assert shouldIterators.isEmpty(); - assert shouldBits.isEmpty(); - } - - // From now on, we don't have to care about SHOULD clauses anymore since we upgraded - // them to required clauses (if necessary) - - // cheap iterators first to make intersection faster - CollectionUtil.timSort(requiredIterators, COST_ASCENDING); - CollectionUtil.timSort(excludedIterators, COST_ASCENDING); - - // Intersect iterators - BitDocIdSet.Builder res = null; - for (DocIdSetIterator iterator : requiredIterators) { - if (res == null) { - res = new BitDocIdSet.Builder(maxDoc); - res.or(iterator); - } else { - res.and(iterator); - } - } - for (DocIdSetIterator iterator : excludedIterators) { - if (res == null) { - res = new BitDocIdSet.Builder(maxDoc, true); - } - res.andNot(iterator); - } - - // Transform the excluded bits into required bits - if (excludedBits.isEmpty() == false) { - Bits excluded; - if (excludedBits.size() == 1) { - excluded = excludedBits.get(0); - } else { - excluded = new OrBits(excludedBits.toArray(new Bits[excludedBits.size()])); - } - requiredBits.add(new NotDocIdSet.NotBits(excluded)); - } - - // The only thing left to do is to intersect 'res' with 'requiredBits' - - // the main doc id set that will drive iteration - DocIdSet main; - if (res == null) { - main = new AllDocIdSet(maxDoc); - } else { - main = res.build(); - } - - // apply accepted docs and compute the bits to filter with - // accepted docs are added first since they are fast and will help not computing anything on deleted docs - if (acceptDocs != null) { - requiredBits.add(0, acceptDocs); - } - // the random-access filter that we will apply to 'main' - Bits filter; - if (requiredBits.isEmpty()) { - filter = null; - } else if (requiredBits.size() == 1) { - filter = requiredBits.get(0); - } else { - filter = new AndDocIdSet.AndBits(requiredBits.toArray(new Bits[requiredBits.size()])); - } - - return BitsFilteredDocIdSet.wrap(main, filter); - } - - /** - * Adds a new FilterClause to the Boolean Filter container - * - * @param filterClause A FilterClause object containing a Filter and an Occur parameter - */ - public void add(FilterClause filterClause) { - clauses.add(filterClause); - } - - public final void add(Filter filter, Occur occur) { - add(new FilterClause(filter, occur)); - } - - /** - * Returns the list of clauses - */ - public List clauses() { - return clauses; - } - - /** - * Returns an iterator on the clauses in this query. It implements the {@link Iterable} interface to - * make it possible to do: - *

for (FilterClause clause : booleanFilter) {}
- */ - @Override - public final Iterator iterator() { - return clauses().iterator(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - - if ((obj == null) || (obj.getClass() != this.getClass())) { - return false; - } - - final XBooleanFilter other = (XBooleanFilter) obj; - return clauses.equals(other.clauses); - } - - @Override - public int hashCode() { - return 657153718 ^ clauses.hashCode(); - } - - /** - * Prints a user-readable version of this Filter. - */ - @Override - public String toString(String field) { - final StringBuilder buffer = new StringBuilder("BooleanFilter("); - final int minLen = buffer.length(); - for (final FilterClause c : clauses) { - if (buffer.length() > minLen) { - buffer.append(' '); - } - buffer.append(c); - } - return buffer.append(')').toString(); - } - - static class ResultClause { - - public final DocIdSet docIdSet; - public final Bits bits; - public final FilterClause clause; - - DocIdSetIterator docIdSetIterator; - - ResultClause(DocIdSet docIdSet, Bits bits, FilterClause clause) { - this.docIdSet = docIdSet; - this.bits = bits; - this.clause = clause; - } - - /** - * @return An iterator, but caches it for subsequent usage. Don't use if iterator is consumed in one invocation. - */ - DocIdSetIterator iterator() throws IOException { - if (docIdSetIterator != null) { - return docIdSetIterator; - } else { - return docIdSetIterator = docIdSet.iterator(); - } - } - - } - - static boolean iteratorMatch(DocIdSetIterator docIdSetIterator, int target) throws IOException { - assert docIdSetIterator != null; - int current = docIdSetIterator.docID(); - if (current == DocIdSetIterator.NO_MORE_DOCS || target < current) { - return false; - } else { - if (current == target) { - return true; - } else { - return docIdSetIterator.advance(target) == target; - } - } - } - -} diff --git a/src/main/java/org/elasticsearch/common/lucene/search/XDocIdSetIterator.java b/src/main/java/org/elasticsearch/common/lucene/search/XDocIdSetIterator.java deleted file mode 100644 index 6f89967280c..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/search/XDocIdSetIterator.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.search.DocIdSetIterator; -import org.elasticsearch.common.lucene.docset.DocIdSets; - -/** - * Extension of {@link DocIdSetIterator} that allows to know if iteration is - * implemented efficiently. - */ -public abstract class XDocIdSetIterator extends DocIdSetIterator { - - /** - * Return true if this iterator cannot both - * {@link DocIdSetIterator#nextDoc} and {@link DocIdSetIterator#advance} - * in sub-linear time. - * - * Do not call this method directly, use {@link DocIdSets#isBroken}. - */ - public abstract boolean isBroken(); - -} diff --git a/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java b/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java index 178fbbd7d2a..7d142a0c803 100644 --- a/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java +++ b/src/main/java/org/elasticsearch/index/aliases/IndexAliasesService.java @@ -19,13 +19,13 @@ package org.elasticsearch.index.aliases; -import org.apache.lucene.queries.FilterClause; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.compress.CompressedString; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.xcontent.XContentFactory; @@ -95,7 +95,7 @@ public class IndexAliasesService extends AbstractIndexComponent implements Itera return indexAlias.parsedFilter(); } else { // we need to bench here a bit, to see maybe it makes sense to use OrFilter - XBooleanFilter combined = new XBooleanFilter(); + BooleanQuery combined = new BooleanQuery(); for (String alias : aliases) { IndexAlias indexAlias = alias(alias); if (indexAlias == null) { @@ -103,19 +103,13 @@ public class IndexAliasesService extends AbstractIndexComponent implements Itera throw new InvalidAliasNameException(index, aliases[0], "Unknown alias name was passed to alias Filter"); } if (indexAlias.parsedFilter() != null) { - combined.add(new FilterClause(indexAlias.parsedFilter(), BooleanClause.Occur.SHOULD)); + combined.add(indexAlias.parsedFilter(), BooleanClause.Occur.SHOULD); } else { // The filter might be null only if filter was removed after filteringAliases was called return null; } } - if (combined.clauses().size() == 0) { - return null; - } - if (combined.clauses().size() == 1) { - return combined.clauses().get(0).getFilter(); - } - return combined; + return Queries.wrap(combined); } } diff --git a/src/main/java/org/elasticsearch/index/cache/filter/AutoFilterCachingPolicy.java b/src/main/java/org/elasticsearch/index/cache/filter/AutoFilterCachingPolicy.java deleted file mode 100644 index 9e7e27e450e..00000000000 --- a/src/main/java/org/elasticsearch/index/cache/filter/AutoFilterCachingPolicy.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.cache.filter; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; -import org.apache.lucene.search.UsageTrackingFilterCachingPolicy; -import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.docset.DocIdSets; -import org.elasticsearch.common.settings.ImmutableSettings; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.AbstractIndexComponent; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.settings.IndexSettings; - -import java.io.IOException; - -/** - * This class is a wrapper around {@link UsageTrackingFilterCachingPolicy} - * which wires parameters through index settings and makes sure to not - * cache {@link DocIdSet}s which have a {@link DocIdSets#isBroken(DocIdSetIterator) broken} - * iterator. - */ -public class AutoFilterCachingPolicy extends AbstractIndexComponent implements FilterCachingPolicy { - - // These settings don't have the purpose of being documented. They are only here so that - // if anyone ever hits an issue with elasticsearch that is due to the value of one of these - // parameters, then it might be possible to temporarily work around the issue without having - // to wait for a new release - - // number of times a filter that is expensive to compute should be seen before the doc id sets are cached - public static final String MIN_FREQUENCY_COSTLY = "index.cache.filter.policy.min_frequency.costly"; - // number of times a filter that produces cacheable filters should be seen before the doc id sets are cached - public static final String MIN_FREQUENCY_CACHEABLE = "index.cache.filter.policy.min_frequency.cacheable"; - // same for filters that produce doc id sets that are not directly cacheable - public static final String MIN_FREQUENCY_OTHER = "index.cache.filter.policy.min_frequency.other"; - // sources of segments that should be cached - public static final String MIN_SEGMENT_SIZE_RATIO = "index.cache.filter.policy.min_segment_size_ratio"; - // size of the history to keep for filters. A filter will be cached if it has been seen more than a given - // number of times (depending on the filter, the segment and the produced DocIdSet) in the most - // ${history_size} recently used filters - public static final String HISTORY_SIZE = "index.cache.filter.policy.history_size"; - - public static Settings AGGRESSIVE_CACHING_SETTINGS = ImmutableSettings.builder() - .put(MIN_FREQUENCY_CACHEABLE, 1) - .put(MIN_FREQUENCY_COSTLY, 1) - .put(MIN_FREQUENCY_OTHER, 1) - .put(MIN_SEGMENT_SIZE_RATIO, 0.000000001f) - .build(); - - private final FilterCachingPolicy in; - - @Inject - public AutoFilterCachingPolicy(Index index, @IndexSettings Settings indexSettings) { - super(index, indexSettings); - final int historySize = indexSettings.getAsInt(HISTORY_SIZE, 1000); - // cache aggressively filters that produce sets that are already cacheable, - // ie. if the filter has been used twice or more among the most 1000 recently - // used filters - final int minFrequencyCacheable = indexSettings.getAsInt(MIN_FREQUENCY_CACHEABLE, 2); - // cache aggressively filters whose getDocIdSet method is costly - final int minFrequencyCostly = indexSettings.getAsInt(MIN_FREQUENCY_COSTLY, 2); - // be a bit less aggressive when the produced doc id sets are not cacheable - final int minFrequencyOther = indexSettings.getAsInt(MIN_FREQUENCY_OTHER, 5); - final float minSegmentSizeRatio = indexSettings.getAsFloat(MIN_SEGMENT_SIZE_RATIO, 0.01f); - in = new UsageTrackingFilterCachingPolicy(minSegmentSizeRatio, historySize, minFrequencyCostly, minFrequencyCacheable, minFrequencyOther); - } - - @Override - public void onUse(Filter filter) { - in.onUse(filter); - } - - @Override - public boolean shouldCache(Filter filter, LeafReaderContext context, DocIdSet set) throws IOException { - if (set != null && DocIdSets.isBroken(set.iterator())) { - // O(maxDoc) to cache, no thanks. - return false; - } - - return in.shouldCache(filter, context, set); - } - -} diff --git a/src/main/java/org/elasticsearch/index/cache/filter/FilterCache.java b/src/main/java/org/elasticsearch/index/cache/filter/FilterCache.java index 48592cfff26..a16b5da2bd9 100644 --- a/src/main/java/org/elasticsearch/index/cache/filter/FilterCache.java +++ b/src/main/java/org/elasticsearch/index/cache/filter/FilterCache.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.cache.filter; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.lucene.HashedBytesRef; import org.elasticsearch.index.IndexComponent; @@ -48,7 +48,7 @@ public interface FilterCache extends IndexComponent, Closeable { String type(); - Filter cache(Filter filterToCache, @Nullable HashedBytesRef cacheKey, FilterCachingPolicy policy); + Filter cache(Filter filterToCache, @Nullable HashedBytesRef cacheKey, QueryCachingPolicy policy); void clear(Object reader); diff --git a/src/main/java/org/elasticsearch/index/cache/filter/FilterCacheModule.java b/src/main/java/org/elasticsearch/index/cache/filter/FilterCacheModule.java index a50de7bef0c..551ea4fa279 100644 --- a/src/main/java/org/elasticsearch/index/cache/filter/FilterCacheModule.java +++ b/src/main/java/org/elasticsearch/index/cache/filter/FilterCacheModule.java @@ -19,7 +19,8 @@ package org.elasticsearch.index.cache.filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.UsageTrackingQueryCachingPolicy; import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Scopes; import org.elasticsearch.common.settings.Settings; @@ -32,6 +33,8 @@ public class FilterCacheModule extends AbstractModule { public static final class FilterCacheSettings { public static final String FILTER_CACHE_TYPE = "index.cache.filter.type"; + // for test purposes only + public static final String FILTER_CACHE_EVERYTHING = "index.cache.filter.everything"; } private final Settings settings; @@ -48,7 +51,10 @@ public class FilterCacheModule extends AbstractModule { // the filter cache is a node-level thing, however we want the most popular filters // to be computed on a per-index basis, that is why we don't use the SINGLETON // scope below - bind(FilterCachingPolicy.class) - .to(AutoFilterCachingPolicy.class); + if (settings.getAsBoolean(FilterCacheSettings.FILTER_CACHE_EVERYTHING, false)) { + bind(QueryCachingPolicy.class).toInstance(QueryCachingPolicy.ALWAYS_CACHE); + } else { + bind(QueryCachingPolicy.class).toInstance(new UsageTrackingQueryCachingPolicy()); + } } } diff --git a/src/main/java/org/elasticsearch/index/cache/filter/none/NoneFilterCache.java b/src/main/java/org/elasticsearch/index/cache/filter/none/NoneFilterCache.java index d1b6c75194b..41a704a9afd 100644 --- a/src/main/java/org/elasticsearch/index/cache/filter/none/NoneFilterCache.java +++ b/src/main/java/org/elasticsearch/index/cache/filter/none/NoneFilterCache.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.cache.filter.none; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; @@ -58,7 +58,7 @@ public class NoneFilterCache extends AbstractIndexComponent implements FilterCac } @Override - public Filter cache(Filter filterToCache, @Nullable HashedBytesRef cacheKey, FilterCachingPolicy policy) { + public Filter cache(Filter filterToCache, @Nullable HashedBytesRef cacheKey, QueryCachingPolicy policy) { return filterToCache; } diff --git a/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java b/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java index 8333e0fa01d..2720d32d9d1 100644 --- a/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java +++ b/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java @@ -29,7 +29,7 @@ import org.apache.lucene.index.SegmentReader; import org.apache.lucene.search.BitsFilteredDocIdSet; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.util.Bits; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Nullable; @@ -128,7 +128,7 @@ public class WeightedFilterCache extends AbstractIndexComponent implements Filte } @Override - public Filter cache(Filter filterToCache, @Nullable HashedBytesRef cacheKey, FilterCachingPolicy cachePolicy) { + public Filter cache(Filter filterToCache, @Nullable HashedBytesRef cacheKey, QueryCachingPolicy cachePolicy) { if (filterToCache == null) { return null; } @@ -148,10 +148,10 @@ public class WeightedFilterCache extends AbstractIndexComponent implements Filte private final Filter filter; private final Object filterCacheKey; - private final FilterCachingPolicy cachePolicy; + private final QueryCachingPolicy cachePolicy; private final WeightedFilterCache cache; - FilterCacheFilterWrapper(Filter filter, Object cacheKey, FilterCachingPolicy cachePolicy, WeightedFilterCache cache) { + FilterCacheFilterWrapper(Filter filter, Object cacheKey, QueryCachingPolicy cachePolicy, WeightedFilterCache cache) { this.filter = filter; this.filterCacheKey = cacheKey != null ? cacheKey : filter; this.cachePolicy = cachePolicy; @@ -172,7 +172,7 @@ public class WeightedFilterCache extends AbstractIndexComponent implements Filte ret = cacheValue; } else { final DocIdSet uncached = filter.getDocIdSet(context, null); - if (cachePolicy.shouldCache(filter, context, uncached)) { + if (cachePolicy.shouldCache(filter, context)) { if (!cache.seenReaders.containsKey(context.reader().getCoreCacheKey())) { Boolean previous = cache.seenReaders.putIfAbsent(context.reader().getCoreCacheKey(), Boolean.TRUE); if (previous == null) { diff --git a/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 4872e107f6e..a311e9eaded 100755 --- a/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -31,11 +31,13 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.DelegatingAnalyzerWrapper; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.FilterClause; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.QueryWrapperFilter; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchIllegalArgumentException; @@ -47,9 +49,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.Streams; -import org.elasticsearch.common.lucene.search.AndFilter; -import org.elasticsearch.common.lucene.search.NotFilter; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; @@ -446,18 +446,21 @@ public class MapperService extends AbstractIndexComponent { } } } - Filter excludePercolatorType = null; + Filter percolatorType = null; if (filterPercolateType) { - excludePercolatorType = new NotFilter(documentMapper(PercolatorService.TYPE_NAME).typeFilter()); + percolatorType = documentMapper(PercolatorService.TYPE_NAME).typeFilter(); } if (types == null || types.length == 0) { if (hasNested && filterPercolateType) { - return new AndFilter(ImmutableList.of(excludePercolatorType, NonNestedDocsFilter.INSTANCE)); + BooleanQuery bq = new BooleanQuery(); + bq.add(percolatorType, Occur.MUST_NOT); + bq.add(NonNestedDocsFilter.INSTANCE, Occur.MUST); + return Queries.wrap(bq); } else if (hasNested) { return NonNestedDocsFilter.INSTANCE; } else if (filterPercolateType) { - return excludePercolatorType; + return Queries.wrap(Queries.not(percolatorType)); } else { return null; } @@ -466,9 +469,12 @@ public class MapperService extends AbstractIndexComponent { // since they have different types (starting with __) if (types.length == 1) { DocumentMapper docMapper = documentMapper(types[0]); - Filter filter = docMapper != null ? docMapper.typeFilter() : new TermFilter(new Term(types[0])); - if (hasNested) { - return new AndFilter(ImmutableList.of(filter, NonNestedDocsFilter.INSTANCE)); + Filter filter = docMapper != null ? docMapper.typeFilter() : Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, types[0]))); + if (filterPercolateType) { + BooleanQuery bq = new BooleanQuery(); + bq.add(percolatorType, Occur.MUST_NOT); + bq.add(filter, Occur.MUST); + return Queries.wrap(bq); } else { return filter; } @@ -493,31 +499,34 @@ public class MapperService extends AbstractIndexComponent { for (int i = 0; i < typesBytes.length; i++) { typesBytes[i] = new BytesRef(types[i]); } - TermsFilter termsFilter = new TermsFilter(TypeFieldMapper.NAME, typesBytes); + TermsQuery termsFilter = new TermsQuery(TypeFieldMapper.NAME, typesBytes); if (filterPercolateType) { - return new AndFilter(ImmutableList.of(excludePercolatorType, termsFilter)); + BooleanQuery bq = new BooleanQuery(); + bq.add(percolatorType, Occur.MUST_NOT); + bq.add(termsFilter, Occur.MUST); + return Queries.wrap(bq); } else { - return termsFilter; + return Queries.wrap(termsFilter); } } else { // Current bool filter requires that at least one should clause matches, even with a must clause. - XBooleanFilter bool = new XBooleanFilter(); + BooleanQuery bool = new BooleanQuery(); for (String type : types) { DocumentMapper docMapper = documentMapper(type); if (docMapper == null) { - bool.add(new FilterClause(new TermFilter(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.SHOULD)); + bool.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.SHOULD); } else { - bool.add(new FilterClause(docMapper.typeFilter(), BooleanClause.Occur.SHOULD)); + bool.add(docMapper.typeFilter(), BooleanClause.Occur.SHOULD); } } if (filterPercolateType) { - bool.add(excludePercolatorType, BooleanClause.Occur.MUST); + bool.add(percolatorType, BooleanClause.Occur.MUST_NOT); } if (hasNested) { bool.add(NonNestedDocsFilter.INSTANCE, BooleanClause.Occur.MUST); } - return bool; + return Queries.wrap(bool); } } diff --git a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java index 9eee65768db..a9ab088d285 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java @@ -29,17 +29,14 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.MultiTermQuery; -import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.search.TermRangeFilter; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchIllegalArgumentException; @@ -49,8 +46,7 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.lucene.search.MatchNoDocsFilter; -import org.elasticsearch.common.lucene.search.RegexpFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; @@ -496,14 +492,14 @@ public abstract class AbstractFieldMapper implements FieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { - return new TermFilter(names().createIndexNameTerm(indexedValueForSearch(value))); + return Queries.wrap(new TermQuery(names().createIndexNameTerm(indexedValueForSearch(value)))); } @Override public Filter termsFilter(List values, @Nullable QueryParseContext context) { switch (values.size()) { case 0: - return new MatchNoDocsFilter(); + return Queries.newMatchNoDocsFilter(); case 1: // When there is a single term, it's important to return a term filter so that // it can return a DocIdSet that is directly backed by a postings list, instead @@ -515,7 +511,7 @@ public abstract class AbstractFieldMapper implements FieldMapper { for (int i = 0; i < bytesRefs.length; i++) { bytesRefs[i] = indexedValueForSearch(values.get(i)); } - return new TermsFilter(names.indexName(), bytesRefs); + return Queries.wrap(new TermsQuery(names.indexName(), bytesRefs)); } } @@ -545,10 +541,10 @@ public abstract class AbstractFieldMapper implements FieldMapper { @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return new TermRangeFilter(names.indexName(), + return Queries.wrap(new TermRangeQuery(names.indexName(), lowerTerm == null ? null : indexedValueForSearch(lowerTerm), upperTerm == null ? null : indexedValueForSearch(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -567,7 +563,7 @@ public abstract class AbstractFieldMapper implements FieldMapper { @Override public Filter prefixFilter(Object value, @Nullable QueryParseContext context) { - return new PrefixFilter(names().createIndexNameTerm(indexedValueForSearch(value))); + return Queries.wrap(new PrefixQuery(names().createIndexNameTerm(indexedValueForSearch(value)))); } @Override @@ -581,7 +577,7 @@ public abstract class AbstractFieldMapper implements FieldMapper { @Override public Filter regexpFilter(Object value, int flags, int maxDeterminizedStates, @Nullable QueryParseContext parseContext) { - return new RegexpFilter(names().createIndexNameTerm(indexedValueForSearch(value)), flags, maxDeterminizedStates); + return Queries.wrap(new RegexpQuery(names().createIndexNameTerm(indexedValueForSearch(value)), flags, maxDeterminizedStates)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java index 5f66116c895..18344809168 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java @@ -23,14 +23,15 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.IndexOptions; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -205,7 +206,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper { if (nullValue == null) { return null; } - return new TermFilter(names().createIndexNameTerm(nullValue ? Values.TRUE : Values.FALSE)); + return Queries.wrap(new TermQuery(names().createIndexNameTerm(nullValue ? Values.TRUE : Values.FALSE))); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java index 676df2c090a..d841b9a01ec 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java @@ -24,7 +24,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -34,6 +33,7 @@ import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -218,16 +218,16 @@ public class ByteFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { int iValue = parseValueAsInt(value); - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, - iValue, iValue, true, true); + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true)); } @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseValueAsInt(lowerTerm), upperTerm == null ? null : parseValueAsInt(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -243,10 +243,10 @@ public class ByteFieldMapper extends NumberFieldMapper { if (nullValue == null) { return null; } - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, nullValue.intValue(), nullValue.intValue(), - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index a518369ea3e..d5cc31606d7 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -24,7 +24,6 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -40,6 +39,7 @@ import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.lucene.search.NoCacheQuery; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.ResolvableFilter; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; @@ -326,8 +326,8 @@ public class DateFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { final long lValue = parseToMilliseconds(value); - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, - lValue, lValue, true, true); + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, + lValue, lValue, true, true)); } @Override @@ -405,9 +405,9 @@ public class DateFieldMapper extends NumberFieldMapper { if (fieldData != null) { filter = NumericRangeFieldDataFilter.newLongRange(fieldData, lowerVal,upperVal, includeLower, includeUpper); } else { - filter = NumericRangeFilter.newLongRange( + filter = Queries.wrap(NumericRangeQuery.newLongRange( names.indexName(), precisionStep, lowerVal, upperVal, includeLower, includeUpper - ); + )); } return filter; @@ -419,10 +419,10 @@ public class DateFieldMapper extends NumberFieldMapper { return null; } long value = parseStringValue(nullValue); - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, value, value, - true, true); + true, true)); } diff --git a/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java index 9103714b55a..96c75f98153 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java @@ -28,7 +28,6 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -38,6 +37,7 @@ import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.util.ByteUtils; @@ -209,20 +209,20 @@ public class DoubleFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { double dValue = parseDoubleValue(value); - return NumericRangeFilter.newDoubleRange(names.indexName(), precisionStep, - dValue, dValue, true, true); + return Queries.wrap(NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, + dValue, dValue, true, true)); } @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newDoubleRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseDoubleValue(lowerTerm), upperTerm == null ? null : parseDoubleValue(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } public Filter rangeFilter(Double lowerTerm, Double upperTerm, boolean includeLower, boolean includeUpper) { - return NumericRangeFilter.newDoubleRange(names.indexName(), precisionStep, lowerTerm, upperTerm, includeLower, includeUpper); + return Queries.wrap(NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, lowerTerm, upperTerm, includeLower, includeUpper)); } @Override @@ -238,10 +238,10 @@ public class DoubleFieldMapper extends NumberFieldMapper { if (nullValue == null) { return null; } - return NumericRangeFilter.newDoubleRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, nullValue, nullValue, - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java index 26094073a04..4bb38974103 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java @@ -28,7 +28,6 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -39,6 +38,7 @@ import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.util.ByteUtils; @@ -219,16 +219,16 @@ public class FloatFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { float fValue = parseValue(value); - return NumericRangeFilter.newFloatRange(names.indexName(), precisionStep, - fValue, fValue, true, true); + return Queries.wrap(NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, + fValue, fValue, true, true)); } @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newFloatRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseValue(lowerTerm), upperTerm == null ? null : parseValue(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -244,10 +244,10 @@ public class FloatFieldMapper extends NumberFieldMapper { if (nullValue == null) { return null; } - return NumericRangeFilter.newFloatRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, nullValue, nullValue, - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java index 8bf9a2d267f..4989d3e6856 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java @@ -25,7 +25,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -36,6 +35,7 @@ import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -205,8 +205,8 @@ public class IntegerFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { int iValue = parseValue(value); - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, - iValue, iValue, true, true); + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true)); } @Override @@ -219,10 +219,10 @@ public class IntegerFieldMapper extends NumberFieldMapper { @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseValue(lowerTerm), upperTerm == null ? null : parseValue(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -238,10 +238,10 @@ public class IntegerFieldMapper extends NumberFieldMapper { if (nullValue == null) { return null; } - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, nullValue, nullValue, - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java index 46e825a764c..a11d89a000d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java @@ -25,7 +25,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -36,6 +35,7 @@ import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -195,8 +195,8 @@ public class LongFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { long iValue = parseLongValue(value); - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, - iValue, iValue, true, true); + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, + iValue, iValue, true, true)); } @Override @@ -209,10 +209,10 @@ public class LongFieldMapper extends NumberFieldMapper { @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseLongValue(lowerTerm), upperTerm == null ? null : parseLongValue(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -228,10 +228,10 @@ public class LongFieldMapper extends NumberFieldMapper { if (nullValue == null) { return null; } - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, nullValue, nullValue, - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java index 00830c6aae2..59e9fd44869 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java @@ -25,7 +25,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -36,6 +35,7 @@ import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -219,24 +219,24 @@ public class ShortFieldMapper extends NumberFieldMapper { @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { int iValue = parseValueAsInt(value); - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, - iValue, iValue, true, true); + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true)); } @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseValueAsInt(lowerTerm), upperTerm == null ? null : parseValueAsInt(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override public Filter rangeFilter(QueryParseContext parseContext, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFieldDataFilter.newShortRange((IndexNumericFieldData) parseContext.getForField(this), + return Queries.wrap(NumericRangeFieldDataFilter.newShortRange((IndexNumericFieldData) parseContext.getForField(this), lowerTerm == null ? null : parseValue(lowerTerm), upperTerm == null ? null : parseValue(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -244,10 +244,10 @@ public class ShortFieldMapper extends NumberFieldMapper { if (nullValue == null) { return null; } - return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newIntRange(names.indexName(), precisionStep, nullValue.intValue(), nullValue.intValue(), - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java index 6556aa20bff..549023faa4d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java @@ -26,13 +26,12 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.MultiTermQuery; -import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.RegexpQuery; @@ -42,8 +41,7 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.lucene.search.RegexpFilter; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -202,7 +200,7 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern if (fieldType.indexOptions() != IndexOptions.NONE || context == null) { return super.termFilter(value, context); } - return new TermsFilter(UidFieldMapper.NAME, Uid.createTypeUids(context.queryTypes(), value)); + return Queries.wrap(new TermsQuery(UidFieldMapper.NAME, Uid.createTypeUids(context.queryTypes(), value))); } @Override @@ -210,7 +208,7 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern if (fieldType.indexOptions() != IndexOptions.NONE || context == null) { return super.termsFilter(values, context); } - return new TermsFilter(UidFieldMapper.NAME, Uid.createTypeUids(context.queryTypes(), values)); + return Queries.wrap(new TermsQuery(UidFieldMapper.NAME, Uid.createTypeUids(context.queryTypes(), values))); } @Override @@ -219,13 +217,6 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern return super.prefixQuery(value, method, context); } Collection queryTypes = context.queryTypes(); - if (queryTypes.size() == 1) { - PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value)))); - if (method != null) { - prefixQuery.setRewriteMethod(method); - } - return prefixQuery; - } BooleanQuery query = new BooleanQuery(); for (String queryType : queryTypes) { PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value)))); @@ -243,14 +234,11 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern return super.prefixFilter(value, context); } Collection queryTypes = context.queryTypes(); - if (queryTypes.size() == 1) { - return new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value)))); - } - XBooleanFilter filter = new XBooleanFilter(); + BooleanQuery filter = new BooleanQuery(); for (String queryType : queryTypes) { - filter.add(new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value)))), BooleanClause.Occur.SHOULD); + filter.add(new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value)))), BooleanClause.Occur.SHOULD); } - return filter; + return Queries.wrap(filter); } @Override @@ -284,16 +272,12 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern return super.regexpFilter(value, flags, maxDeterminizedStates, context); } Collection queryTypes = context.queryTypes(); - if (queryTypes.size() == 1) { - return new RegexpFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value))), - flags, maxDeterminizedStates); - } - XBooleanFilter filter = new XBooleanFilter(); + BooleanQuery filter = new BooleanQuery(); for (String queryType : queryTypes) { - filter.add(new RegexpFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value))), + filter.add(new RegexpQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value))), flags, maxDeterminizedStates), BooleanClause.Occur.SHOULD); } - return filter; + return Queries.wrap(filter); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java index 8c6ea1fd8c7..963001cafb2 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java @@ -24,11 +24,11 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; @@ -275,7 +275,7 @@ public class ParentFieldMapper extends AbstractFieldMapper implements Inter } BytesRef bValue = BytesRefs.toBytesRef(value); if (Uid.hasDelimiter(bValue)) { - return new TermFilter(new Term(names.indexName(), bValue)); + return Queries.wrap(new TermQuery(new Term(names.indexName(), bValue))); } List types = new ArrayList<>(context.mapperService().types().size()); @@ -286,16 +286,16 @@ public class ParentFieldMapper extends AbstractFieldMapper implements Inter } if (types.isEmpty()) { - return Queries.MATCH_NO_FILTER; + return Queries.newMatchNoDocsFilter(); } else if (types.size() == 1) { - return new TermFilter(new Term(names.indexName(), Uid.createUidAsBytes(types.get(0), bValue))); + return Queries.wrap(new TermQuery(new Term(names.indexName(), Uid.createUidAsBytes(types.get(0), bValue)))); } else { // we use all non child types, cause we don't know if its exact or not... List typesValues = new ArrayList<>(types.size()); for (String type : context.mapperService().types()) { typesValues.add(Uid.createUidAsBytes(type, bValue)); } - return new TermsFilter(names.indexName(), typesValues); + return Queries.wrap(new TermsQuery(names.indexName(), typesValues)); } } @@ -328,7 +328,7 @@ public class ParentFieldMapper extends AbstractFieldMapper implements Inter } } } - return new TermsFilter(names.indexName(), bValues); + return Queries.wrap(new TermsQuery(names.indexName(), bValues)); } /** diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java index c93a1545aec..206cc3a8c3c 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java @@ -24,16 +24,17 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.PrefixFilter; +import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; @@ -138,9 +139,9 @@ public class TypeFieldMapper extends AbstractFieldMapper implements Inte @Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { if (fieldType.indexOptions() == IndexOptions.NONE) { - return new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))); + return Queries.wrap(new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value))))); } - return new TermFilter(names().createIndexNameTerm(BytesRefs.toBytesRef(value))); + return Queries.wrap(new TermQuery(names().createIndexNameTerm(BytesRefs.toBytesRef(value)))); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java index 40ae5a48c4c..23d373c65ab 100644 --- a/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java @@ -26,7 +26,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -37,6 +36,7 @@ import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -254,10 +254,10 @@ public class IpFieldMapper extends NumberFieldMapper { @Override public Filter rangeFilter(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, lowerTerm == null ? null : parseValue(lowerTerm), upperTerm == null ? null : parseValue(upperTerm), - includeLower, includeUpper); + includeLower, includeUpper)); } @Override @@ -274,10 +274,10 @@ public class IpFieldMapper extends NumberFieldMapper { return null; } final long value = ipToLong(nullValue); - return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, + return Queries.wrap(NumericRangeQuery.newLongRange(names.indexName(), precisionStep, value, value, - true, true); + true, true)); } @Override diff --git a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index 54533dbf195..fab309081ab 100644 --- a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -24,8 +24,8 @@ import com.google.common.collect.Iterables; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchIllegalStateException; import org.elasticsearch.ElasticsearchParseException; @@ -34,6 +34,7 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.CopyOnWriteHashMap; import org.elasticsearch.common.joda.FormatDateTimeFormatter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -387,7 +388,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll, Clonea } this.nestedTypePathAsString = "__" + fullPath; this.nestedTypePathAsBytes = new BytesRef(nestedTypePathAsString); - this.nestedTypeFilter = new TermFilter(new Term(TypeFieldMapper.NAME, nestedTypePathAsBytes)); + this.nestedTypeFilter = Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, nestedTypePathAsBytes))); } @Override diff --git a/src/main/java/org/elasticsearch/index/percolator/PercolatorQueriesRegistry.java b/src/main/java/org/elasticsearch/index/percolator/PercolatorQueriesRegistry.java index bb4c571e3d8..f77c335577d 100644 --- a/src/main/java/org/elasticsearch/index/percolator/PercolatorQueriesRegistry.java +++ b/src/main/java/org/elasticsearch/index/percolator/PercolatorQueriesRegistry.java @@ -20,14 +20,15 @@ package org.elasticsearch.index.percolator; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.CloseableThreadLocal; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -49,8 +50,8 @@ import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.shard.AbstractIndexShardComponent; -import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.IndexShard; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesLifecycle; import org.elasticsearch.percolator.PercolatorService; @@ -281,7 +282,7 @@ public class PercolatorQueriesRegistry extends AbstractIndexShardComponent imple try (Engine.Searcher searcher = shard.acquireSearcher("percolator_load_queries", true)) { Query query = new ConstantScoreQuery( indexCache.filter().cache( - new TermFilter(new Term(TypeFieldMapper.NAME, PercolatorService.TYPE_NAME)), + Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, PercolatorService.TYPE_NAME))), null, queryParserService.autoFilterCachePolicy() ) diff --git a/src/main/java/org/elasticsearch/index/query/AndFilterBuilder.java b/src/main/java/org/elasticsearch/index/query/AndFilterBuilder.java index 687fbade0b7..c69f7c8ef0f 100644 --- a/src/main/java/org/elasticsearch/index/query/AndFilterBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/AndFilterBuilder.java @@ -27,9 +27,9 @@ import java.util.ArrayList; /** * A filter that matches documents matching boolean combinations of other filters. - * - * + * @deprecated Use {@link BoolFilterBuilder} instead */ +@Deprecated public class AndFilterBuilder extends BaseFilterBuilder { private ArrayList filters = Lists.newArrayList(); diff --git a/src/main/java/org/elasticsearch/index/query/AndFilterParser.java b/src/main/java/org/elasticsearch/index/query/AndFilterParser.java index 75f2ee3c49d..176a8c6dd7b 100644 --- a/src/main/java/org/elasticsearch/index/query/AndFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/AndFilterParser.java @@ -19,11 +19,13 @@ package org.elasticsearch.index.query; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.AndFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -54,7 +56,7 @@ public class AndFilterParser implements FilterParser { ArrayList filters = newArrayList(); boolean filtersFound = false; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; @@ -114,7 +116,11 @@ public class AndFilterParser implements FilterParser { } // no need to cache this one - Filter filter = new AndFilter(filters); + BooleanQuery boolQuery = new BooleanQuery(); + for (Filter filter : filters) { + boolQuery.add(filter, Occur.MUST); + } + Filter filter = Queries.wrap(boolQuery); if (cache != null) { filter = parseContext.cacheFilter(filter, cacheKey, cache); } diff --git a/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java b/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java index 8a7af990615..fcd2e68c8b4 100644 --- a/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/BoolFilterParser.java @@ -19,13 +19,13 @@ package org.elasticsearch.index.query; -import org.apache.lucene.queries.FilterClause; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -50,9 +50,9 @@ public class BoolFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - XBooleanFilter boolFilter = new XBooleanFilter(); + BooleanQuery boolFilter = new BooleanQuery(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; @@ -69,19 +69,20 @@ public class BoolFilterParser implements FilterParser { hasAnyFilter = true; Filter filter = parseContext.parseInnerFilter(); if (filter != null) { - boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST)); + boolFilter.add(new BooleanClause(filter, BooleanClause.Occur.FILTER)); } } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) { hasAnyFilter = true; Filter filter = parseContext.parseInnerFilter(); if (filter != null) { - boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT)); + boolFilter.add(new BooleanClause(filter, BooleanClause.Occur.MUST_NOT)); } } else if ("should".equals(currentFieldName)) { hasAnyFilter = true; Filter filter = parseContext.parseInnerFilter(); if (filter != null) { - boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD)); + boolFilter.setMinimumNumberShouldMatch(1); + boolFilter.add(new BooleanClause(filter, BooleanClause.Occur.SHOULD)); } } else { throw new QueryParsingException(parseContext.index(), "[bool] filter does not support [" + currentFieldName + "]"); @@ -92,7 +93,7 @@ public class BoolFilterParser implements FilterParser { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { Filter filter = parseContext.parseInnerFilter(); if (filter != null) { - boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST)); + boolFilter.add(new BooleanClause(filter, BooleanClause.Occur.MUST)); } } } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) { @@ -100,7 +101,7 @@ public class BoolFilterParser implements FilterParser { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { Filter filter = parseContext.parseInnerFilter(); if (filter != null) { - boolFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST_NOT)); + boolFilter.add(new BooleanClause(filter, BooleanClause.Occur.MUST_NOT)); } } } else if ("should".equals(currentFieldName)) { @@ -108,7 +109,8 @@ public class BoolFilterParser implements FilterParser { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { Filter filter = parseContext.parseInnerFilter(); if (filter != null) { - boolFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD)); + boolFilter.setMinimumNumberShouldMatch(1); + boolFilter.add(new BooleanClause(filter, BooleanClause.Occur.SHOULD)); } } } else { @@ -136,7 +138,7 @@ public class BoolFilterParser implements FilterParser { return null; } - Filter filter = boolFilter; + Filter filter = Queries.wrap(boolFilter); if (cache != null) { filter = parseContext.cacheFilter(filter, cacheKey, cache); } diff --git a/src/main/java/org/elasticsearch/index/query/ConstantScoreQueryParser.java b/src/main/java/org/elasticsearch/index/query/ConstantScoreQueryParser.java index 8c4c2dc261e..78c5879b63f 100644 --- a/src/main/java/org/elasticsearch/index/query/ConstantScoreQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/ConstantScoreQueryParser.java @@ -21,8 +21,8 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; @@ -55,7 +55,7 @@ public class ConstantScoreQueryParser implements QueryParser { Query query = null; boolean queryFound = false; float boost = 1.0f; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String currentFieldName = null; diff --git a/src/main/java/org/elasticsearch/index/query/ExistsFilterParser.java b/src/main/java/org/elasticsearch/index/query/ExistsFilterParser.java index dfe42cdfa38..0f3c155ac92 100644 --- a/src/main/java/org/elasticsearch/index/query/ExistsFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/ExistsFilterParser.java @@ -19,16 +19,17 @@ package org.elasticsearch.index.query; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.search.TermRangeFilter; +import org.apache.lucene.search.TermRangeQuery; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper; @@ -95,17 +96,17 @@ public class ExistsFilterParser implements FilterParser { List fields = parseContext.simpleMatchToIndexNames(fieldPattern); if (fields.isEmpty()) { // no fields exists, so we should not match anything - return Queries.MATCH_NO_FILTER; + return Queries.newMatchNoDocsFilter(); } MapperService.SmartNameFieldMappers nonNullFieldMappers = null; - XBooleanFilter boolFilter = new XBooleanFilter(); + BooleanQuery boolFilter = new BooleanQuery(); for (String field : fields) { MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(field); if (smartNameFieldMappers != null) { nonNullFieldMappers = smartNameFieldMappers; } - Filter filter = null; + Query filter = null; if (fieldNamesMapper!= null && fieldNamesMapper.enabled()) { final String f; if (smartNameFieldMappers != null && smartNameFieldMappers.hasMapper()) { @@ -120,14 +121,15 @@ public class ExistsFilterParser implements FilterParser { filter = smartNameFieldMappers.mapper().rangeFilter(null, null, true, true, parseContext); } if (filter == null) { - filter = new TermRangeFilter(field, null, null, true, true); + filter = new TermRangeQuery(field, null, null, true, true); } boolFilter.add(filter, BooleanClause.Occur.SHOULD); } + Filter filter = Queries.wrap(boolFilter); // we always cache this one, really does not change... (exists) // its ok to cache under the fieldName cacheKey, since its per segment and the mapping applies to this data on this segment... - Filter filter = parseContext.cacheFilter(boolFilter, new HashedBytesRef("$exists$" + fieldPattern), parseContext.autoFilterCachePolicy()); + filter = parseContext.cacheFilter(filter, new HashedBytesRef("$exists$" + fieldPattern), parseContext.autoFilterCachePolicy()); if (filterName != null) { parseContext.addNamedFilter(filterName, filter); diff --git a/src/main/java/org/elasticsearch/index/query/FQueryFilterParser.java b/src/main/java/org/elasticsearch/index/query/FQueryFilterParser.java index 37e174ff884..cb821912ca9 100644 --- a/src/main/java/org/elasticsearch/index/query/FQueryFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/FQueryFilterParser.java @@ -20,8 +20,8 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; import org.elasticsearch.common.lucene.search.Queries; @@ -52,7 +52,7 @@ public class FQueryFilterParser implements FilterParser { Query query = null; boolean queryFound = false; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; diff --git a/src/main/java/org/elasticsearch/index/query/FilterBuilders.java b/src/main/java/org/elasticsearch/index/query/FilterBuilders.java index efd48255f1e..3cd63fb6e51 100644 --- a/src/main/java/org/elasticsearch/index/query/FilterBuilders.java +++ b/src/main/java/org/elasticsearch/index/query/FilterBuilders.java @@ -524,10 +524,18 @@ public abstract class FilterBuilders { return new BoolFilterBuilder(); } + /** + * @deprecated Use {@link #boolFilter()} instead + */ + @Deprecated public static AndFilterBuilder andFilter(FilterBuilder... filters) { return new AndFilterBuilder(filters); } + /** + * @deprecated Use {@link #boolFilter()} instead + */ + @Deprecated public static OrFilterBuilder orFilter(FilterBuilder... filters) { return new OrFilterBuilder(filters); } diff --git a/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java b/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java index 562c8e58ae9..e1e27eec64b 100644 --- a/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/FilteredQueryParser.java @@ -19,20 +19,13 @@ package org.elasticsearch.index.query; -import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.ConstantScoreQuery; -import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; import org.apache.lucene.search.FilteredQuery; -import org.apache.lucene.search.FilteredQuery.FilterStrategy; import org.apache.lucene.search.Query; -import org.apache.lucene.search.Scorer; -import org.apache.lucene.search.Weight; -import org.apache.lucene.util.Bits; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.docset.DocIdSets; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; @@ -45,70 +38,6 @@ public class FilteredQueryParser implements QueryParser { public static final String NAME = "filtered"; - public static final FilterStrategy ALWAYS_RANDOM_ACCESS_FILTER_STRATEGY = new CustomRandomAccessFilterStrategy(0); - - public static final CustomRandomAccessFilterStrategy CUSTOM_FILTER_STRATEGY = new CustomRandomAccessFilterStrategy(); - - /** - * Extends {@link org.apache.lucene.search.FilteredQuery.RandomAccessFilterStrategy}. - *

- * Adds a threshold value, which defaults to -1. When set to -1, it will check if the filter docSet is - * *not* a fast docSet, and if not, it will use {@link FilteredQuery#QUERY_FIRST_FILTER_STRATEGY} (since - * the assumption is that its a "slow" filter and better computed only on whatever matched the query). - *

- * If the threshold value is 0, it always tries to pass "down" the filter as acceptDocs, and it the filter - * can't be represented as Bits (never really), then it uses {@link FilteredQuery#LEAP_FROG_QUERY_FIRST_STRATEGY}. - *

- * If the above conditions are not met, then it reverts to the {@link FilteredQuery.RandomAccessFilterStrategy} logic, - * with the threshold used to control {@link #useRandomAccess(org.apache.lucene.util.Bits, int)}. - */ - public static class CustomRandomAccessFilterStrategy extends FilteredQuery.RandomAccessFilterStrategy { - - private final int threshold; - - public CustomRandomAccessFilterStrategy() { - this.threshold = -1; - } - - public CustomRandomAccessFilterStrategy(int threshold) { - this.threshold = threshold; - } - - @Override - public Scorer filteredScorer(LeafReaderContext context, Weight weight, DocIdSet docIdSet) throws IOException { - // CHANGE: If threshold is 0, always pass down the accept docs, don't pay the price of calling nextDoc even... - final Bits filterAcceptDocs = docIdSet.bits(); - if (threshold == 0) { - if (filterAcceptDocs != null) { - return weight.scorer(context, filterAcceptDocs); - } else { - return FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY.filteredScorer(context, weight, docIdSet); - } - } - - // CHANGE: handle "default" value - if (threshold == -1) { - // default value, don't iterate on only apply filter after query if its not a "fast" docIdSet - // TODO: is there a way we could avoid creating an iterator here? - if (filterAcceptDocs != null && DocIdSets.isBroken(docIdSet.iterator())) { - return FilteredQuery.QUERY_FIRST_FILTER_STRATEGY.filteredScorer(context, weight, docIdSet); - } - } - - return super.filteredScorer(context, weight, docIdSet); - } - - @Override - protected boolean useRandomAccess(Bits bits, long filterCost) { - int multiplier = threshold; - if (threshold == -1) { - // default - multiplier = 100; - } - return filterCost * multiplier > bits.length(); - } - } - @Inject public FilteredQueryParser() { } @@ -126,13 +55,13 @@ public class FilteredQueryParser implements QueryParser { Filter filter = null; boolean filterFound = false; float boost = 1.0f; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String queryName = null; String currentFieldName = null; XContentParser.Token token; - FilteredQuery.FilterStrategy filterStrategy = CUSTOM_FILTER_STRATEGY; + FilteredQuery.FilterStrategy filterStrategy = FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -152,15 +81,13 @@ public class FilteredQueryParser implements QueryParser { if ("query_first".equals(value) || "queryFirst".equals(value)) { filterStrategy = FilteredQuery.QUERY_FIRST_FILTER_STRATEGY; } else if ("random_access_always".equals(value) || "randomAccessAlways".equals(value)) { - filterStrategy = ALWAYS_RANDOM_ACCESS_FILTER_STRATEGY; + filterStrategy = FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; } else if ("leap_frog".equals(value) || "leapFrog".equals(value)) { filterStrategy = FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY; } else if (value.startsWith("random_access_")) { - int threshold = Integer.parseInt(value.substring("random_access_".length())); - filterStrategy = new CustomRandomAccessFilterStrategy(threshold); + filterStrategy = FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; } else if (value.startsWith("randomAccess")) { - int threshold = Integer.parseInt(value.substring("randomAccess".length())); - filterStrategy = new CustomRandomAccessFilterStrategy(threshold); + filterStrategy = FilteredQuery.RANDOM_ACCESS_FILTER_STRATEGY; } else if ("leap_frog_query_first".equals(value) || "leapFrogQueryFirst".equals(value)) { filterStrategy = FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY; } else if ("leap_frog_filter_first".equals(value) || "leapFrogFilterFirst".equals(value)) { @@ -197,7 +124,7 @@ public class FilteredQueryParser implements QueryParser { return query; } } - if (filter == Queries.MATCH_ALL_FILTER) { + if (Queries.isConstantMatchAllQuery(filter)) { // this is an instance of match all filter, just execute the query return query; } diff --git a/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java index 2b2ab9d2ef3..8f68dbea074 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; @@ -72,7 +72,7 @@ public class GeoBoundingBoxFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String fieldName = null; diff --git a/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java index 19160fbe3dc..252afdf25cf 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.geo.GeoPoint; @@ -64,7 +64,7 @@ public class GeoDistanceFilterParser implements FilterParser { XContentParser.Token token; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; String currentFieldName = null; diff --git a/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java index 95ef7067400..b7452bec0f1 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.geo.GeoPoint; @@ -64,7 +64,7 @@ public class GeoDistanceRangeFilterParser implements FilterParser { XContentParser.Token token; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; String currentFieldName = null; diff --git a/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java index ba8b2291c53..fefa37c07e3 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java @@ -22,7 +22,7 @@ package org.elasticsearch.index.query; import com.google.common.collect.Lists; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.inject.Inject; @@ -68,7 +68,7 @@ public class GeoPolygonFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String fieldName = null; diff --git a/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java index f7aa4b72986..72eba62854e 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java @@ -22,8 +22,9 @@ package org.elasticsearch.index.query; import com.spatial4j.core.shape.Shape; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.spatial.prefix.PrefixTreeStrategy; import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy; import org.elasticsearch.common.geo.ShapeRelation; @@ -31,7 +32,7 @@ import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.internal.Nullable; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; @@ -84,7 +85,7 @@ public class GeoShapeFilterParser implements FilterParser { ShapeRelation shapeRelation = ShapeRelation.INTERSECTS; String strategyName = null; ShapeBuilder shape = null; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; @@ -183,12 +184,12 @@ public class GeoShapeFilterParser implements FilterParser { if (strategy instanceof RecursivePrefixTreeStrategy && shapeRelation == ShapeRelation.DISJOINT) { // this strategy doesn't support disjoint anymore: but it did before, including creating lucene fieldcache (!) // in this case, execute disjoint as exists && !intersects - XBooleanFilter bool = new XBooleanFilter(); + BooleanQuery bool = new BooleanQuery(); Filter exists = ExistsFilterParser.newFilter(parseContext, fieldName, null); Filter intersects = strategy.makeFilter(GeoShapeQueryParser.getArgs(shape, ShapeRelation.INTERSECTS)); bool.add(exists, BooleanClause.Occur.MUST); bool.add(intersects, BooleanClause.Occur.MUST_NOT); - filter = bool; + filter = Queries.wrap(bool); } else { filter = strategy.makeFilter(GeoShapeQueryParser.getArgs(shape, shapeRelation)); } diff --git a/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java b/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java index a7190443ed7..ac732bc99aa 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; @@ -33,7 +34,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; @@ -161,7 +161,7 @@ public class GeoShapeQueryParser implements QueryParser { if (strategy instanceof RecursivePrefixTreeStrategy && shapeRelation == ShapeRelation.DISJOINT) { // this strategy doesn't support disjoint anymore: but it did before, including creating lucene fieldcache (!) // in this case, execute disjoint as exists && !intersects - XBooleanFilter bool = new XBooleanFilter(); + BooleanQuery bool = new BooleanQuery(); Filter exists = ExistsFilterParser.newFilter(parseContext, fieldName, null); Filter intersects = strategy.makeFilter(getArgs(shape, ShapeRelation.INTERSECTS)); bool.add(exists, BooleanClause.Occur.MUST); diff --git a/src/main/java/org/elasticsearch/index/query/GeohashCellFilter.java b/src/main/java/org/elasticsearch/index/query/GeohashCellFilter.java index d8b54c31988..9e69bc25a89 100644 --- a/src/main/java/org/elasticsearch/index/query/GeohashCellFilter.java +++ b/src/main/java/org/elasticsearch/index/query/GeohashCellFilter.java @@ -20,7 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Nullable; @@ -215,7 +215,7 @@ public class GeohashCellFilter { String geohash = null; int levels = -1; boolean neighbors = false; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; diff --git a/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java b/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java index a44d5a69917..2325d2840e8 100644 --- a/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java @@ -19,14 +19,15 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.search.NotFilter; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; import org.elasticsearch.index.mapper.DocumentMapper; @@ -181,14 +182,14 @@ public class HasParentQueryParser implements QueryParser { parentFilter = documentMapper.typeFilter(); } } else { - XBooleanFilter parentsFilter = new XBooleanFilter(); + BooleanQuery parentsFilter = new BooleanQuery(); for (String parentTypeStr : parentTypes) { DocumentMapper documentMapper = parseContext.mapperService().documentMapper(parentTypeStr); if (documentMapper != null) { parentsFilter.add(documentMapper.typeFilter(), BooleanClause.Occur.SHOULD); } } - parentFilter = parentsFilter; + parentFilter = Queries.wrap(parentsFilter); } if (parentFilter == null) { @@ -197,7 +198,7 @@ public class HasParentQueryParser implements QueryParser { // wrap the query with type query innerQuery = new FilteredQuery(innerQuery, parseContext.cacheFilter(parentDocMapper.typeFilter(), null, parseContext.autoFilterCachePolicy())); - Filter childrenFilter = parseContext.cacheFilter(new NotFilter(parentFilter), null, parseContext.autoFilterCachePolicy()); + Filter childrenFilter = parseContext.cacheFilter(Queries.wrap(Queries.not(parentFilter)), null, parseContext.autoFilterCachePolicy()); if (score) { return new ParentQuery(parentChildIndexFieldData, innerQuery, parentDocMapper.type(), childrenFilter); } else { diff --git a/src/main/java/org/elasticsearch/index/query/IdsFilterParser.java b/src/main/java/org/elasticsearch/index/query/IdsFilterParser.java index c2bfb7505b8..d0402aabf95 100644 --- a/src/main/java/org/elasticsearch/index/query/IdsFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/IdsFilterParser.java @@ -21,7 +21,8 @@ package org.elasticsearch.index.query; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import org.apache.lucene.queries.TermsFilter; + +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.inject.Inject; @@ -99,7 +100,7 @@ public class IdsFilterParser implements FilterParser { } if (ids.isEmpty()) { - return Queries.MATCH_NO_FILTER; + return Queries.newMatchNoDocsFilter(); } if (types == null || types.isEmpty()) { @@ -108,7 +109,7 @@ public class IdsFilterParser implements FilterParser { types = parseContext.mapperService().types(); } - TermsFilter filter = new TermsFilter(UidFieldMapper.NAME, Uid.createTypeUids(types, ids)); + Filter filter = Queries.wrap(new TermsQuery(UidFieldMapper.NAME, Uid.createTypeUids(types, ids))); if (filterName != null) { parseContext.addNamedFilter(filterName, filter); } diff --git a/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java b/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java index fc8f2b7103a..d0345944c66 100644 --- a/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java @@ -21,8 +21,8 @@ package org.elasticsearch.index.query; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import org.apache.lucene.queries.TermsFilter; -import org.apache.lucene.search.ConstantScoreQuery; + +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.inject.Inject; @@ -121,9 +121,7 @@ public class IdsQueryParser implements QueryParser { types = parseContext.mapperService().types(); } - TermsFilter filter = new TermsFilter(UidFieldMapper.NAME, Uid.createTypeUids(types, ids)); - // no need for constant score filter, since we don't cache the filter, and it always takes deletes into account - ConstantScoreQuery query = new ConstantScoreQuery(filter); + TermsQuery query = new TermsQuery(UidFieldMapper.NAME, Uid.createTypeUids(types, ids)); query.setBoost(boost); if (queryName != null) { parseContext.addNamedQuery(queryName, query); diff --git a/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java b/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java index 80002710c84..348e9cacba1 100644 --- a/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java +++ b/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java @@ -22,8 +22,8 @@ package org.elasticsearch.index.query; import com.google.common.collect.ImmutableMap; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.util.CloseableThreadLocal; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; @@ -94,7 +94,7 @@ public class IndexQueryParserService extends AbstractIndexComponent { final BitsetFilterCache bitsetFilterCache; - final FilterCachingPolicy autoFilterCachePolicy; + final QueryCachingPolicy autoFilterCachePolicy; private final Map queryParsers; @@ -111,7 +111,7 @@ public class IndexQueryParserService extends AbstractIndexComponent { ScriptService scriptService, AnalysisService analysisService, MapperService mapperService, IndexCache indexCache, IndexFieldDataService fieldDataService, BitsetFilterCache bitsetFilterCache, - FilterCachingPolicy autoFilterCachePolicy, + QueryCachingPolicy autoFilterCachePolicy, @Nullable SimilarityService similarityService, @Nullable Map namedQueryParsers, @Nullable Map namedFilterParsers) { @@ -185,7 +185,7 @@ public class IndexQueryParserService extends AbstractIndexComponent { return this.defaultField; } - public FilterCachingPolicy autoFilterCachePolicy() { + public QueryCachingPolicy autoFilterCachePolicy() { return autoFilterCachePolicy; } diff --git a/src/main/java/org/elasticsearch/index/query/IndicesFilterParser.java b/src/main/java/org/elasticsearch/index/query/IndicesFilterParser.java index d1dee834c16..c1f5b804f94 100644 --- a/src/main/java/org/elasticsearch/index/query/IndicesFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/IndicesFilterParser.java @@ -56,7 +56,7 @@ public class IndicesFilterParser implements FilterParser { XContentParser parser = parseContext.parser(); Filter filter = null; - Filter noMatchFilter = Queries.MATCH_ALL_FILTER; + Filter noMatchFilter = Queries.newMatchAllFilter(); boolean filterFound = false; boolean indicesFound = false; boolean currentIndexMatchesIndices = false; @@ -113,9 +113,9 @@ public class IndicesFilterParser implements FilterParser { } else if ("no_match_filter".equals(currentFieldName)) { String type = parser.text(); if ("all".equals(type)) { - noMatchFilter = Queries.MATCH_ALL_FILTER; + noMatchFilter = Queries.newMatchAllFilter(); } else if ("none".equals(type)) { - noMatchFilter = Queries.MATCH_NO_FILTER; + noMatchFilter = Queries.newMatchNoDocsFilter(); } } else if ("_name".equals(currentFieldName)) { filterName = parser.text(); diff --git a/src/main/java/org/elasticsearch/index/query/LimitFilterParser.java b/src/main/java/org/elasticsearch/index/query/LimitFilterParser.java index f22308f058f..858b23c6693 100644 --- a/src/main/java/org/elasticsearch/index/query/LimitFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/LimitFilterParser.java @@ -63,6 +63,6 @@ public class LimitFilterParser implements FilterParser { } // this filter is deprecated and parses to a filter that matches everything - return Queries.MATCH_ALL_FILTER; + return Queries.newMatchAllFilter(); } } diff --git a/src/main/java/org/elasticsearch/index/query/MatchAllFilterParser.java b/src/main/java/org/elasticsearch/index/query/MatchAllFilterParser.java index a10f37d8f8b..2d78edad685 100644 --- a/src/main/java/org/elasticsearch/index/query/MatchAllFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/MatchAllFilterParser.java @@ -51,6 +51,6 @@ public class MatchAllFilterParser implements FilterParser { while (((token = parser.nextToken()) != XContentParser.Token.END_OBJECT && token != XContentParser.Token.END_ARRAY)) { } - return Queries.MATCH_ALL_FILTER; + return Queries.newMatchAllFilter(); } } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/index/query/MissingFilterParser.java b/src/main/java/org/elasticsearch/index/query/MissingFilterParser.java index ee3c306a24b..44341fd3ef4 100644 --- a/src/main/java/org/elasticsearch/index/query/MissingFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/MissingFilterParser.java @@ -19,15 +19,15 @@ package org.elasticsearch.index.query; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.TermRangeFilter; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; +import org.apache.lucene.search.TermRangeQuery; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.NotFilter; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.MapperService; @@ -107,7 +107,7 @@ public class MissingFilterParser implements FilterParser { if (fields.isEmpty()) { if (existence) { // if we ask for existence of fields, and we found none, then we should match on all - return Queries.MATCH_ALL_FILTER; + return Queries.newMatchAllFilter(); } return null; } @@ -118,13 +118,13 @@ public class MissingFilterParser implements FilterParser { MapperService.SmartNameFieldMappers nonNullFieldMappers = null; if (existence) { - XBooleanFilter boolFilter = new XBooleanFilter(); + BooleanQuery boolFilter = new BooleanQuery(); for (String field : fields) { MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(field); if (smartNameFieldMappers != null) { nonNullFieldMappers = smartNameFieldMappers; } - Filter filter = null; + Query filter = null; if (fieldNamesMapper != null && fieldNamesMapper.enabled()) { final String f; if (smartNameFieldMappers != null && smartNameFieldMappers.hasMapper()) { @@ -139,15 +139,16 @@ public class MissingFilterParser implements FilterParser { filter = smartNameFieldMappers.mapper().rangeFilter(null, null, true, true, parseContext); } if (filter == null) { - filter = new TermRangeFilter(field, null, null, true, true); + filter = new TermRangeQuery(field, null, null, true, true); } boolFilter.add(filter, BooleanClause.Occur.SHOULD); } // we always cache this one, really does not change... (exists) // its ok to cache under the fieldName cacheKey, since its per segment and the mapping applies to this data on this segment... - existenceFilter = parseContext.cacheFilter(boolFilter, new HashedBytesRef("$exists$" + fieldPattern), parseContext.autoFilterCachePolicy()); - existenceFilter = new NotFilter(existenceFilter); + existenceFilter = Queries.wrap(boolFilter); + existenceFilter = parseContext.cacheFilter(existenceFilter, new HashedBytesRef("$exists$" + fieldPattern), parseContext.autoFilterCachePolicy()); + existenceFilter = Queries.wrap(Queries.not(existenceFilter)); // cache the not filter as well, so it will be faster existenceFilter = parseContext.cacheFilter(existenceFilter, new HashedBytesRef("$missing$" + fieldPattern), parseContext.autoFilterCachePolicy()); } @@ -168,11 +169,11 @@ public class MissingFilterParser implements FilterParser { Filter filter; if (nullFilter != null) { if (existenceFilter != null) { - XBooleanFilter combined = new XBooleanFilter(); + BooleanQuery combined = new BooleanQuery(); combined.add(existenceFilter, BooleanClause.Occur.SHOULD); combined.add(nullFilter, BooleanClause.Occur.SHOULD); // cache the not filter as well, so it will be faster - filter = parseContext.cacheFilter(combined, null, parseContext.autoFilterCachePolicy()); + filter = parseContext.cacheFilter(Queries.wrap(combined), null, parseContext.autoFilterCachePolicy()); } else { filter = nullFilter; } diff --git a/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java b/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java index 1a065d1d909..9ef53961a9e 100644 --- a/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java @@ -21,11 +21,11 @@ package org.elasticsearch.index.query; import com.google.common.collect.Lists; import com.google.common.collect.Sets; + import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchIllegalArgumentException; @@ -350,8 +350,7 @@ public class MoreLikeThisQueryParser implements QueryParser { uids.add(createUidAsBytes(item.type(), item.id())); } if (!uids.isEmpty()) { - TermsFilter filter = new TermsFilter(UidFieldMapper.NAME, uids.toArray(new BytesRef[0])); - ConstantScoreQuery query = new ConstantScoreQuery(filter); + TermsQuery query = new TermsQuery(UidFieldMapper.NAME, uids.toArray(new BytesRef[0])); boolQuery.add(query, BooleanClause.Occur.MUST_NOT); } } diff --git a/src/main/java/org/elasticsearch/index/query/NotFilterParser.java b/src/main/java/org/elasticsearch/index/query/NotFilterParser.java index 3eb94b99d62..db8adccc5dd 100644 --- a/src/main/java/org/elasticsearch/index/query/NotFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/NotFilterParser.java @@ -20,9 +20,10 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.QueryWrapperFilter; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -92,7 +93,7 @@ public class NotFilterParser implements FilterParser { return null; } - Filter notFilter = new NotFilter(filter); + Filter notFilter = Queries.wrap(Queries.not(filter)); if (cache) { notFilter = parseContext.cacheFilter(notFilter, cacheKey, parseContext.autoFilterCachePolicy()); } diff --git a/src/main/java/org/elasticsearch/index/query/OrFilterBuilder.java b/src/main/java/org/elasticsearch/index/query/OrFilterBuilder.java index 9a68776fbb7..04d516b00b7 100644 --- a/src/main/java/org/elasticsearch/index/query/OrFilterBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/OrFilterBuilder.java @@ -27,9 +27,9 @@ import java.util.ArrayList; /** * A filter that matches documents matching boolean combinations of other filters. - * - * + * @deprecated Use {@link BoolFilterBuilder} instead */ +@Deprecated public class OrFilterBuilder extends BaseFilterBuilder { private ArrayList filters = Lists.newArrayList(); diff --git a/src/main/java/org/elasticsearch/index/query/OrFilterParser.java b/src/main/java/org/elasticsearch/index/query/OrFilterParser.java index 2009dd13fa9..9c3ad615105 100644 --- a/src/main/java/org/elasticsearch/index/query/OrFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/OrFilterParser.java @@ -19,11 +19,13 @@ package org.elasticsearch.index.query; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.OrFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -54,7 +56,7 @@ public class OrFilterParser implements FilterParser { ArrayList filters = newArrayList(); boolean filtersFound = false; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String filterName = null; @@ -113,7 +115,11 @@ public class OrFilterParser implements FilterParser { } // no need to cache this one - Filter filter = new OrFilter(filters); + BooleanQuery boolQuery = new BooleanQuery(); + for (Filter filter : filters) { + boolQuery.add(filter, Occur.SHOULD); + } + Filter filter = Queries.wrap(boolQuery); if (cache != null) { filter = parseContext.cacheFilter(filter, cacheKey, cache); } diff --git a/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java b/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java index 3f0ca979e6f..e6bc4e3437f 100644 --- a/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/PrefixFilterParser.java @@ -21,11 +21,12 @@ package org.elasticsearch.index.query; import org.apache.lucene.index.Term; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; -import org.apache.lucene.search.PrefixFilter; +import org.apache.lucene.search.PrefixQuery; +import org.apache.lucene.search.QueryCachingPolicy; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.HashedBytesRef; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.MapperService; @@ -51,7 +52,7 @@ public class PrefixFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String fieldName = null; Object value = null; @@ -87,7 +88,7 @@ public class PrefixFilterParser implements FilterParser { filter = smartNameFieldMappers.mapper().prefixFilter(value, parseContext); } if (filter == null) { - filter = new PrefixFilter(new Term(fieldName, BytesRefs.toBytesRef(value))); + filter = Queries.wrap(new PrefixQuery(new Term(fieldName, BytesRefs.toBytesRef(value)))); } if (cache != null) { diff --git a/src/main/java/org/elasticsearch/index/query/QueryParseContext.java b/src/main/java/org/elasticsearch/index/query/QueryParseContext.java index fe6292735ca..fb55b7b818f 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryParseContext.java +++ b/src/main/java/org/elasticsearch/index/query/QueryParseContext.java @@ -27,8 +27,8 @@ import org.apache.lucene.queryparser.classic.MapperQueryParser; import org.apache.lucene.queryparser.classic.QueryParserSettings; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.util.Bits; @@ -190,11 +190,11 @@ public class QueryParseContext { return indexQueryParser.defaultField(); } - public FilterCachingPolicy autoFilterCachePolicy() { + public QueryCachingPolicy autoFilterCachePolicy() { return indexQueryParser.autoFilterCachePolicy(); } - public FilterCachingPolicy parseFilterCachePolicy() throws IOException { + public QueryCachingPolicy parseFilterCachePolicy() throws IOException { final String text = parser.textOrNull(); if (text == null || text.equals("auto")) { return autoFilterCachePolicy(); @@ -202,7 +202,7 @@ public class QueryParseContext { // cache without conditions on how many times the filter has been // used or what the produced DocIdSet looks like, but ONLY on large // segments to not pollute the cache - return FilterCachingPolicy.CacheOnLargeSegments.DEFAULT; + return QueryCachingPolicy.CacheOnLargeSegments.DEFAULT; } else { return null; } @@ -221,7 +221,7 @@ public class QueryParseContext { return indexQueryParser.bitsetFilterCache.getBitDocIdSetFilter(filter); } - public Filter cacheFilter(Filter filter, final @Nullable HashedBytesRef cacheKey, final FilterCachingPolicy cachePolicy) { + public Filter cacheFilter(Filter filter, final @Nullable HashedBytesRef cacheKey, final QueryCachingPolicy cachePolicy) { if (filter == null) { return null; } diff --git a/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java b/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java index bf6144c90b1..300ed66e6d8 100644 --- a/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/RangeFilterParser.java @@ -20,13 +20,14 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; -import org.apache.lucene.search.TermRangeFilter; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.TermRangeQuery; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.HashedBytesRef; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; @@ -56,7 +57,7 @@ public class RangeFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String fieldName = null; Object from = null; @@ -167,7 +168,7 @@ public class RangeFilterParser implements FilterParser { } if (filter == null) { - filter = new TermRangeFilter(fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper); + filter = Queries.wrap(new TermRangeQuery(fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper)); } if (cache != null) { diff --git a/src/main/java/org/elasticsearch/index/query/RegexpFilterParser.java b/src/main/java/org/elasticsearch/index/query/RegexpFilterParser.java index 8109bd4e471..76db069af17 100644 --- a/src/main/java/org/elasticsearch/index/query/RegexpFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/RegexpFilterParser.java @@ -21,12 +21,13 @@ package org.elasticsearch.index.query; import org.apache.lucene.index.Term; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.util.automaton.Operations; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.RegexpFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.MapperService; @@ -52,7 +53,7 @@ public class RegexpFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String fieldName = null; String secondaryFieldName = null; @@ -117,7 +118,7 @@ public class RegexpFilterParser implements FilterParser { filter = smartNameFieldMappers.mapper().regexpFilter(value, flagsValue, maxDeterminizedStates, parseContext); } if (filter == null) { - filter = new RegexpFilter(new Term(fieldName, BytesRefs.toBytesRef(value)), flagsValue, maxDeterminizedStates); + filter = Queries.wrap(new RegexpQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), flagsValue, maxDeterminizedStates)); } if (cache != null) { diff --git a/src/main/java/org/elasticsearch/index/query/ScriptFilterParser.java b/src/main/java/org/elasticsearch/index/query/ScriptFilterParser.java index 57948092738..8ada496be08 100644 --- a/src/main/java/org/elasticsearch/index/query/ScriptFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/ScriptFilterParser.java @@ -24,15 +24,16 @@ import org.apache.lucene.search.BitsFilteredDocIdSet; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocValuesDocIdSet; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.util.Bits; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.HashedBytesRef; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.script.LeafSearchScript; +import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptParameterParser; -import org.elasticsearch.script.*; import org.elasticsearch.script.ScriptParameterParser.ScriptParameterValue; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.SearchScript; @@ -66,7 +67,7 @@ public class ScriptFilterParser implements FilterParser { XContentParser.Token token; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; // also, when caching, since its isCacheable is false, will result in loading all bit set... String script = null; diff --git a/src/main/java/org/elasticsearch/index/query/TermFilterParser.java b/src/main/java/org/elasticsearch/index/query/TermFilterParser.java index 429def2c2a6..f03a8a43cae 100644 --- a/src/main/java/org/elasticsearch/index/query/TermFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/TermFilterParser.java @@ -20,12 +20,13 @@ package org.elasticsearch.index.query; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.HashedBytesRef; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.MapperService; @@ -51,7 +52,7 @@ public class TermFilterParser implements FilterParser { public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); HashedBytesRef cacheKey = null; String fieldName = null; Object value = null; @@ -112,7 +113,7 @@ public class TermFilterParser implements FilterParser { filter = smartNameFieldMappers.mapper().termFilter(value, parseContext); } if (filter == null) { - filter = new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(value))); + filter = Queries.wrap(new TermQuery(new Term(fieldName, BytesRefs.toBytesRef(value)))); } if (cache != null) { diff --git a/src/main/java/org/elasticsearch/index/query/TermsFilterBuilder.java b/src/main/java/org/elasticsearch/index/query/TermsFilterBuilder.java index f259a84eb11..a6331fb51a6 100644 --- a/src/main/java/org/elasticsearch/index/query/TermsFilterBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/TermsFilterBuilder.java @@ -118,7 +118,9 @@ public class TermsFilterBuilder extends BaseFilterBuilder { /** * Sets the execution mode for the terms filter. Cane be either "plain", "bool" * "and". Defaults to "plain". + * @deprecated elasticsearch now makes better decisions on its own */ + @Deprecated public TermsFilterBuilder execution(String execution) { this.execution = execution; return this; diff --git a/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java b/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java index 2a5a7e02bf2..3c5ecd15106 100644 --- a/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java @@ -21,12 +21,9 @@ package org.elasticsearch.index.query; import com.google.common.collect.Lists; -import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; -import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.util.BytesRef; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; @@ -34,10 +31,7 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.HashedBytesRef; -import org.elasticsearch.common.lucene.search.AndFilter; -import org.elasticsearch.common.lucene.search.OrFilter; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.mapper.FieldMapper; @@ -55,15 +49,8 @@ public class TermsFilterParser implements FilterParser { public static final String NAME = "terms"; private Client client; + @Deprecated public static final String EXECUTION_KEY = "execution"; - public static final String EXECUTION_VALUE_PLAIN = "plain"; - public static final String EXECUTION_VALUE_FIELDDATA = "fielddata"; - public static final String EXECUTION_VALUE_BOOL = "bool"; - public static final String EXECUTION_VALUE_BOOL_NOCACHE = "bool_nocache"; - public static final String EXECUTION_VALUE_AND = "and"; - public static final String EXECUTION_VALUE_AND_NOCACHE = "and_nocache"; - public static final String EXECUTION_VALUE_OR = "or"; - public static final String EXECUTION_VALUE_OR_NOCACHE = "or_nocache"; @Inject public TermsFilterParser() { @@ -84,7 +71,7 @@ public class TermsFilterParser implements FilterParser { XContentParser parser = parseContext.parser(); MapperService.SmartNameFieldMappers smartNameFieldMappers; - FilterCachingPolicy cache = parseContext.autoFilterCachePolicy(); + QueryCachingPolicy cache = parseContext.autoFilterCachePolicy(); String filterName = null; String currentFieldName = null; @@ -96,7 +83,6 @@ public class TermsFilterParser implements FilterParser { HashedBytesRef cacheKey = null; XContentParser.Token token; - String execution = EXECUTION_VALUE_PLAIN; List terms = Lists.newArrayList(); String fieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { @@ -147,7 +133,7 @@ public class TermsFilterParser implements FilterParser { } } else if (token.isValue()) { if (EXECUTION_KEY.equals(currentFieldName)) { - execution = parser.text(); + // ignore } else if ("_name".equals(currentFieldName)) { filterName = parser.text(); } else if ("_cache".equals(currentFieldName)) { @@ -183,111 +169,27 @@ public class TermsFilterParser implements FilterParser { } if (terms.isEmpty()) { - return Queries.MATCH_NO_FILTER; + return Queries.newMatchNoDocsFilter(); } - - Filter filter; - if (EXECUTION_VALUE_PLAIN.equals(execution)) { - if (fieldMapper != null) { - filter = fieldMapper.termsFilter(terms, parseContext); - } else { - BytesRef[] filterValues = new BytesRef[terms.size()]; - for (int i = 0; i < filterValues.length; i++) { - filterValues[i] = BytesRefs.toBytesRef(terms.get(i)); - } - filter = new TermsFilter(fieldName, filterValues); - } - } else if (EXECUTION_VALUE_FIELDDATA.equals(execution)) { - // if there are no mappings, then nothing has been indexing yet against this shard, so we can return - // no match (but not cached!), since the FieldDataTermsFilter relies on a mapping... - if (fieldMapper == null) { - return Queries.MATCH_NO_FILTER; - } - - filter = fieldMapper.fieldDataTermsFilter(terms, parseContext); - } else if (EXECUTION_VALUE_BOOL.equals(execution)) { - XBooleanFilter boolFiler = new XBooleanFilter(); - if (fieldMapper != null) { - for (Object term : terms) { - boolFiler.add(parseContext.cacheFilter(fieldMapper.termFilter(term, parseContext), null, parseContext.autoFilterCachePolicy()), BooleanClause.Occur.SHOULD); - } - } else { - for (Object term : terms) { - boolFiler.add(parseContext.cacheFilter(new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(term))), null, parseContext.autoFilterCachePolicy()), BooleanClause.Occur.SHOULD); - } - } - filter = boolFiler; - } else if (EXECUTION_VALUE_BOOL_NOCACHE.equals(execution)) { - XBooleanFilter boolFiler = new XBooleanFilter(); - if (fieldMapper != null) { - for (Object term : terms) { - boolFiler.add(fieldMapper.termFilter(term, parseContext), BooleanClause.Occur.SHOULD); - } - } else { - for (Object term : terms) { - boolFiler.add(new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(term))), BooleanClause.Occur.SHOULD); - } - } - filter = boolFiler; - } else if (EXECUTION_VALUE_AND.equals(execution)) { - List filters = Lists.newArrayList(); - if (fieldMapper != null) { - for (Object term : terms) { - filters.add(parseContext.cacheFilter(fieldMapper.termFilter(term, parseContext), null, parseContext.autoFilterCachePolicy())); - } - } else { - for (Object term : terms) { - filters.add(parseContext.cacheFilter(new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(term))), null, parseContext.autoFilterCachePolicy())); - } - } - filter = new AndFilter(filters); - } else if (EXECUTION_VALUE_AND_NOCACHE.equals(execution)) { - List filters = Lists.newArrayList(); - if (fieldMapper != null) { - for (Object term : terms) { - filters.add(fieldMapper.termFilter(term, parseContext)); - } - } else { - for (Object term : terms) { - filters.add(new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(term)))); - } - } - filter = new AndFilter(filters); - } else if (EXECUTION_VALUE_OR.equals(execution)) { - List filters = Lists.newArrayList(); - if (fieldMapper != null) { - for (Object term : terms) { - filters.add(parseContext.cacheFilter(fieldMapper.termFilter(term, parseContext), null, parseContext.autoFilterCachePolicy())); - } - } else { - for (Object term : terms) { - filters.add(parseContext.cacheFilter(new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(term))), null, parseContext.autoFilterCachePolicy())); - } - } - filter = new OrFilter(filters); - } else if (EXECUTION_VALUE_OR_NOCACHE.equals(execution)) { - List filters = Lists.newArrayList(); - if (fieldMapper != null) { - for (Object term : terms) { - filters.add(fieldMapper.termFilter(term, parseContext)); - } - } else { - for (Object term : terms) { - filters.add(new TermFilter(new Term(fieldName, BytesRefs.toBytesRef(term)))); - } - } - filter = new OrFilter(filters); - } else { - throw new QueryParsingException(parseContext.index(), "terms filter execution value [" + execution + "] not supported"); + + Filter filter; + if (fieldMapper != null) { + filter = fieldMapper.termsFilter(terms, parseContext); + } else { + BytesRef[] filterValues = new BytesRef[terms.size()]; + for (int i = 0; i < filterValues.length; i++) { + filterValues[i] = BytesRefs.toBytesRef(terms.get(i)); } - - if (cache != null) { - filter = parseContext.cacheFilter(filter, cacheKey, cache); - } - - if (filterName != null) { - parseContext.addNamedFilter(filterName, filter); - } - return filter; + filter = Queries.wrap(new TermsQuery(fieldName, filterValues)); + } + + if (cache != null) { + filter = parseContext.cacheFilter(filter, cacheKey, cache); + } + + if (filterName != null) { + parseContext.addNamedFilter(filterName, filter); + } + return filter; } } diff --git a/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java b/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java index 220f59cdaa2..e4ae0b957e0 100644 --- a/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/TypeFilterParser.java @@ -20,10 +20,11 @@ package org.elasticsearch.index.query; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; @@ -67,7 +68,7 @@ public class TypeFilterParser implements FilterParser { //LUCENE 4 UPGRADE document mapper should use bytesref as well? DocumentMapper documentMapper = parseContext.mapperService().documentMapper(type.utf8ToString()); if (documentMapper == null) { - filter = new TermFilter(new Term(TypeFieldMapper.NAME, type)); + filter = Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, type))); } else { filter = documentMapper.typeFilter(); } diff --git a/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryParser.java b/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryParser.java index 92bfd31035c..10d4c7f3d55 100644 --- a/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryParser.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.query.functionscore; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; + import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; @@ -29,7 +30,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.search.MatchAllDocsFilter; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.function.CombineFunction; import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery; @@ -165,7 +165,7 @@ public class FunctionScoreQueryParser implements QueryParser { } // handle cases where only one score function and no filter was // provided. In this case we create a FunctionScoreQuery. - if (filterFunctions.size() == 0 || filterFunctions.size() == 1 && (filterFunctions.get(0).filter == null || filterFunctions.get(0).filter instanceof MatchAllDocsFilter)) { + if (filterFunctions.size() == 0 || filterFunctions.size() == 1 && (filterFunctions.get(0).filter == null || Queries.isConstantMatchAllQuery(filterFunctions.get(0).filter))) { ScoreFunction function = filterFunctions.size() == 0 ? null : filterFunctions.get(0).function; FunctionScoreQuery theQuery = new FunctionScoreQuery(query, function, minScore); if (combineFunction != null) { @@ -227,7 +227,7 @@ public class FunctionScoreQueryParser implements QueryParser { } } if (filter == null) { - filter = Queries.MATCH_ALL_FILTER; + filter = Queries.newMatchAllFilter(); } if (scoreFunction == null) { throw new ElasticsearchParseException("function_score: One entry in functions list is missing a function."); diff --git a/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java b/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java index a2e60522f1c..86b9e371784 100644 --- a/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java +++ b/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java @@ -26,7 +26,6 @@ import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.lucene.search.AndFilter; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryParseContext; diff --git a/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java b/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java index 9c8569ad952..db1c15ddbb6 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQuery.java @@ -24,6 +24,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.Term; import org.apache.lucene.search.BitsFilteredDocIdSet; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.CollectionTerminatedException; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; @@ -105,7 +106,7 @@ public class ChildrenConstantScoreQuery extends Query { final long valueCount; List leaves = searcher.getIndexReader().leaves(); if (globalIfd == null || leaves.isEmpty()) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } else { AtomicParentChildFieldData afd = globalIfd.load(leaves.get(0)); SortedDocValues globalValues = afd.getOrdinalsValues(parentType); @@ -113,7 +114,7 @@ public class ChildrenConstantScoreQuery extends Query { } if (valueCount == 0) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } Query childQuery = rewrittenChildQuery; @@ -124,7 +125,7 @@ public class ChildrenConstantScoreQuery extends Query { final long remaining = collector.foundParents(); if (remaining == 0) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } Filter shortCircuitFilter = null; diff --git a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java index 3b6c62fef45..e6f3069818d 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java @@ -23,6 +23,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.Term; import org.apache.lucene.search.BitsFilteredDocIdSet; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.CollectionTerminatedException; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; @@ -170,7 +171,7 @@ public class ChildrenQuery extends Query { IndexParentChildFieldData globalIfd = ifd.loadGlobal(searcher.getIndexReader()); if (globalIfd == null) { // No docs of the specified type exist on this shard - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); indexSearcher.setSimilarity(searcher.getSimilarity()); @@ -215,7 +216,7 @@ public class ChildrenQuery extends Query { indexSearcher.search(childQuery, collector); numFoundParents = collector.foundParents(); if (numFoundParents == 0) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } abort = false; } finally { diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java b/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java index bd6ea3d44d8..3617ab29a89 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentConstantScoreQuery.java @@ -85,7 +85,7 @@ public class ParentConstantScoreQuery extends Query { final long maxOrd; List leaves = searcher.getIndexReader().leaves(); if (globalIfd == null || leaves.isEmpty()) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } else { AtomicParentChildFieldData afd = globalIfd.load(leaves.get(0)); SortedDocValues globalValues = afd.getOrdinalsValues(parentType); @@ -93,7 +93,7 @@ public class ParentConstantScoreQuery extends Query { } if (maxOrd == 0) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } final Query parentQuery = rewrittenParentQuery; @@ -103,7 +103,7 @@ public class ParentConstantScoreQuery extends Query { indexSearcher.search(parentQuery, collector); if (collector.parentCount() == 0) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } return new ChildrenWeight(this, childrenFilter, collector, globalIfd); diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentIdsFilter.java b/src/main/java/org/elasticsearch/index/search/child/ParentIdsFilter.java index 342ca4d3045..0b437a83b9e 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentIdsFilter.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentIdsFilter.java @@ -24,10 +24,12 @@ import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.Term; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; -import org.apache.lucene.queries.TermFilter; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.util.BitDocIdSet; import org.apache.lucene.util.BitSet; @@ -38,7 +40,7 @@ import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LongBitSet; import org.apache.lucene.util.SparseFixedBitSet; import org.elasticsearch.common.lease.Releasables; -import org.elasticsearch.common.lucene.search.AndFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.util.BytesRefHash; import org.elasticsearch.common.util.LongHash; import org.elasticsearch.index.mapper.Uid; @@ -46,8 +48,6 @@ import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.Arrays; -import java.util.List; /** * Advantages over using this filter over Lucene's TermsFilter in the parent child context: @@ -63,13 +63,12 @@ final class ParentIdsFilter extends Filter { if (numFoundParents == 1) { BytesRef id = globalValues.lookupOrd((int) parentOrds.nextSetBit(0)); if (nonNestedDocsFilter != null) { - List filters = Arrays.asList( - new TermFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))), - nonNestedDocsFilter - ); - return new AndFilter(filters); + BooleanQuery bq = new BooleanQuery(); + bq.add(new TermQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))), Occur.MUST); + bq.add(nonNestedDocsFilter, Occur.MUST); + return Queries.wrap(bq); } else { - return new TermFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))); + return Queries.wrap(new TermQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id)))); } } else { BytesRefHash parentIds= null; @@ -96,13 +95,12 @@ final class ParentIdsFilter extends Filter { if (numFoundParents == 1) { BytesRef id = globalValues.lookupOrd((int) parentIdxs.get(0)); if (nonNestedDocsFilter != null) { - List filters = Arrays.asList( - new TermFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))), - nonNestedDocsFilter - ); - return new AndFilter(filters); + BooleanQuery bq = new BooleanQuery(); + bq.add(new TermQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))), Occur.MUST); + bq.add(nonNestedDocsFilter, Occur.MUST); + return Queries.wrap(bq); } else { - return new TermFilter(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id))); + return Queries.wrap(new TermQuery(new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(parentType, id)))); } } else { BytesRefHash parentIds = null; diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java index d535c111e4e..5b9f22ace70 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java @@ -126,7 +126,7 @@ public class ParentQuery extends Query { IndexParentChildFieldData globalIfd = parentChildIndexFieldData.loadGlobal(searcher.getIndexReader()); if (globalIfd == null) { // No docs of the specified type don't exist on this shard - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } try { @@ -138,7 +138,7 @@ public class ParentQuery extends Query { indexSearcher.setSimilarity(searcher.getSimilarity()); indexSearcher.search(parentQuery, collector); if (collector.parentCount() == 0) { - return Queries.newMatchNoDocsQuery().createWeight(searcher, needsScores); + return new BooleanQuery().createWeight(searcher, needsScores); } childWeight = new ChildWeight(this, parentQuery.createWeight(searcher, needsScores), childrenFilter, collector, globalIfd); releaseCollectorResource = false; diff --git a/src/main/java/org/elasticsearch/index/search/geo/IndexedGeoBoundingBoxFilter.java b/src/main/java/org/elasticsearch/index/search/geo/IndexedGeoBoundingBoxFilter.java index 2355245e958..6c57c251771 100644 --- a/src/main/java/org/elasticsearch/index/search/geo/IndexedGeoBoundingBoxFilter.java +++ b/src/main/java/org/elasticsearch/index/search/geo/IndexedGeoBoundingBoxFilter.java @@ -20,10 +20,11 @@ package org.elasticsearch.index.search.geo; import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Filter; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; /** @@ -43,17 +44,18 @@ public class IndexedGeoBoundingBoxFilter { } private static Filter westGeoBoundingBoxFilter(GeoPoint topLeft, GeoPoint bottomRight, GeoPointFieldMapper fieldMapper) { - XBooleanFilter filter = new XBooleanFilter(); + BooleanQuery filter = new BooleanQuery(); + filter.setMinimumNumberShouldMatch(1); filter.add(fieldMapper.lonMapper().rangeFilter(null, bottomRight.lon(), true, true), Occur.SHOULD); filter.add(fieldMapper.lonMapper().rangeFilter(topLeft.lon(), null, true, true), Occur.SHOULD); filter.add(fieldMapper.latMapper().rangeFilter(bottomRight.lat(), topLeft.lat(), true, true), Occur.MUST); - return filter; + return Queries.wrap(filter); } private static Filter eastGeoBoundingBoxFilter(GeoPoint topLeft, GeoPoint bottomRight, GeoPointFieldMapper fieldMapper) { - XBooleanFilter filter = new XBooleanFilter(); + BooleanQuery filter = new BooleanQuery(); filter.add(fieldMapper.lonMapper().rangeFilter(topLeft.lon(), bottomRight.lon(), true, true), Occur.MUST); filter.add(fieldMapper.latMapper().rangeFilter(bottomRight.lat(), topLeft.lat(), true, true), Occur.MUST); - return filter; + return Queries.wrap(filter); } } diff --git a/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java b/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java index 34c2b91fcdc..12f35f26b25 100644 --- a/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java +++ b/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java @@ -23,10 +23,12 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.PrefixFilter; +import org.apache.lucene.search.PrefixQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import java.io.IOException; @@ -38,16 +40,21 @@ import java.io.IOException; * A nested document is a sub documents that belong to a root document. * Nested documents share the unique id and type and optionally the _source with root documents. */ -public class NonNestedDocsFilter extends Filter { +public final class NonNestedDocsFilter extends Filter { public static final NonNestedDocsFilter INSTANCE = new NonNestedDocsFilter(); - private final Filter filter = new NotFilter(nestedFilter()); + private final Filter filter = Queries.wrap(Queries.not(nestedFilter())); private final int hashCode = filter.hashCode(); private NonNestedDocsFilter() { } + @Override + public Query clone() { + return INSTANCE; + } + @Override public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { return filter.getDocIdSet(context, acceptDocs); @@ -72,6 +79,6 @@ public class NonNestedDocsFilter extends Filter { * @return a filter that returns all nested documents. */ private static Filter nestedFilter() { - return new PrefixFilter(new Term(TypeFieldMapper.NAME, new BytesRef("__"))); + return Queries.wrap(new PrefixQuery(new Term(TypeFieldMapper.NAME, new BytesRef("__")))); } } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/percolator/PercolatorService.java b/src/main/java/org/elasticsearch/percolator/PercolatorService.java index f72fb497ab5..1fa8aa85693 100644 --- a/src/main/java/org/elasticsearch/percolator/PercolatorService.java +++ b/src/main/java/org/elasticsearch/percolator/PercolatorService.java @@ -25,6 +25,7 @@ import org.apache.lucene.index.ReaderUtil; import org.apache.lucene.index.memory.ExtendedMemoryIndex; import org.apache.lucene.index.memory.MemoryIndex; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; @@ -50,7 +51,7 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.lucene.search.XBooleanFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.text.BytesText; import org.elasticsearch.common.text.StringText; @@ -796,10 +797,10 @@ public class PercolatorService extends AbstractComponent { final Filter filter; if (context.aliasFilter() != null) { - XBooleanFilter booleanFilter = new XBooleanFilter(); + BooleanQuery booleanFilter = new BooleanQuery(); booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST); booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST); - filter = booleanFilter; + filter = Queries.wrap(booleanFilter); } else { filter = percolatorTypeFilter; } diff --git a/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java b/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java index 387d365c62d..118b37e386d 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java +++ b/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.aggregations; import com.google.common.collect.ImmutableMap; -import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.Query; @@ -116,7 +115,7 @@ public class AggregationPhase implements SearchPhase { // optimize the global collector based execution if (!globals.isEmpty()) { BucketCollector globalsCollector = BucketCollector.wrap(globals); - Query query = new ConstantScoreQuery(Queries.MATCH_ALL_FILTER); + Query query = Queries.newMatchAllQuery(); Filter searchFilter = context.searchFilter(context.types()); if (searchFilter != null) { query = new FilteredQuery(query, searchFilter); diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java index c64e36116ef..e0974852e03 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java @@ -18,7 +18,7 @@ */ package org.elasticsearch.search.aggregations.bucket.filter; -import org.elasticsearch.common.lucene.search.MatchAllDocsFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.ParsedFilter; import org.elasticsearch.search.aggregations.Aggregator; @@ -41,7 +41,7 @@ public class FilterParser implements Aggregator.Parser { public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException { ParsedFilter filter = context.queryParserService().parseInnerFilter(parser); - return new FilterAggregator.Factory(aggregationName, filter == null ? new MatchAllDocsFilter() : filter.filter()); + return new FilterAggregator.Factory(aggregationName, filter == null ? Queries.newMatchAllFilter() : filter.filter()); } } diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java index a8ae39494a3..49f43eafc36 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java @@ -19,7 +19,7 @@ package org.elasticsearch.search.aggregations.bucket.filters; -import org.elasticsearch.common.lucene.search.MatchAllDocsFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.ParsedFilter; import org.elasticsearch.search.SearchParseException; @@ -61,7 +61,7 @@ public class FiltersParser implements Aggregator.Parser { key = parser.currentName(); } else { ParsedFilter filter = context.queryParserService().parseInnerFilter(parser); - filters.add(new FiltersAggregator.KeyedFilter(key, filter == null ? new MatchAllDocsFilter() : filter.filter())); + filters.add(new FiltersAggregator.KeyedFilter(key, filter == null ? Queries.newMatchAllFilter() : filter.filter())); } } } else { @@ -73,7 +73,7 @@ public class FiltersParser implements Aggregator.Parser { int idx = 0; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { ParsedFilter filter = context.queryParserService().parseInnerFilter(parser); - filters.add(new FiltersAggregator.KeyedFilter(String.valueOf(idx), filter == null ? new MatchAllDocsFilter() + filters.add(new FiltersAggregator.KeyedFilter(String.valueOf(idx), filter == null ? Queries.newMatchAllFilter() : filter.filter())); idx++; } diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java index 3fa459525f2..36ec1438caa 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java @@ -23,6 +23,7 @@ import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilterCachingPolicy; +import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.util.BitDocIdSet; import org.apache.lucene.util.BitSet; @@ -55,7 +56,7 @@ public class NestedAggregator extends SingleBucketAggregator { private DocIdSetIterator childDocs; private BitSet parentDocs; - public NestedAggregator(String name, AggregatorFactories factories, ObjectMapper objectMapper, AggregationContext aggregationContext, Aggregator parentAggregator, Map metaData, FilterCachingPolicy filterCachingPolicy) throws IOException { + public NestedAggregator(String name, AggregatorFactories factories, ObjectMapper objectMapper, AggregationContext aggregationContext, Aggregator parentAggregator, Map metaData, QueryCachingPolicy filterCachingPolicy) throws IOException { super(name, factories, aggregationContext, parentAggregator, metaData); childFilter = aggregationContext.searchContext().filterCache().cache(objectMapper.nestedTypeFilter(), null, filterCachingPolicy); } @@ -142,12 +143,12 @@ public class NestedAggregator extends SingleBucketAggregator { public static class Factory extends AggregatorFactory { private final String path; - private final FilterCachingPolicy filterCachingPolicy; + private final QueryCachingPolicy queryCachingPolicy; - public Factory(String name, String path, FilterCachingPolicy filterCachingPolicy) { + public Factory(String name, String path, QueryCachingPolicy queryCachingPolicy) { super(name, InternalNested.TYPE.name()); this.path = path; - this.filterCachingPolicy = filterCachingPolicy; + this.queryCachingPolicy = queryCachingPolicy; } @Override @@ -166,7 +167,7 @@ public class NestedAggregator extends SingleBucketAggregator { if (!objectMapper.nested().isNested()) { throw new AggregationExecutionException("[nested] nested path [" + path + "] is not nested"); } - return new NestedAggregator(name, factories, objectMapper, context, parent, metaData, filterCachingPolicy); + return new NestedAggregator(name, factories, objectMapper, context, parent, metaData, queryCachingPolicy); } private final static class Unmapped extends NonCollectingAggregator { diff --git a/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsContext.java b/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsContext.java index c5074f91457..557e4d5164a 100644 --- a/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsContext.java +++ b/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsContext.java @@ -24,12 +24,14 @@ import com.google.common.collect.ImmutableMap; import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocsCollector; import org.apache.lucene.search.TopFieldCollector; @@ -40,7 +42,7 @@ import org.apache.lucene.util.BitSet; import org.apache.lucene.util.Bits; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.lucene.search.AndFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.Uid; @@ -55,7 +57,6 @@ import org.elasticsearch.search.internal.FilteredSearchContext; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.Arrays; import java.util.Map; /** @@ -284,16 +285,16 @@ public final class InnerHitsContext { term = (String) fieldsVisitor.fields().get(ParentFieldMapper.NAME).get(0); } } - Filter filter = new TermFilter(new Term(field, term)); // Only include docs that have the current hit as parent + Filter filter = Queries.wrap(new TermQuery(new Term(field, term))); // Only include docs that have the current hit as parent Filter typeFilter = documentMapper.typeFilter(); // Only include docs that have this inner hits type. + BooleanQuery filteredQuery = new BooleanQuery(); + filteredQuery.add(query, Occur.MUST); + filteredQuery.add(filter, Occur.FILTER); + filteredQuery.add(typeFilter, Occur.FILTER); if (size() == 0) { - TotalHitCountCollector collector = new TotalHitCountCollector(); - context.searcher().search( - new FilteredQuery(query, new AndFilter(Arrays.asList(filter, typeFilter))), - collector - ); - return new TopDocs(collector.getTotalHits(), Lucene.EMPTY_SCORE_DOCS, 0); + final int count = context.searcher().count(filteredQuery); + return new TopDocs(count, Lucene.EMPTY_SCORE_DOCS, 0); } else { int topN = from() + size(); TopDocsCollector topDocsCollector; @@ -302,10 +303,7 @@ public final class InnerHitsContext { } else { topDocsCollector = TopScoreDocCollector.create(topN); } - context.searcher().search( - new FilteredQuery(query, new AndFilter(Arrays.asList(filter, typeFilter))), - topDocsCollector - ); + context.searcher().search( filteredQuery, topDocsCollector); return topDocsCollector.topDocs(from(), size()); } } diff --git a/src/main/java/org/elasticsearch/search/fetch/matchedqueries/MatchedQueriesFetchSubPhase.java b/src/main/java/org/elasticsearch/search/fetch/matchedqueries/MatchedQueriesFetchSubPhase.java index 9607eb223b7..8e6197eae0b 100644 --- a/src/main/java/org/elasticsearch/search/fetch/matchedqueries/MatchedQueriesFetchSubPhase.java +++ b/src/main/java/org/elasticsearch/search/fetch/matchedqueries/MatchedQueriesFetchSubPhase.java @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.Bits; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ExceptionsHelper; diff --git a/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 822423060f6..e4b7070b8c3 100644 --- a/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -21,10 +21,14 @@ package org.elasticsearch.search.internal; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; + +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Sort; import org.apache.lucene.util.Counter; @@ -33,11 +37,11 @@ import org.elasticsearch.action.search.SearchType; import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.lease.Releasables; -import org.elasticsearch.common.lucene.search.AndFilter; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.function.BoostScoreFunction; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.index.IndexService; import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.cache.filter.FilterCache; @@ -49,8 +53,6 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedFilter; import org.elasticsearch.index.query.ParsedQuery; -import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.query.support.NestedScope; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; @@ -248,15 +250,17 @@ public class DefaultSearchContext extends SearchContext { @Override public Filter searchFilter(String[] types) { Filter filter = mapperService().searchFilter(types); - if (filter == null) { - return aliasFilter; - } else { - filter = filterCache().cache(filter, null, indexService.queryParserService().autoFilterCachePolicy()); - if (aliasFilter != null) { - return new AndFilter(ImmutableList.of(filter, aliasFilter)); - } - return filter; + if (filter == null && aliasFilter == null) { + return null; } + BooleanQuery bq = new BooleanQuery(); + if (filter != null) { + bq.add(filterCache().cache(filter, null, indexService.queryParserService().autoFilterCachePolicy()), Occur.MUST); + } + if (aliasFilter != null) { + bq.add(aliasFilter, Occur.MUST); + } + return Queries.wrap(bq); } @Override diff --git a/src/test/java/org/elasticsearch/common/lucene/docset/DocIdSetsTests.java b/src/test/java/org/elasticsearch/common/lucene/docset/DocIdSetsTests.java deleted file mode 100644 index e57bd0c9e8f..00000000000 --- a/src/test/java/org/elasticsearch/common/lucene/docset/DocIdSetsTests.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.docset; - -import java.io.IOException; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.search.Filter; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.RoaringDocIdSet; -import org.elasticsearch.ElasticsearchIllegalArgumentException; -import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.common.settings.ImmutableSettings; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.DistanceUnit; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.index.engine.Engine.Searcher; -import org.elasticsearch.index.query.FilterBuilder; -import org.elasticsearch.index.query.FilterBuilders; -import org.elasticsearch.index.query.TermFilterBuilder; -import org.elasticsearch.index.IndexService; -import org.elasticsearch.test.ElasticsearchSingleNodeTest; - -public class DocIdSetsTests extends ElasticsearchSingleNodeTest { - - private static final Settings SINGLE_SHARD_SETTINGS = ImmutableSettings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).build(); - - private void test(IndexService indexService, boolean broken, FilterBuilder filterBuilder) throws IOException { - client().admin().indices().prepareRefresh("test").get(); - XContentBuilder builder = filterBuilder.toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS); - XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes()); - Filter filter = indexService.queryParserService().parseInnerFilter(parser).filter(); - try (Searcher searcher = indexService.shardSafe(0).acquireSearcher("test")) { - final LeafReaderContext ctx = searcher.reader().leaves().get(0); - DocIdSet set = filter.getDocIdSet(ctx, null); - assertEquals(broken, DocIdSets.isBroken(set.iterator())); - } - } - - public void testTermIsNotBroken() throws IOException { - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "l", "type=long"); - client().prepareIndex("test", "type").setSource("l", 7).get(); - TermFilterBuilder filter = FilterBuilders.termFilter("l", 7).cache(randomBoolean()); - test(indexService, false, filter); - } - - public void testDefaultGeoIsBroken() throws IOException { - // Geo is slow by default :'( - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "gp", "type=geo_point"); - client().prepareIndex("test", "type").setSource("gp", "2,3").get(); - FilterBuilder filter = FilterBuilders.geoDistanceFilter("gp").distance(1000, DistanceUnit.KILOMETERS).point(3, 2); - test(indexService, true, filter); - - } - - public void testIndexedGeoIsNotBroken() throws IOException { - // Geo has a fast iterator when indexing lat,lon and using the "indexed" bbox optimization - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "gp", "type=geo_point,lat_lon=true"); - client().prepareIndex("test", "type").setSource("gp", "2,3").get(); - FilterBuilder filter = FilterBuilders.geoDistanceFilter("gp").distance(1000, DistanceUnit.KILOMETERS).point(3, 2).optimizeBbox("indexed"); - test(indexService, false, filter); - } - - public void testScriptIsBroken() throws IOException { // by nature unfortunately - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "l", "type=long"); - client().prepareIndex("test", "type").setSource("l", 7).get(); - FilterBuilder filter = FilterBuilders.scriptFilter("doc['l'].value < 8"); - test(indexService, true, filter); - } - - public void testCachedIsNotBroken() throws IOException { - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "l", "type=long"); - client().prepareIndex("test", "type").setSource("l", 7).get(); - // This filter is inherently slow but by caching it we pay the price at caching time, not iteration - FilterBuilder filter = FilterBuilders.scriptFilter("doc['l'].value < 8").cache(true); - test(indexService, false, filter); - } - - public void testOr() throws IOException { - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "l", "type=long"); - client().prepareIndex("test", "type").setSource("l", new long[] {7, 8}).get(); - // Or with fast clauses is fast - FilterBuilder filter = FilterBuilders.orFilter(FilterBuilders.termFilter("l", 7), FilterBuilders.termFilter("l", 8)); - test(indexService, false, filter); - // But if at least one clause is broken, it is broken - filter = FilterBuilders.orFilter(FilterBuilders.termFilter("l", 7), FilterBuilders.scriptFilter("doc['l'].value < 8")); - test(indexService, true, filter); - } - - public void testAnd() throws IOException { - IndexService indexService = createIndex("test", SINGLE_SHARD_SETTINGS, "type", "l", "type=long"); - client().prepareIndex("test", "type").setSource("l", new long[] {7, 8}).get(); - // And with fast clauses is fast - FilterBuilder filter = FilterBuilders.andFilter(FilterBuilders.termFilter("l", 7), FilterBuilders.termFilter("l", 8)); - test(indexService, false, filter); - // If at least one clause is 'fast' and the other clauses supports random-access, it is still fast - filter = FilterBuilders.andFilter(FilterBuilders.termFilter("l", 7).cache(randomBoolean()), FilterBuilders.scriptFilter("doc['l'].value < 8")); - test(indexService, false, filter); - // However if all clauses are broken, the and is broken - filter = FilterBuilders.andFilter(FilterBuilders.scriptFilter("doc['l'].value > 5"), FilterBuilders.scriptFilter("doc['l'].value < 8")); - test(indexService, true, filter); - } - - public void testAsSequentialAccessBits() throws IOException { - final int maxDoc = randomIntBetween(5, 100); - - // Null DocIdSet maps to empty bits - Bits bits = DocIdSets.asSequentialAccessBits(100, null); - for (int i = 0; i < maxDoc; ++i) { - assertFalse(bits.get(i)); - } - - // Empty set maps to empty bits - bits = DocIdSets.asSequentialAccessBits(100, DocIdSet.EMPTY); - for (int i = 0; i < maxDoc; ++i) { - assertFalse(bits.get(i)); - } - - RoaringDocIdSet.Builder b = new RoaringDocIdSet.Builder(maxDoc); - for (int i = randomInt(maxDoc - 1); i < maxDoc; i += randomIntBetween(1, 10)) { - b.add(i); - } - final RoaringDocIdSet set = b.build(); - // RoaringDocIdSet does not support random access - assertNull(set.bits()); - - bits = DocIdSets.asSequentialAccessBits(100, set); - bits.get(4); - try { - bits.get(2); - fail("Should have thrown an exception because of out-of-order consumption"); - } catch (ElasticsearchIllegalArgumentException e) { - // ok - } - - bits = DocIdSets.asSequentialAccessBits(100, set); - DocIdSetIterator iterator = set.iterator(); - for (int i = randomInt(maxDoc - 1); i < maxDoc; i += randomIntBetween(1, 10)) { - if (iterator.docID() < i) { - iterator.advance(i); - } - - assertEquals(iterator.docID() == i, bits.get(i)); - } - } -} diff --git a/src/test/java/org/elasticsearch/common/lucene/index/FreqTermsEnumTests.java b/src/test/java/org/elasticsearch/common/lucene/index/FreqTermsEnumTests.java index 28fdf7c6ae0..38054992298 100644 --- a/src/test/java/org/elasticsearch/common/lucene/index/FreqTermsEnumTests.java +++ b/src/test/java/org/elasticsearch/common/lucene/index/FreqTermsEnumTests.java @@ -28,8 +28,14 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.IndexableField; +import org.apache.lucene.index.NoMergePolicy; +import org.apache.lucene.index.Term; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; @@ -41,9 +47,18 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; -import static com.carrotsearch.randomizedtesting.RandomizedTest.*; +import static com.carrotsearch.randomizedtesting.RandomizedTest.frequently; +import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomInt; +import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -143,7 +158,7 @@ public class FreqTermsEnumTests extends ElasticsearchTestCase { } } } - filter = new TermsFilter(filterTerms); + filter = Queries.wrap(new TermsQuery(filterTerms)); } private void addFreqs(Document doc, Map reference) { @@ -175,9 +190,9 @@ public class FreqTermsEnumTests extends ElasticsearchTestCase { @Test public void testNonDeletedFreqs() throws Exception { - assertAgainstReference(true, true, Queries.MATCH_ALL_FILTER, referenceNotDeleted); - assertAgainstReference(true, false, Queries.MATCH_ALL_FILTER, referenceNotDeleted); - assertAgainstReference(false, true, Queries.MATCH_ALL_FILTER, referenceNotDeleted); + assertAgainstReference(true, true, Queries.newMatchAllFilter(), referenceNotDeleted); + assertAgainstReference(true, false, Queries.newMatchAllFilter(), referenceNotDeleted); + assertAgainstReference(false, true, Queries.newMatchAllFilter(), referenceNotDeleted); } @Test diff --git a/src/test/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilterTests.java b/src/test/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilterTests.java index 861dabf6913..10f10ff0cc2 100644 --- a/src/test/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilterTests.java +++ b/src/test/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilterTests.java @@ -59,7 +59,7 @@ public class MatchAllDocsFilterTests extends ElasticsearchTestCase { IndexReader reader = DirectoryReader.open(indexWriter, true); IndexSearcher searcher = new IndexSearcher(reader); - ConstantScoreQuery query = new ConstantScoreQuery(Queries.MATCH_ALL_FILTER); + ConstantScoreQuery query = new ConstantScoreQuery(Queries.newMatchAllFilter()); long count = Lucene.count(searcher, query); assertThat(count, equalTo(2l)); diff --git a/src/test/java/org/elasticsearch/common/lucene/search/TermsFilterTests.java b/src/test/java/org/elasticsearch/common/lucene/search/TermsFilterTests.java deleted file mode 100644 index 61aecb98f1b..00000000000 --- a/src/test/java/org/elasticsearch/common/lucene/search/TermsFilterTests.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.analysis.core.KeywordAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; -import org.apache.lucene.search.*; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.RAMDirectory; -import org.apache.lucene.util.BitSet; -import org.elasticsearch.common.lucene.docset.DocIdSets; -import org.elasticsearch.test.ElasticsearchTestCase; -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; - -/** - */ -public class TermsFilterTests extends ElasticsearchTestCase { - - @Test - public void testTermFilter() throws Exception { - String fieldName = "field1"; - Directory rd = new RAMDirectory(); - IndexWriter w = new IndexWriter(rd, new IndexWriterConfig(new KeywordAnalyzer())); - for (int i = 0; i < 100; i++) { - Document doc = new Document(); - int term = i * 10; //terms are units of 10; - doc.add(new Field(fieldName, "" + term, StringField.TYPE_NOT_STORED)); - doc.add(new Field("all", "xxx", StringField.TYPE_NOT_STORED)); - w.addDocument(doc); - if ((i % 40) == 0) { - w.commit(); - } - } - LeafReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(w, true)); - w.close(); - - TermFilter tf = new TermFilter(new Term(fieldName, "19")); - DocIdSet dis = tf.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - assertTrue(dis == null || dis.iterator() == null); - - tf = new TermFilter(new Term(fieldName, "20")); - DocIdSet result = tf.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - BitSet bits = DocIdSets.toBitSet(result.iterator(), reader.maxDoc()); - assertThat(bits.cardinality(), equalTo(1)); - - tf = new TermFilter(new Term("all", "xxx")); - result = tf.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - bits = DocIdSets.toBitSet(result.iterator(), reader.maxDoc()); - assertThat(bits.cardinality(), equalTo(100)); - - reader.close(); - rd.close(); - } - - @Test - public void testTermsFilter() throws Exception { - String fieldName = "field1"; - Directory rd = new RAMDirectory(); - IndexWriter w = new IndexWriter(rd, new IndexWriterConfig(new KeywordAnalyzer())); - for (int i = 0; i < 100; i++) { - Document doc = new Document(); - int term = i * 10; //terms are units of 10; - doc.add(new Field(fieldName, "" + term, StringField.TYPE_NOT_STORED)); - doc.add(new Field("all", "xxx", StringField.TYPE_NOT_STORED)); - w.addDocument(doc); - if ((i % 40) == 0) { - w.commit(); - } - } - LeafReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(w, true)); - w.close(); - - TermsFilter tf = new TermsFilter(new Term[]{new Term(fieldName, "19")}); - assertNull(tf.getDocIdSet(reader.getContext(), reader.getLiveDocs())); - - tf = new TermsFilter(new Term[]{new Term(fieldName, "19"), new Term(fieldName, "20")}); - DocIdSet result = tf.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - BitSet bits = DocIdSets.toBitSet(result.iterator(), reader.maxDoc()); - assertThat(bits.cardinality(), equalTo(1)); - - tf = new TermsFilter(new Term[]{new Term(fieldName, "19"), new Term(fieldName, "20"), new Term(fieldName, "10")}); - result = tf.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - bits = DocIdSets.toBitSet(result.iterator(), reader.maxDoc()); - assertThat(bits.cardinality(), equalTo(2)); - - tf = new TermsFilter(new Term[]{new Term(fieldName, "19"), new Term(fieldName, "20"), new Term(fieldName, "10"), new Term(fieldName, "00")}); - result = tf.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - bits = DocIdSets.toBitSet(result.iterator(), reader.maxDoc()); - assertThat(bits.cardinality(), equalTo(2)); - - reader.close(); - rd.close(); - } -} diff --git a/src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterLuceneTests.java b/src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterLuceneTests.java deleted file mode 100644 index aba12111b15..00000000000 --- a/src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterLuceneTests.java +++ /dev/null @@ -1,424 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.analysis.core.WhitespaceAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.TextField; -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.IndexWriterConfig; -import org.apache.lucene.index.LeafReader; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.SlowCompositeReaderWrapper; -import org.apache.lucene.index.Term; -import org.apache.lucene.queries.FilterClause; -import org.apache.lucene.queries.TermsFilter; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.search.Filter; -import org.apache.lucene.search.QueryWrapperFilter; -import org.apache.lucene.search.TermQuery; -import org.apache.lucene.search.TermRangeFilter; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.RAMDirectory; -import org.apache.lucene.util.BitDocIdSet; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.SparseFixedBitSet; -import org.elasticsearch.test.ElasticsearchTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; - -import static org.hamcrest.core.IsEqual.equalTo; - -/** - * Tests ported from Lucene. - */ -public class XBooleanFilterLuceneTests extends ElasticsearchTestCase { - - private Directory directory; - private LeafReader reader; - - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - directory = new RAMDirectory(); - IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(new WhitespaceAnalyzer())); - - //Add series of docs with filterable fields : acces rights, prices, dates and "in-stock" flags - addDoc(writer, "admin guest", "010", "20040101", "Y"); - addDoc(writer, "guest", "020", "20040101", "Y"); - addDoc(writer, "guest", "020", "20050101", "Y"); - addDoc(writer, "admin", "020", "20050101", "Maybe"); - addDoc(writer, "admin guest", "030", "20050101", "N"); - writer.close(); - reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(directory)); - writer.close(); - } - - @Override - @After - public void tearDown() throws Exception { - super.tearDown(); - reader.close(); - directory.close(); - } - - private void addDoc(IndexWriter writer, String accessRights, String price, String date, String inStock) throws IOException { - Document doc = new Document(); - doc.add(new TextField("accessRights", accessRights, Field.Store.YES)); - doc.add(new TextField("price", price, Field.Store.YES)); - doc.add(new TextField("date", date, Field.Store.YES)); - doc.add(new TextField("inStock", inStock, Field.Store.YES)); - writer.addDocument(doc); - } - - private Filter getRangeFilter(String field, String lowerPrice, String upperPrice) { - return TermRangeFilter.newStringRange(field, lowerPrice, upperPrice, true, true); - } - - private Filter getTermsFilter(String field, String text) { - return new TermsFilter(new Term(field, text)); - } - - private Filter getWrappedTermQuery(String field, String text) { - return new QueryWrapperFilter(new TermQuery(new Term(field, text))); - } - - private Filter getEmptyFilter() { - return new Filter() { - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) { - return new BitDocIdSet(new SparseFixedBitSet(context.reader().maxDoc())); - } - - @Override - public String toString(String field) { - return "empty"; - } - }; - } - - private Filter getNullDISFilter() { - return new Filter() { - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) { - return null; - } - - @Override - public String toString(String field) { - return "nulldis"; - } - }; - } - - private Filter getNullDISIFilter() { - return new Filter() { - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) { - return new DocIdSet() { - @Override - public DocIdSetIterator iterator() { - return null; - } - - @Override - public boolean isCacheable() { - return true; - } - - @Override - public long ramBytesUsed() { - return 0; - } - }; - } - - @Override - public String toString(String field) { - return "nulldisi"; - } - }; - } - - private void tstFilterCard(String mes, int expected, Filter filt) throws Exception { - int actual = 0; - DocIdSet docIdSet = filt.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - if (docIdSet != null) { - DocIdSetIterator disi = docIdSet.iterator(); - if (disi != null) { - while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { - actual++; - } - } - } - assertThat(mes, actual, equalTo(expected)); - } - - @Test - public void testShould() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.SHOULD); - tstFilterCard("Should retrieves only 1 doc", 1, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getWrappedTermQuery("price", "030"), BooleanClause.Occur.SHOULD); - tstFilterCard("Should retrieves only 1 doc", 1, booleanFilter); - } - - @Test - public void testShoulds() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "010", "020"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getRangeFilter("price", "020", "030"), BooleanClause.Occur.SHOULD); - tstFilterCard("Shoulds are Ored together", 5, booleanFilter); - } - - @Test - public void testShouldsAndMustNot() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "010", "020"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getRangeFilter("price", "020", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("inStock", "N"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("Shoulds Ored but AndNot", 4, booleanFilter); - - booleanFilter.add(getTermsFilter("inStock", "Maybe"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("Shoulds Ored but AndNots", 3, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "010", "020"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getRangeFilter("price", "020", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getWrappedTermQuery("inStock", "N"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("Shoulds Ored but AndNot", 4, booleanFilter); - - booleanFilter.add(getWrappedTermQuery("inStock", "Maybe"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("Shoulds Ored but AndNots", 3, booleanFilter); - } - - @Test - public void testShouldsAndMust() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "010", "020"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getRangeFilter("price", "020", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard("Shoulds Ored but MUST", 3, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "010", "020"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getRangeFilter("price", "020", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getWrappedTermQuery("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard("Shoulds Ored but MUST", 3, booleanFilter); - } - - @Test - public void testShouldsAndMusts() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "010", "020"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getRangeFilter("price", "020", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - booleanFilter.add(getRangeFilter("date", "20040101", "20041231"), BooleanClause.Occur.MUST); - tstFilterCard("Shoulds Ored but MUSTs ANDED", 1, booleanFilter); - } - - @Test - public void testShouldsAndMustsAndMustNot() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "030", "040"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - booleanFilter.add(getRangeFilter("date", "20050101", "20051231"), BooleanClause.Occur.MUST); - booleanFilter.add(getTermsFilter("inStock", "N"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("Shoulds Ored but MUSTs ANDED and MustNot", 0, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getRangeFilter("price", "030", "040"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getWrappedTermQuery("accessRights", "admin"), BooleanClause.Occur.MUST); - booleanFilter.add(getRangeFilter("date", "20050101", "20051231"), BooleanClause.Occur.MUST); - booleanFilter.add(getWrappedTermQuery("inStock", "N"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("Shoulds Ored but MUSTs ANDED and MustNot", 0, booleanFilter); - } - - @Test - public void testJustMust() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard("MUST", 3, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getWrappedTermQuery("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard("MUST", 3, booleanFilter); - } - - @Test - public void testJustMustNot() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("inStock", "N"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("MUST_NOT", 4, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getWrappedTermQuery("inStock", "N"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("MUST_NOT", 4, booleanFilter); - } - - @Test - public void testMustAndMustNot() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("inStock", "N"), BooleanClause.Occur.MUST); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("MUST_NOT wins over MUST for same docs", 0, booleanFilter); - - // same with a real DISI (no OpenBitSetIterator) - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getWrappedTermQuery("inStock", "N"), BooleanClause.Occur.MUST); - booleanFilter.add(getWrappedTermQuery("price", "030"), BooleanClause.Occur.MUST_NOT); - tstFilterCard("MUST_NOT wins over MUST for same docs", 0, booleanFilter); - } - - @Test - public void testEmpty() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - tstFilterCard("empty XBooleanFilter returns no results", 0, booleanFilter); - } - - @Test - public void testCombinedNullDocIdSets() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.MUST); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.MUST); - tstFilterCard("A MUST filter that returns a null DIS should never return documents", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.MUST); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.MUST); - tstFilterCard("A MUST filter that returns a null DISI should never return documents", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.SHOULD); - tstFilterCard("A SHOULD filter that returns a null DIS should be invisible", 1, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.SHOULD); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.SHOULD); - tstFilterCard("A SHOULD filter that returns a null DISI should be invisible", 1, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.MUST); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.MUST_NOT); - tstFilterCard("A MUST_NOT filter that returns a null DIS should be invisible", 1, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("price", "030"), BooleanClause.Occur.MUST); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.MUST_NOT); - tstFilterCard("A MUST_NOT filter that returns a null DISI should be invisible", 1, booleanFilter); - } - - @Test - public void testJustNullDocIdSets() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.MUST); - tstFilterCard("A MUST filter that returns a null DIS should never return documents", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.MUST); - tstFilterCard("A MUST filter that returns a null DISI should never return documents", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.SHOULD); - tstFilterCard("A single SHOULD filter that returns a null DIS should never return documents", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.SHOULD); - tstFilterCard("A single SHOULD filter that returns a null DISI should never return documents", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.MUST_NOT); - tstFilterCard("A single MUST_NOT filter that returns a null DIS should be invisible", 5, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.MUST_NOT); - tstFilterCard("A single MUST_NOT filter that returns a null DIS should be invisible", 5, booleanFilter); - } - - @Test - public void testNonMatchingShouldsAndMusts() throws Exception { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getEmptyFilter(), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard(">0 shoulds with no matches should return no docs", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISFilter(), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard(">0 shoulds with no matches should return no docs", 0, booleanFilter); - - booleanFilter = new XBooleanFilter(); - booleanFilter.add(getNullDISIFilter(), BooleanClause.Occur.SHOULD); - booleanFilter.add(getTermsFilter("accessRights", "admin"), BooleanClause.Occur.MUST); - tstFilterCard(">0 shoulds with no matches should return no docs", 0, booleanFilter); - } - - @Test - public void testToStringOfBooleanFilterContainingTermsFilter() { - XBooleanFilter booleanFilter = new XBooleanFilter(); - booleanFilter.add(getTermsFilter("inStock", "N"), BooleanClause.Occur.MUST); - booleanFilter.add(getTermsFilter("isFragile", "Y"), BooleanClause.Occur.MUST); - - assertThat("BooleanFilter(+inStock:N +isFragile:Y)", equalTo(booleanFilter.toString())); - } - - @Test - public void testToStringOfWrappedBooleanFilters() { - XBooleanFilter orFilter = new XBooleanFilter(); - - XBooleanFilter stockFilter = new XBooleanFilter(); - stockFilter.add(new FilterClause(getTermsFilter("inStock", "Y"), BooleanClause.Occur.MUST)); - stockFilter.add(new FilterClause(getTermsFilter("barCode", "12345678"), BooleanClause.Occur.MUST)); - - orFilter.add(new FilterClause(stockFilter, BooleanClause.Occur.SHOULD)); - - XBooleanFilter productPropertyFilter = new XBooleanFilter(); - productPropertyFilter.add(new FilterClause(getTermsFilter("isHeavy", "N"), BooleanClause.Occur.MUST)); - productPropertyFilter.add(new FilterClause(getTermsFilter("isDamaged", "Y"), BooleanClause.Occur.MUST)); - - orFilter.add(new FilterClause(productPropertyFilter, BooleanClause.Occur.SHOULD)); - - XBooleanFilter composedFilter = new XBooleanFilter(); - composedFilter.add(new FilterClause(orFilter, BooleanClause.Occur.MUST)); - - assertThat( - "BooleanFilter(+BooleanFilter(BooleanFilter(+inStock:Y +barCode:12345678) BooleanFilter(+isHeavy:N +isDamaged:Y)))", - equalTo(composedFilter.toString()) - ); - } - -} diff --git a/src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterTests.java b/src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterTests.java deleted file mode 100644 index ea1539d867c..00000000000 --- a/src/test/java/org/elasticsearch/common/lucene/search/XBooleanFilterTests.java +++ /dev/null @@ -1,591 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene.search; - -import org.apache.lucene.analysis.core.KeywordAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.SortedDocValuesField; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.FilterClause; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.search.*; -import org.apache.lucene.store.Directory; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.FixedBitSet; -import org.apache.lucene.util.TestUtil; -import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.test.ElasticsearchTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.apache.lucene.search.BooleanClause.Occur.*; -import static org.hamcrest.core.IsEqual.equalTo; - -/** - */ -public class XBooleanFilterTests extends ElasticsearchTestCase { - - private Directory directory; - private LeafReader reader; - private static final char[] distinctValues = new char[] {'a', 'b', 'c', 'd', 'v','z','y'}; - - @Before - public void setup() throws Exception { - super.setUp(); - char[][] documentMatrix = new char[][] { - {'a', 'b', 'c', 'd', 'v'}, - {'a', 'b', 'c', 'd', 'z'}, - {'a', 'a', 'a', 'a', 'x'} - }; - - List documents = new ArrayList<>(documentMatrix.length); - for (char[] fields : documentMatrix) { - Document document = new Document(); - for (int i = 0; i < fields.length; i++) { - document.add(new StringField(Integer.toString(i), String.valueOf(fields[i]), Field.Store.NO)); - document.add(new SortedDocValuesField(Integer.toString(i), new BytesRef(String.valueOf(fields[i])))); - } - documents.add(document); - } - directory = newDirectory(); - IndexWriter w = new IndexWriter(directory, new IndexWriterConfig(new KeywordAnalyzer())); - w.addDocuments(documents); - w.close(); - reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(directory)); - } - - @Override - @After - public void tearDown() throws Exception { - reader.close(); - directory.close(); - super.tearDown(); - - } - - @Test - public void testWithTwoClausesOfEachOccur_allFixedBitDocIdSetFilters() throws Exception { - List booleanFilters = new ArrayList<>(); - booleanFilters.add(createBooleanFilter( - newFilterClause(0, 'a', MUST, false), newFilterClause(1, 'b', MUST, false), - newFilterClause(2, 'c', SHOULD, false), newFilterClause(3, 'd', SHOULD, false), - newFilterClause(4, 'e', MUST_NOT, false), newFilterClause(5, 'f', MUST_NOT, false) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(4, 'e', MUST_NOT, false), newFilterClause(5, 'f', MUST_NOT, false), - newFilterClause(0, 'a', MUST, false), newFilterClause(1, 'b', MUST, false), - newFilterClause(2, 'c', SHOULD, false), newFilterClause(3, 'd', SHOULD, false) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(2, 'c', SHOULD, false), newFilterClause(3, 'd', SHOULD, false), - newFilterClause(4, 'e', MUST_NOT, false), newFilterClause(5, 'f', MUST_NOT, false), - newFilterClause(0, 'a', MUST, false), newFilterClause(1, 'b', MUST, false) - )); - - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - } - - @Test - public void testWithTwoClausesOfEachOccur_allBitsBasedFilters() throws Exception { - List booleanFilters = new ArrayList<>(); - booleanFilters.add(createBooleanFilter( - newFilterClause(0, 'a', MUST, true), newFilterClause(1, 'b', MUST, true), - newFilterClause(2, 'c', SHOULD, true), newFilterClause(3, 'd', SHOULD, true), - newFilterClause(4, 'e', MUST_NOT, true), newFilterClause(5, 'f', MUST_NOT, true) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(4, 'e', MUST_NOT, true), newFilterClause(5, 'f', MUST_NOT, true), - newFilterClause(0, 'a', MUST, true), newFilterClause(1, 'b', MUST, true), - newFilterClause(2, 'c', SHOULD, true), newFilterClause(3, 'd', SHOULD, true) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(2, 'c', SHOULD, true), newFilterClause(3, 'd', SHOULD, true), - newFilterClause(4, 'e', MUST_NOT, true), newFilterClause(5, 'f', MUST_NOT, true), - newFilterClause(0, 'a', MUST, true), newFilterClause(1, 'b', MUST, true) - )); - - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - } - - @Test - public void testWithTwoClausesOfEachOccur_allFilterTypes() throws Exception { - List booleanFilters = new ArrayList<>(); - booleanFilters.add(createBooleanFilter( - newFilterClause(0, 'a', MUST, true), newFilterClause(1, 'b', MUST, false), - newFilterClause(2, 'c', SHOULD, true), newFilterClause(3, 'd', SHOULD, false), - newFilterClause(4, 'e', MUST_NOT, true), newFilterClause(5, 'f', MUST_NOT, false) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(4, 'e', MUST_NOT, true), newFilterClause(5, 'f', MUST_NOT, false), - newFilterClause(0, 'a', MUST, true), newFilterClause(1, 'b', MUST, false), - newFilterClause(2, 'c', SHOULD, true), newFilterClause(3, 'd', SHOULD, false) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(2, 'c', SHOULD, true), newFilterClause(3, 'd', SHOULD, false), - newFilterClause(4, 'e', MUST_NOT, true), newFilterClause(5, 'f', MUST_NOT, false), - newFilterClause(0, 'a', MUST, true), newFilterClause(1, 'b', MUST, false) - )); - - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - - booleanFilters.clear(); - booleanFilters.add(createBooleanFilter( - newFilterClause(0, 'a', MUST, false), newFilterClause(1, 'b', MUST, true), - newFilterClause(2, 'c', SHOULD, false), newFilterClause(3, 'd', SHOULD, true), - newFilterClause(4, 'e', MUST_NOT, false), newFilterClause(5, 'f', MUST_NOT, true) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(4, 'e', MUST_NOT, false), newFilterClause(5, 'f', MUST_NOT, true), - newFilterClause(0, 'a', MUST, false), newFilterClause(1, 'b', MUST, true), - newFilterClause(2, 'c', SHOULD, false), newFilterClause(3, 'd', SHOULD, true) - )); - booleanFilters.add(createBooleanFilter( - newFilterClause(2, 'c', SHOULD, false), newFilterClause(3, 'd', SHOULD, true), - newFilterClause(4, 'e', MUST_NOT, false), newFilterClause(5, 'f', MUST_NOT, true), - newFilterClause(0, 'a', MUST, false), newFilterClause(1, 'b', MUST, true) - )); - - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - } - - @Test - public void testWithTwoClausesOfEachOccur_singleClauseOptimisation() throws Exception { - List booleanFilters = new ArrayList<>(); - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'b', MUST, true) - )); - - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - - booleanFilters.clear(); - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'c', MUST_NOT, true) - )); - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(3)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(true)); - } - - booleanFilters.clear(); - booleanFilters.add(createBooleanFilter( - newFilterClause(2, 'c', SHOULD, true) - )); - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - } - - @Test - public void testOnlyShouldClauses() throws Exception { - List booleanFilters = new ArrayList<>(); - // 2 slow filters - // This case caused: https://github.com/elasticsearch/elasticsearch/issues/2826 - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'a', SHOULD, true), - newFilterClause(1, 'b', SHOULD, true) - )); - // 2 fast filters - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'a', SHOULD, false), - newFilterClause(1, 'b', SHOULD, false) - )); - // 1 fast filters, 1 slow filter - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'a', SHOULD, true), - newFilterClause(1, 'b', SHOULD, false) - )); - - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(3)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(true)); - } - } - - @Test - public void testOnlyMustClauses() throws Exception { - List booleanFilters = new ArrayList<>(); - // Slow filters - booleanFilters.add(createBooleanFilter( - newFilterClause(3, 'd', MUST, true), - newFilterClause(3, 'd', MUST, true) - )); - // 2 fast filters - booleanFilters.add(createBooleanFilter( - newFilterClause(3, 'd', MUST, false), - newFilterClause(3, 'd', MUST, false) - )); - // 1 fast filters, 1 slow filter - booleanFilters.add(createBooleanFilter( - newFilterClause(3, 'd', MUST, true), - newFilterClause(3, 'd', MUST, false) - )); - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - } - - @Test - public void testOnlyMustNotClauses() throws Exception { - List booleanFilters = new ArrayList<>(); - // Slow filters - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'a', MUST_NOT, true), - newFilterClause(1, 'a', MUST_NOT, true) - )); - // 2 fast filters - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'a', MUST_NOT, false), - newFilterClause(1, 'a', MUST_NOT, false) - )); - // 1 fast filters, 1 slow filter - booleanFilters.add(createBooleanFilter( - newFilterClause(1, 'a', MUST_NOT, true), - newFilterClause(1, 'a', MUST_NOT, false) - )); - for (XBooleanFilter booleanFilter : booleanFilters) { - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(true)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(false)); - } - } - - @Test - public void testNonMatchingSlowShouldWithMatchingMust() throws Exception { - XBooleanFilter booleanFilter = createBooleanFilter( - newFilterClause(0, 'a', MUST, false), - newFilterClause(0, 'b', SHOULD, true) - ); - - DocIdSet docIdSet = booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - boolean empty = false; - if (docIdSet == null) { - empty = true; - } else { - DocIdSetIterator it = docIdSet.iterator(); - if (it == null || it.nextDoc() == DocIdSetIterator.NO_MORE_DOCS) { - empty = true; - } - } - assertTrue(empty); - } - - @Test - public void testSlowShouldClause_atLeastOneShouldMustMatch() throws Exception { - XBooleanFilter booleanFilter = createBooleanFilter( - newFilterClause(0, 'a', MUST, false), - newFilterClause(1, 'a', SHOULD, true) - ); - - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(1)); - assertThat(result.get(0), equalTo(false)); - assertThat(result.get(1), equalTo(false)); - assertThat(result.get(2), equalTo(true)); - - booleanFilter = createBooleanFilter( - newFilterClause(0, 'a', MUST, false), - newFilterClause(1, 'a', SHOULD, true), - newFilterClause(4, 'z', SHOULD, true) - ); - - result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(false)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(true)); - } - - @Test - // See issue: https://github.com/elasticsearch/elasticsearch/issues/4130 - public void testOneFastMustNotOneFastShouldAndOneSlowShould() throws Exception { - XBooleanFilter booleanFilter = createBooleanFilter( - newFilterClause(4, 'v', MUST_NOT, false), - newFilterClause(4, 'z', SHOULD, false), - newFilterClause(4, 'x', SHOULD, true) - ); - - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(false)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(true)); - } - - @Test - public void testOneFastShouldClauseAndOneSlowShouldClause() throws Exception { - XBooleanFilter booleanFilter = createBooleanFilter( - newFilterClause(4, 'z', SHOULD, false), - newFilterClause(4, 'x', SHOULD, true) - ); - - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(false)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(true)); - } - - @Test - public void testOneMustClauseOneFastShouldClauseAndOneSlowShouldClause() throws Exception { - XBooleanFilter booleanFilter = createBooleanFilter( - newFilterClause(0, 'a', MUST, false), - newFilterClause(4, 'z', SHOULD, false), - newFilterClause(4, 'x', SHOULD, true) - ); - - FixedBitSet result = new FixedBitSet(reader.maxDoc()); - result.or(booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()).iterator()); - assertThat(result.cardinality(), equalTo(2)); - assertThat(result.get(0), equalTo(false)); - assertThat(result.get(1), equalTo(true)); - assertThat(result.get(2), equalTo(true)); - } - - private static FilterClause newFilterClause(int field, char character, BooleanClause.Occur occur, boolean slowerBitsBackedFilter) { - Filter filter; - if (slowerBitsBackedFilter) { - filter = new PrettyPrintFieldCacheTermsFilter(String.valueOf(field), String.valueOf(character)); - } else { - Term term = new Term(String.valueOf(field), String.valueOf(character)); - filter = new TermFilter(term); - } - return new FilterClause(filter, occur); - } - - private static XBooleanFilter createBooleanFilter(FilterClause... clauses) { - XBooleanFilter booleanFilter = new XBooleanFilter(); - for (FilterClause clause : clauses) { - booleanFilter.add(clause); - } - return booleanFilter; - } - - @Test - public void testRandom() throws IOException { - int iterations = scaledRandomIntBetween(100, 1000); // don't worry that is fast! - for (int iter = 0; iter < iterations; iter++) { - int numClauses = 1 + random().nextInt(10); - FilterClause[] clauses = new FilterClause[numClauses]; - BooleanQuery topLevel = new BooleanQuery(); - BooleanQuery orQuery = new BooleanQuery(); - boolean hasMust = false; - boolean hasShould = false; - boolean hasMustNot = false; - for(int i = 0; i < numClauses; i++) { - int field = random().nextInt(5); - char value = distinctValues[random().nextInt(distinctValues.length)]; - switch(random().nextInt(10)) { - case 9: - case 8: - case 7: - case 6: - case 5: - hasMust = true; - if (rarely()) { - clauses[i] = new FilterClause(new EmptyFilter(), MUST); - topLevel.add(new BooleanClause(new MatchNoDocsQuery(), MUST)); - } else { - clauses[i] = newFilterClause(field, value, MUST, random().nextBoolean()); - topLevel.add(new BooleanClause(new TermQuery(new Term(String.valueOf(field), String.valueOf(value))), MUST)); - } - break; - case 4: - case 3: - case 2: - case 1: - hasShould = true; - if (rarely()) { - clauses[i] = new FilterClause(new EmptyFilter(), SHOULD); - orQuery.add(new BooleanClause(new MatchNoDocsQuery(), SHOULD)); - } else { - clauses[i] = newFilterClause(field, value, SHOULD, random().nextBoolean()); - orQuery.add(new BooleanClause(new TermQuery(new Term(String.valueOf(field), String.valueOf(value))), SHOULD)); - } - break; - case 0: - hasMustNot = true; - if (rarely()) { - clauses[i] = new FilterClause(new EmptyFilter(), MUST_NOT); - topLevel.add(new BooleanClause(new MatchNoDocsQuery(), MUST_NOT)); - } else { - clauses[i] = newFilterClause(field, value, MUST_NOT, random().nextBoolean()); - topLevel.add(new BooleanClause(new TermQuery(new Term(String.valueOf(field), String.valueOf(value))), MUST_NOT)); - } - break; - - } - } - if (orQuery.getClauses().length > 0) { - topLevel.add(new BooleanClause(orQuery, MUST)); - } - if (hasMustNot && !hasMust && !hasShould) { // pure negative - topLevel.add(new BooleanClause(new MatchAllDocsQuery(), MUST)); - } - XBooleanFilter booleanFilter = createBooleanFilter(clauses); - - FixedBitSet leftResult = new FixedBitSet(reader.maxDoc()); - FixedBitSet rightResult = new FixedBitSet(reader.maxDoc()); - DocIdSet left = booleanFilter.getDocIdSet(reader.getContext(), reader.getLiveDocs()); - DocIdSet right = new QueryWrapperFilter(topLevel).getDocIdSet(reader.getContext(), reader.getLiveDocs()); - if (left == null || right == null) { - if (left == null && right != null) { - assertThat(errorMsg(clauses, topLevel), (right.iterator() == null ? DocIdSetIterator.NO_MORE_DOCS : right.iterator().nextDoc()), equalTo(DocIdSetIterator.NO_MORE_DOCS)); - } - if (left != null && right == null) { - assertThat(errorMsg(clauses, topLevel), (left.iterator() == null ? DocIdSetIterator.NO_MORE_DOCS : left.iterator().nextDoc()), equalTo(DocIdSetIterator.NO_MORE_DOCS)); - } - } else { - DocIdSetIterator leftIter = left.iterator(); - DocIdSetIterator rightIter = right.iterator(); - if (leftIter != null) { - leftResult.or(leftIter); - } - - if (rightIter != null) { - rightResult.or(rightIter); - } - - assertThat(leftResult.cardinality(), equalTo(rightResult.cardinality())); - for (int i = 0; i < reader.maxDoc(); i++) { - assertThat(errorMsg(clauses, topLevel) + " -- failed at index " + i, leftResult.get(i), equalTo(rightResult.get(i))); - } - } - } - } - - private String errorMsg(FilterClause[] clauses, BooleanQuery query) { - return query.toString() + " vs. " + Arrays.toString(clauses); - } - - - public static final class PrettyPrintFieldCacheTermsFilter extends DocValuesTermsFilter { - - private final String value; - private final String field; - - public PrettyPrintFieldCacheTermsFilter(String field, String value) { - super(field, value); - this.field = field; - this.value = value; - } - - @Override - public String toString(String field) { - return "SLOW(" + field + ":" + value + ")"; - } - } - - public final class EmptyFilter extends Filter { - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - return random().nextBoolean() ? new Empty() : null; - } - - @Override - public String toString(String field) { - return "empty"; - } - - private class Empty extends DocIdSet { - - @Override - public DocIdSetIterator iterator() throws IOException { - return null; - } - - @Override - public long ramBytesUsed() { - return 0; - } - } - } - -} - diff --git a/src/test/java/org/elasticsearch/index/TermsFilterIntegrationTests.java b/src/test/java/org/elasticsearch/index/TermsFilterIntegrationTests.java deleted file mode 100644 index 25e96f7fedd..00000000000 --- a/src/test/java/org/elasticsearch/index/TermsFilterIntegrationTests.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index; - -import org.elasticsearch.common.logging.ESLogger; -import org.elasticsearch.common.logging.Loggers; -import org.elasticsearch.index.query.FilterBuilders; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.test.ElasticsearchIntegrationTest; - -import java.util.Arrays; - -import static org.elasticsearch.index.query.TermsFilterParser.*; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; - -public class TermsFilterIntegrationTests extends ElasticsearchIntegrationTest { - - private final ESLogger logger = Loggers.getLogger(TermsFilterIntegrationTests.class); - - public void testExecution() throws Exception { - assertAcked(prepareCreate("test").addMapping("type", "f", "type=string")); - ensureYellow(); - indexRandom(true, - client().prepareIndex("test", "type").setSource("f", new String[] {"a", "b", "c"}), - client().prepareIndex("test", "type").setSource("f", "b")); - - for (boolean cache : new boolean[] {false, true}) { - logger.info("cache=" + cache); - for (String execution : Arrays.asList( - EXECUTION_VALUE_PLAIN, - EXECUTION_VALUE_FIELDDATA, - EXECUTION_VALUE_BOOL, - EXECUTION_VALUE_BOOL_NOCACHE, - EXECUTION_VALUE_OR, - EXECUTION_VALUE_OR_NOCACHE)) { - logger.info("Execution=" + execution); - assertHitCount(client().prepareCount("test").setQuery( - QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), - FilterBuilders.termsFilter("f", "a", "b").execution(execution).cache(cache))).get(), 2L); - } - - for (String execution : Arrays.asList( - EXECUTION_VALUE_AND, - EXECUTION_VALUE_AND_NOCACHE)) { - logger.info("Execution=" + execution); - assertHitCount(client().prepareCount("test").setQuery( - QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), - FilterBuilders.termsFilter("f", "a", "b").execution(execution).cache(cache))).get(), 1L); - } - } - } - -} diff --git a/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java b/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java index 2e1fc9e8da7..95f95defec2 100644 --- a/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java +++ b/src/test/java/org/elasticsearch/index/aliases/IndexAliasesServiceTests.java @@ -66,8 +66,8 @@ public class IndexAliasesServiceTests extends ElasticsearchSingleNodeTest { assertThat(indexAliasesService.hasAlias("dogs"), equalTo(true)); assertThat(indexAliasesService.hasAlias("turtles"), equalTo(false)); - assertThat(indexAliasesService.aliasFilter("cats").toString(), equalTo("cache(animal:cat)")); - assertThat(indexAliasesService.aliasFilter("cats", "dogs").toString(), equalTo("BooleanFilter(cache(animal:cat) cache(animal:dog))")); + assertThat(indexAliasesService.aliasFilter("cats").toString(), equalTo("cache(QueryWrapperFilter(animal:cat))")); + assertThat(indexAliasesService.aliasFilter("cats", "dogs").toString(), equalTo("QueryWrapperFilter(cache(QueryWrapperFilter(animal:cat)) cache(QueryWrapperFilter(animal:dog)))")); // Non-filtering alias should turn off all filters because filters are ORed assertThat(indexAliasesService.aliasFilter("all"), nullValue()); @@ -76,7 +76,7 @@ public class IndexAliasesServiceTests extends ElasticsearchSingleNodeTest { indexAliasesService.add("cats", filter(termFilter("animal", "feline"))); indexAliasesService.add("dogs", filter(termFilter("animal", "canine"))); - assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("BooleanFilter(cache(animal:canine) cache(animal:feline))")); + assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("QueryWrapperFilter(cache(QueryWrapperFilter(animal:canine)) cache(QueryWrapperFilter(animal:feline)))")); } @Test @@ -86,13 +86,13 @@ public class IndexAliasesServiceTests extends ElasticsearchSingleNodeTest { indexAliasesService.add("dogs", filter(termFilter("animal", "dog"))); assertThat(indexAliasesService.aliasFilter(), nullValue()); - assertThat(indexAliasesService.aliasFilter("dogs").toString(), equalTo("cache(animal:dog)")); - assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("BooleanFilter(cache(animal:dog) cache(animal:cat))")); + assertThat(indexAliasesService.aliasFilter("dogs").toString(), equalTo("cache(QueryWrapperFilter(animal:dog))")); + assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("QueryWrapperFilter(cache(QueryWrapperFilter(animal:dog)) cache(QueryWrapperFilter(animal:cat)))")); indexAliasesService.add("cats", filter(termFilter("animal", "feline"))); indexAliasesService.add("dogs", filter(termFilter("animal", "canine"))); - assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("BooleanFilter(cache(animal:canine) cache(animal:feline))")); + assertThat(indexAliasesService.aliasFilter("dogs", "cats").toString(), equalTo("QueryWrapperFilter(cache(QueryWrapperFilter(animal:canine)) cache(QueryWrapperFilter(animal:feline)))")); } @Test(expected = InvalidAliasNameException.class) diff --git a/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTest.java b/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTest.java index b3d030560e8..72b0134b4ca 100644 --- a/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTest.java +++ b/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTest.java @@ -29,12 +29,13 @@ import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LogByteSizeMergePolicy; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.store.RAMDirectory; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.Index; import org.elasticsearch.test.ElasticsearchTestCase; @@ -71,7 +72,7 @@ public class BitSetFilterCacheTest extends ElasticsearchTestCase { IndexSearcher searcher = new IndexSearcher(reader); BitsetFilterCache cache = new BitsetFilterCache(new Index("test"), ImmutableSettings.EMPTY); - BitDocIdSetFilter filter = cache.getBitDocIdSetFilter(new TermFilter(new Term("field", "value"))); + BitDocIdSetFilter filter = cache.getBitDocIdSetFilter(Queries.wrap(new TermQuery(new Term("field", "value")))); TopDocs docs = searcher.search(new ConstantScoreQuery(filter), 1); assertThat(docs.totalHits, equalTo(3)); diff --git a/src/test/java/org/elasticsearch/index/fielddata/AbstractStringFieldDataTests.java b/src/test/java/org/elasticsearch/index/fielddata/AbstractStringFieldDataTests.java index a557e1cd07f..71d802abbb1 100644 --- a/src/test/java/org/elasticsearch/index/fielddata/AbstractStringFieldDataTests.java +++ b/src/test/java/org/elasticsearch/index/fielddata/AbstractStringFieldDataTests.java @@ -31,13 +31,14 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.RandomAccessOrds; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermsEnum; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopFieldDocs; import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter; import org.apache.lucene.search.join.ScoreMode; @@ -47,7 +48,7 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.UnicodeUtil; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested; @@ -405,8 +406,8 @@ public abstract class AbstractStringFieldDataTests extends AbstractFieldDataImpl missingValue = new BytesRef(TestUtil.randomSimpleString(getRandom())); break; } - Filter parentFilter = new TermFilter(new Term("type", "parent")); - Filter childFilter = new NotFilter(parentFilter); + Filter parentFilter = new QueryWrapperFilter(new TermQuery(new Term("type", "parent"))); + Filter childFilter = new QueryWrapperFilter(Queries.not(parentFilter)); Nested nested = createNested(parentFilter, childFilter); BytesRefFieldComparatorSource nestedComparatorSource = new BytesRefFieldComparatorSource(fieldData, missingValue, sortMode, nested); ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new FilteredQuery(new MatchAllDocsQuery(), childFilter), new BitDocIdSetCachingWrapperFilter(parentFilter), ScoreMode.None); diff --git a/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java index f2fc9552714..2d2e0acd203 100644 --- a/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java @@ -24,7 +24,8 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.Filter; -import org.apache.lucene.search.NumericRangeFilter; +import org.apache.lucene.search.NumericRangeQuery; +import org.apache.lucene.search.QueryWrapperFilter; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -243,10 +244,9 @@ public class SimpleDateMappingTests extends ElasticsearchSingleNodeTest { } finally { SearchContext.removeCurrent(); } - assertThat(filter, instanceOf(NumericRangeFilter.class)); - NumericRangeFilter rangeFilter = (NumericRangeFilter) filter; - assertThat(rangeFilter.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(11).millis()).getMillis())); - assertThat(rangeFilter.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis()).getMillis())); + NumericRangeQuery rangeQuery = (NumericRangeQuery) ((QueryWrapperFilter) filter).getQuery(); + assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(11).millis()).getMillis())); + assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis()).getMillis())); } public void testDayWithoutYearFormat() throws Exception { @@ -271,10 +271,9 @@ public class SimpleDateMappingTests extends ElasticsearchSingleNodeTest { } finally { SearchContext.removeCurrent(); } - assertThat(filter, instanceOf(NumericRangeFilter.class)); - NumericRangeFilter rangeFilter = (NumericRangeFilter) filter; - assertThat(rangeFilter.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(35).millis()).getMillis())); - assertThat(rangeFilter.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(34).millis()).getMillis())); + NumericRangeQuery rangeQuery = (NumericRangeQuery) ((QueryWrapperFilter) filter).getQuery(); + assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(35).millis()).getMillis())); + assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(34).millis()).getMillis())); } public void testIgnoreMalformedOption() throws Exception { diff --git a/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java index cc3f9f35c26..45b6cb9440a 100644 --- a/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java @@ -27,8 +27,9 @@ import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableFieldType; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; +import org.apache.lucene.search.QueryWrapperFilter; +import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; @@ -529,9 +530,9 @@ public class SimpleStringMappingTests extends ElasticsearchSingleNodeTest { FieldMapper mapper = defaultMapper.mappers().fullName("field").mapper(); assertNotNull(mapper); assertTrue(mapper instanceof StringFieldMapper); - assertEquals(Queries.MATCH_NO_FILTER, mapper.termsFilter(Collections.emptyList(), null)); - assertEquals(new TermFilter(new Term("field", "value")), mapper.termsFilter(Collections.singletonList("value"), null)); - assertEquals(new TermsFilter(new Term("field", "value1"), new Term("field", "value2")), mapper.termsFilter(Arrays.asList("value1", "value2"), null)); + assertEquals(Queries.newMatchNoDocsFilter(), mapper.termsFilter(Collections.emptyList(), null)); + assertEquals(new QueryWrapperFilter(new TermQuery(new Term("field", "value"))), mapper.termsFilter(Collections.singletonList("value"), null)); + assertEquals(new QueryWrapperFilter(new TermsQuery(new Term("field", "value1"), new Term("field", "value2"))), mapper.termsFilter(Arrays.asList("value1", "value2"), null)); } } diff --git a/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java b/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java index 6d29816f29e..6fc4a341d7b 100644 --- a/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java +++ b/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java @@ -31,10 +31,9 @@ import org.apache.lucene.index.TermsEnum; import org.apache.lucene.index.memory.MemoryIndex; import org.apache.lucene.queries.BoostingQuery; import org.apache.lucene.queries.ExtendedCommonTermsQuery; -import org.apache.lucene.queries.FilterClause; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.queries.TermsFilter; +import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.DisjunctionMaxQuery; @@ -43,9 +42,7 @@ import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MultiTermQuery; -import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; -import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryWrapperFilter; @@ -76,14 +73,8 @@ import org.elasticsearch.action.termvectors.TermVectorsResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedString; -import org.elasticsearch.common.lucene.search.AndFilter; -import org.elasticsearch.common.lucene.search.MatchAllDocsFilter; import org.elasticsearch.common.lucene.search.MoreLikeThisQuery; -import org.elasticsearch.common.lucene.search.NotFilter; -import org.elasticsearch.common.lucene.search.OrFilter; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.common.lucene.search.RegexpFilter; -import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.lucene.search.function.BoostScoreFunction; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.lucene.search.function.WeightFactorFunction; @@ -114,7 +105,6 @@ import org.junit.Test; import java.io.IOException; import java.lang.reflect.Field; import java.util.EnumSet; -import java.util.Iterator; import java.util.List; import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; @@ -150,9 +140,6 @@ import static org.elasticsearch.index.query.QueryBuilders.spanTermQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.QueryBuilders.termsQuery; import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery; -import static org.elasticsearch.index.query.RegexpFlag.COMPLEMENT; -import static org.elasticsearch.index.query.RegexpFlag.EMPTY; -import static org.elasticsearch.index.query.RegexpFlag.INTERSECTION; import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.factorFunction; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery; import static org.hamcrest.Matchers.closeTo; @@ -412,10 +399,7 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/starColonStar.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class)); - ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) parsedQuery; - Filter internalFilter = (Filter) constantScoreQuery.getQuery(); - assertThat(internalFilter, instanceOf(MatchAllDocsFilter.class)); + assertThat(parsedQuery, instanceOf(MatchAllDocsQuery.class)); } @Test @@ -622,8 +606,9 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), prefixFilter("name.first", "sh"))).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - PrefixFilter prefixFilter = (PrefixFilter) filteredQuery.getFilter(); - assertThat(prefixFilter.getPrefix(), equalTo(new Term("name.first", "sh"))); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + PrefixQuery prefixQuery = (PrefixQuery) filter.getQuery(); + assertThat(prefixQuery.getPrefix(), equalTo(new Term("name.first", "sh"))); } @Test @@ -633,8 +618,9 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - PrefixFilter prefixFilter = (PrefixFilter) filteredQuery.getFilter(); - assertThat(prefixFilter.getPrefix(), equalTo(new Term("name.first", "sh"))); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + PrefixQuery prefixQuery = (PrefixQuery) filter.getQuery(); + assertThat(prefixQuery.getPrefix(), equalTo(new Term("name.first", "sh"))); } @Test @@ -645,7 +631,8 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); - PrefixFilter prefixFilter = (PrefixFilter) filteredQuery.getFilter(); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + PrefixQuery prefixFilter = (PrefixQuery) filter.getQuery(); assertThat(prefixFilter.getPrefix(), equalTo(new Term("name.first", "sh"))); } @@ -716,10 +703,12 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); Filter filter = ((FilteredQuery) parsedQuery).getFilter(); - assertThat(filter, instanceOf(RegexpFilter.class)); - RegexpFilter regexpFilter = (RegexpFilter) filter; - assertThat(regexpFilter.field(), equalTo("name.first")); - assertThat(regexpFilter.regexp(), equalTo("s.*y")); + assertThat(filter, instanceOf(QueryWrapperFilter.class)); + Query q = ((QueryWrapperFilter) filter).getQuery(); + assertThat(q, instanceOf(RegexpQuery.class)); + RegexpQuery regexpQuery = (RegexpQuery) q; + assertThat(regexpQuery.getField(), equalTo("name.first")); + assertThat(regexpQuery.toString(), containsString("s.*y")); } @Test @@ -729,10 +718,12 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); Filter filter = ((FilteredQuery) parsedQuery).getFilter(); - assertThat(filter, instanceOf(RegexpFilter.class)); - RegexpFilter regexpFilter = (RegexpFilter) filter; - assertThat(regexpFilter.field(), equalTo("name.first")); - assertThat(regexpFilter.regexp(), equalTo("s.*y")); + assertThat(filter, instanceOf(QueryWrapperFilter.class)); + Query q = ((QueryWrapperFilter) filter).getQuery(); + assertThat(q, instanceOf(RegexpQuery.class)); + RegexpQuery regexpQuery = (RegexpQuery) q; + assertThat(regexpQuery.getField(), equalTo("name.first")); + assertThat(regexpQuery.toString(), containsString("s.*y")); } @Test @@ -743,10 +734,12 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); Filter filter = ((FilteredQuery) parsedQuery.query()).getFilter(); - assertThat(filter, instanceOf(RegexpFilter.class)); - RegexpFilter regexpFilter = (RegexpFilter) filter; - assertThat(regexpFilter.field(), equalTo("name.first")); - assertThat(regexpFilter.regexp(), equalTo("s.*y")); + assertThat(filter, instanceOf(QueryWrapperFilter.class)); + Query q = ((QueryWrapperFilter) filter).getQuery(); + assertThat(q, instanceOf(RegexpQuery.class)); + RegexpQuery regexpQuery = (RegexpQuery) q; + assertThat(regexpQuery.getField(), equalTo("name.first")); + assertThat(regexpQuery.toString(), containsString("s.*y")); } @Test @@ -756,11 +749,11 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { ParsedQuery parsedQuery = queryParser.parse(query); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); Filter filter = ((FilteredQuery) parsedQuery.query()).getFilter(); - assertThat(filter, instanceOf(RegexpFilter.class)); - RegexpFilter regexpFilter = (RegexpFilter) filter; - assertThat(regexpFilter.field(), equalTo("name.first")); - assertThat(regexpFilter.regexp(), equalTo("s.*y")); - assertThat(regexpFilter.flags(), equalTo(INTERSECTION.value() | COMPLEMENT.value() | EMPTY.value())); + assertThat(filter, instanceOf(QueryWrapperFilter.class)); + Query q = ((QueryWrapperFilter) filter).getQuery(); + assertThat(q, instanceOf(RegexpQuery.class)); + RegexpQuery regexpQuery = (RegexpQuery) q; + assertThat(regexpQuery.toString(), equalTo("name.first:/s.*y/")); } @Test @@ -770,11 +763,11 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { ParsedQuery parsedQuery = queryParser.parse(query); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); Filter filter = ((FilteredQuery) parsedQuery.query()).getFilter(); - assertThat(filter, instanceOf(RegexpFilter.class)); - RegexpFilter regexpFilter = (RegexpFilter) filter; - assertThat(regexpFilter.field(), equalTo("name.first")); - assertThat(regexpFilter.regexp(), equalTo("s.*y")); - assertThat(regexpFilter.flags(), equalTo(INTERSECTION.value() | COMPLEMENT.value() | EMPTY.value())); + assertThat(filter, instanceOf(QueryWrapperFilter.class)); + Query q = ((QueryWrapperFilter) filter).getQuery(); + assertThat(q, instanceOf(RegexpQuery.class)); + RegexpQuery regexpQuery = (RegexpQuery) q; + assertThat(regexpQuery.toString(), equalTo("name.first:/s.*y/")); } @Test @@ -866,16 +859,10 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { public void testRangeFilteredQueryBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), rangeFilter("age").from(23).to(54).includeLower(true).includeUpper(false))).query(); - // since age is automatically registered in data, we encode it as numeric - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - Filter filter = ((FilteredQuery) parsedQuery).getFilter(); - assertThat(filter, instanceOf(NumericRangeFilter.class)); - NumericRangeFilter rangeFilter = (NumericRangeFilter) filter; - assertThat(rangeFilter.getField(), equalTo("age")); - assertThat(rangeFilter.getMin().intValue(), equalTo(23)); - assertThat(rangeFilter.getMax().intValue(), equalTo(54)); - assertThat(rangeFilter.includesMin(), equalTo(true)); - assertThat(rangeFilter.includesMax(), equalTo(false)); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(NumericRangeQuery.newLongRange("age", 23L, 54L, true, false))); + assertEquals(expected, parsedQuery); } @Test @@ -883,33 +870,21 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/range-filter.json"); Query parsedQuery = queryParser.parse(query).query(); - // since age is automatically registered in data, we encode it as numeric - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - Filter filter = ((FilteredQuery) parsedQuery).getFilter(); - assertThat(filter, instanceOf(NumericRangeFilter.class)); - NumericRangeFilter rangeFilter = (NumericRangeFilter) filter; - assertThat(rangeFilter.getField(), equalTo("age")); - assertThat(rangeFilter.getMin().intValue(), equalTo(23)); - assertThat(rangeFilter.getMax().intValue(), equalTo(54)); - assertThat(rangeFilter.includesMin(), equalTo(true)); - assertThat(rangeFilter.includesMax(), equalTo(false)); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(NumericRangeQuery.newLongRange("age", 23L, 54L, true, false))); + assertEquals(expected, parsedQuery); } @Test public void testRangeNamedFilteredQuery() throws IOException { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/range-filter-named.json"); - ParsedQuery parsedQuery = queryParser.parse(query); - assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); - assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); - Filter filter = ((FilteredQuery) parsedQuery.query()).getFilter(); - assertThat(filter, instanceOf(NumericRangeFilter.class)); - NumericRangeFilter rangeFilter = (NumericRangeFilter) filter; - assertThat(rangeFilter.getField(), equalTo("age")); - assertThat(rangeFilter.getMin().intValue(), equalTo(23)); - assertThat(rangeFilter.getMax().intValue(), equalTo(54)); - assertThat(rangeFilter.includesMin(), equalTo(true)); - assertThat(rangeFilter.includesMax(), equalTo(false)); + Query parsedQuery = queryParser.parse(query).query(); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(NumericRangeQuery.newLongRange("age", 23L, 54L, true, false))); + assertEquals(expected, parsedQuery); } @Test @@ -932,32 +907,16 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), boolFilter().must(termFilter("name.first", "shay1"), termFilter("name.first", "shay4")).mustNot(termFilter("name.first", "shay2")).should(termFilter("name.first", "shay3")))).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - XBooleanFilter booleanFilter = (XBooleanFilter) filteredQuery.getFilter(); - - Iterator iterator = booleanFilter.iterator(); - assertThat(iterator.hasNext(), equalTo(true)); - FilterClause clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay1"))); - - assertThat(iterator.hasNext(), equalTo(true)); - clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay4"))); - - assertThat(iterator.hasNext(), equalTo(true)); - clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST_NOT)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay2"))); - - assertThat(iterator.hasNext(), equalTo(true)); - clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.SHOULD)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay3"))); - - assertThat(iterator.hasNext(), equalTo(false)); + BooleanQuery filter = new BooleanQuery(); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.MUST); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.MUST); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay2"))), Occur.MUST_NOT); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay3"))), Occur.SHOULD); + filter.setMinimumNumberShouldMatch(1); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(filter)); + assertEquals(expected, parsedQuery); } @@ -966,45 +925,27 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/bool-filter.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - XBooleanFilter booleanFilter = (XBooleanFilter) filteredQuery.getFilter(); - - Iterator iterator = booleanFilter.iterator(); - assertThat(iterator.hasNext(), equalTo(true)); - FilterClause clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay1"))); - - assertThat(iterator.hasNext(), equalTo(true)); - clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay4"))); - - assertThat(iterator.hasNext(), equalTo(true)); - clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.MUST_NOT)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay2"))); - - assertThat(iterator.hasNext(), equalTo(true)); - clause = iterator.next(); - assertThat(clause.getOccur(), equalTo(BooleanClause.Occur.SHOULD)); - assertThat(getTerm(clause.getFilter()), equalTo(new Term("name.first", "shay3"))); - - assertThat(iterator.hasNext(), equalTo(false)); + BooleanQuery filter = new BooleanQuery(); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.MUST); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.MUST); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay2"))), Occur.MUST_NOT); + filter.add(Queries.wrap(new TermQuery(new Term("name.first", "shay3"))), Occur.SHOULD); + filter.setMinimumNumberShouldMatch(1); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(filter)); + assertEquals(expected, parsedQuery); } @Test public void testAndFilteredQueryBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); Query parsedQuery = queryParser.parse(filteredQuery(matchAllQuery(), andFilter(termFilter("name.first", "shay1"), termFilter("name.first", "shay4")))).query(); - assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class)); - ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) parsedQuery; - - AndFilter andFilter = (AndFilter) constantScoreQuery.getQuery(); - assertThat(andFilter.filters().size(), equalTo(2)); - assertThat(getTerm(andFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(andFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + BooleanQuery and = new BooleanQuery(); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.MUST); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.MUST); + ConstantScoreQuery expected = new ConstantScoreQuery(Queries.wrap(and)); + assertEquals(expected, parsedQuery); } @Test @@ -1012,28 +953,27 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/and-filter.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - - AndFilter andFilter = (AndFilter) filteredQuery.getFilter(); - assertThat(andFilter.filters().size(), equalTo(2)); - assertThat(getTerm(andFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(andFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + BooleanQuery and = new BooleanQuery(); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.MUST); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.MUST); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(and)); + assertEquals(expected, parsedQuery); } @Test public void testAndNamedFilteredQuery() throws IOException { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/and-filter-named.json"); - ParsedQuery parsedQuery = queryParser.parse(query); - assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); - assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); - - AndFilter andFilter = (AndFilter) filteredQuery.getFilter(); - assertThat(andFilter.filters().size(), equalTo(2)); - assertThat(getTerm(andFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(andFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + Query parsedQuery = queryParser.parse(query).query(); + BooleanQuery and = new BooleanQuery(); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.MUST); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.MUST); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(and)); + assertEquals(expected, parsedQuery); } @Test @@ -1041,26 +981,24 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/and-filter2.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - - AndFilter andFilter = (AndFilter) filteredQuery.getFilter(); - assertThat(andFilter.filters().size(), equalTo(2)); - assertThat(getTerm(andFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(andFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + BooleanQuery and = new BooleanQuery(); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.MUST); + and.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.MUST); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(and)); + assertEquals(expected, parsedQuery); } @Test public void testOrFilteredQueryBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); Query parsedQuery = queryParser.parse(filteredQuery(matchAllQuery(), orFilter(termFilter("name.first", "shay1"), termFilter("name.first", "shay4")))).query(); - assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class)); - ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) parsedQuery; - - OrFilter andFilter = (OrFilter) constantScoreQuery.getQuery(); - assertThat(andFilter.filters().size(), equalTo(2)); - assertThat(getTerm(andFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(andFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + BooleanQuery or = new BooleanQuery(); + or.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.SHOULD); + or.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.SHOULD); + ConstantScoreQuery expected = new ConstantScoreQuery(Queries.wrap(or)); + assertEquals(expected, parsedQuery); } @Test @@ -1068,13 +1006,13 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/or-filter.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - - OrFilter orFilter = (OrFilter) filteredQuery.getFilter(); - assertThat(orFilter.filters().size(), equalTo(2)); - assertThat(getTerm(orFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(orFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + BooleanQuery or = new BooleanQuery(); + or.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.SHOULD); + or.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.SHOULD); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(or)); + assertEquals(expected, parsedQuery); } @Test @@ -1082,24 +1020,21 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/or-filter2.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - - OrFilter orFilter = (OrFilter) filteredQuery.getFilter(); - assertThat(orFilter.filters().size(), equalTo(2)); - assertThat(getTerm(orFilter.filters().get(0)), equalTo(new Term("name.first", "shay1"))); - assertThat(getTerm(orFilter.filters().get(1)), equalTo(new Term("name.first", "shay4"))); + BooleanQuery or = new BooleanQuery(); + or.add(Queries.wrap(new TermQuery(new Term("name.first", "shay1"))), Occur.SHOULD); + or.add(Queries.wrap(new TermQuery(new Term("name.first", "shay4"))), Occur.SHOULD); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(or)); + assertEquals(expected, parsedQuery); } @Test public void testNotFilteredQueryBuilder() throws IOException { IndexQueryParserService queryParser = queryParser(); Query parsedQuery = queryParser.parse(filteredQuery(matchAllQuery(), notFilter(termFilter("name.first", "shay1")))).query(); - assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class)); - ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) parsedQuery; - - NotFilter notFilter = (NotFilter) constantScoreQuery.getQuery(); - assertThat(getTerm(notFilter.filter()), equalTo(new Term("name.first", "shay1"))); + ConstantScoreQuery expected = new ConstantScoreQuery(Queries.wrap(Queries.not(Queries.wrap(new TermQuery(new Term("name.first", "shay1")))))); + assertEquals(expected, parsedQuery); } @Test @@ -1108,11 +1043,10 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { String query = copyToStringFromClasspath("/org/elasticsearch/index/query/not-filter.json"); Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(((TermQuery) filteredQuery.getQuery()).getTerm(), equalTo(new Term("name.first", "shay"))); - - NotFilter notFilter = (NotFilter) filteredQuery.getFilter(); - assertThat(getTerm(notFilter.filter()), equalTo(new Term("name.first", "shay1"))); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(Queries.not(Queries.wrap(new TermQuery(new Term("name.first", "shay1")))))); + assertEquals(expected, parsedQuery); } @Test @@ -1120,12 +1054,10 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/not-filter2.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(((TermQuery) filteredQuery.getQuery()).getTerm(), equalTo(new Term("name.first", "shay"))); - - NotFilter notFilter = (NotFilter) filteredQuery.getFilter(); - assertThat(getTerm(notFilter.filter()), equalTo(new Term("name.first", "shay1"))); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(Queries.not(Queries.wrap(new TermQuery(new Term("name.first", "shay1")))))); + assertEquals(expected, parsedQuery); } @Test @@ -1133,12 +1065,10 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { IndexQueryParserService queryParser = queryParser(); String query = copyToStringFromClasspath("/org/elasticsearch/index/query/not-filter3.json"); Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(FilteredQuery.class)); - FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(((TermQuery) filteredQuery.getQuery()).getTerm(), equalTo(new Term("name.first", "shay"))); - - NotFilter notFilter = (NotFilter) filteredQuery.getFilter(); - assertThat(getTerm(notFilter.filter()), equalTo(new Term("name.first", "shay1"))); + FilteredQuery expected = new FilteredQuery( + new TermQuery(new Term("name.first", "shay")), + Queries.wrap(Queries.not(Queries.wrap(new TermQuery(new Term("name.first", "shay1")))))); + assertEquals(expected, parsedQuery); } @Test @@ -1343,12 +1273,11 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; assertThat(((TermQuery) filteredQuery.getQuery()).getTerm(), equalTo(new Term("name.first", "shay"))); - Filter filter = filteredQuery.getFilter(); - assertThat(filter, instanceOf(NumericRangeFilter.class)); - NumericRangeFilter rangeFilter = (NumericRangeFilter) filter; - assertThat(rangeFilter.getField(), equalTo("age")); - assertThat(rangeFilter.getMin().intValue(), equalTo(23)); - assertThat(rangeFilter.getMax().intValue(), equalTo(54)); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + NumericRangeQuery rangeQuery = (NumericRangeQuery) filter.getQuery(); + assertThat(rangeQuery.getField(), equalTo("age")); + assertThat(rangeQuery.getMin().intValue(), equalTo(23)); + assertThat(rangeQuery.getMax().intValue(), equalTo(54)); } @Test @@ -1372,10 +1301,10 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(filteredQuery.getFilter(), instanceOf(TermFilter.class)); - TermFilter termFilter = (TermFilter) filteredQuery.getFilter(); - assertThat(getTerm(termFilter).field(), equalTo("name.last")); - assertThat(getTerm(termFilter).text(), equalTo("banon")); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + TermQuery termFilter = (TermQuery) filter.getQuery(); + assertThat(termFilter.getTerm().field(), equalTo("name.last")); + assertThat(termFilter.getTerm().text(), equalTo("banon")); } @Test @@ -1386,10 +1315,10 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); - assertThat(filteredQuery.getFilter(), instanceOf(TermFilter.class)); - TermFilter termFilter = (TermFilter) filteredQuery.getFilter(); - assertThat(getTerm(termFilter).field(), equalTo("name.last")); - assertThat(getTerm(termFilter).text(), equalTo("banon")); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + TermQuery termFilter = (TermQuery) filter.getQuery(); + assertThat(termFilter.getTerm().field(), equalTo("name.last")); + assertThat(termFilter.getTerm().text(), equalTo("banon")); } @Test @@ -1398,10 +1327,8 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(filteredQuery(termQuery("name.first", "shay"), termsFilter("name.last", "banon", "kimchy"))).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); - TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); - //assertThat(termsFilter.getTerms().length, equalTo(2)); - //assertThat(termsFilter.getTerms()[0].text(), equalTo("banon")); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + assertThat(filter.getQuery(), instanceOf(TermsQuery.class)); } @@ -1412,10 +1339,8 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(query).query(); assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; - assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); - TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); - //assertThat(termsFilter.getTerms().length, equalTo(2)); - //assertThat(termsFilter.getTerms()[0].text(), equalTo("banon")); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + assertThat(filter.getQuery(), instanceOf(TermsQuery.class)); } @Test @@ -1426,10 +1351,8 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); - assertThat(filteredQuery.getFilter(), instanceOf(TermsFilter.class)); - TermsFilter termsFilter = (TermsFilter) filteredQuery.getFilter(); - //assertThat(termsFilter.getTerms().length, equalTo(2)); - //assertThat(termsFilter.getTerms()[0].text(), equalTo("banon")); + QueryWrapperFilter filter = (QueryWrapperFilter) filteredQuery.getFilter(); + assertThat(filter.getQuery(), instanceOf(TermsQuery.class)); } @Test @@ -1467,8 +1390,7 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { Query parsedQuery = queryParser.parse(functionScoreQuery(factorFunction(1.3f))).query(); assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class)); FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery; - assertThat(functionScoreQuery.getSubQuery() instanceof ConstantScoreQuery, equalTo(true)); - assertThat(((ConstantScoreQuery) functionScoreQuery.getSubQuery()).getQuery() instanceof MatchAllDocsFilter, equalTo(true)); + assertThat(functionScoreQuery.getSubQuery() instanceof MatchAllDocsQuery, equalTo(true)); assertThat((double) ((BoostScoreFunction) functionScoreQuery.getFunction()).getBoost(), closeTo(1.3, 0.001)); } @@ -2427,10 +2349,12 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { XContentParser parser = XContentHelper.createParser(new BytesArray(query)); ParsedFilter parsedQuery = queryParser.parseInnerFilter(parser); assertThat(parsedQuery.filter(), instanceOf(QueryWrapperFilter.class)); + //QueryWrapperFilter filter = parsedQuery.filter(); assertThat(((QueryWrapperFilter) parsedQuery.filter()).getQuery(), instanceOf(FilteredQuery.class)); - assertThat(((FilteredQuery) ((QueryWrapperFilter) parsedQuery.filter()).getQuery()).getFilter(), instanceOf(TermFilter.class)); - TermFilter filter = (TermFilter) ((FilteredQuery) ((QueryWrapperFilter) parsedQuery.filter()).getQuery()).getFilter(); - assertThat(getTerm(filter).toString(), equalTo("text:apache")); + QueryWrapperFilter inner = (QueryWrapperFilter) ((FilteredQuery) ((QueryWrapperFilter) parsedQuery.filter()).getQuery()).getFilter(); + assertThat(inner.getQuery(), instanceOf(TermQuery.class)); + TermQuery filter = (TermQuery) inner.getQuery(); + assertThat(filter.getTerm().toString(), equalTo("text:apache")); } @Test @@ -2531,17 +2455,17 @@ public class SimpleIndexQueryParserTests extends ElasticsearchSingleNodeTest { assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class)); assertThat(((ConstantScoreQuery) parsedQuery).getQuery(), instanceOf(CustomQueryWrappingFilter.class)); assertThat(((CustomQueryWrappingFilter) ((ConstantScoreQuery) parsedQuery).getQuery()).getQuery(), instanceOf(ParentConstantScoreQuery.class)); - assertThat(((CustomQueryWrappingFilter) ((ConstantScoreQuery) parsedQuery).getQuery()).getQuery().toString(), equalTo("parent_filter[foo](filtered(*:*)->cache(_type:foo))")); + assertThat(((CustomQueryWrappingFilter) ((ConstantScoreQuery) parsedQuery).getQuery()).getQuery().toString(), equalTo("parent_filter[foo](filtered(*:*)->cache(QueryWrapperFilter(_type:foo)))")); SearchContext.removeCurrent(); } /** - * helper to extract term from TermFilter. - * @deprecated transition device: use TermQuery instead.*/ - @Deprecated + * helper to extract term from TermQuery. */ private Term getTerm(Query query) { - TermFilter filter = (TermFilter) query; - TermQuery wrapped = (TermQuery) filter.getQuery(); + while (query instanceof QueryWrapperFilter) { + query = ((QueryWrapperFilter) query).getQuery(); + } + TermQuery wrapped = (TermQuery) query; return wrapped.getTerm(); } } diff --git a/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java b/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java index 36611e1b43c..654a377a4f1 100644 --- a/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java +++ b/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterService; @@ -116,7 +117,7 @@ public class TemplateQueryParserTest extends ElasticsearchTestCase { TemplateQueryParser parser = injector.getInstance(TemplateQueryParser.class); Query query = parser.parse(context); - assertTrue("Parsing template query failed.", query instanceof ConstantScoreQuery); + assertTrue("Parsing template query failed.", query instanceof MatchAllDocsQuery); } @Test @@ -128,6 +129,6 @@ public class TemplateQueryParserTest extends ElasticsearchTestCase { TemplateQueryParser parser = injector.getInstance(TemplateQueryParser.class); Query query = parser.parse(context); - assertTrue("Parsing template query failed.", query instanceof ConstantScoreQuery); + assertTrue("Parsing template query failed.", query instanceof MatchAllDocsQuery); } } diff --git a/src/test/java/org/elasticsearch/index/query/fquery-with-empty-bool-query.json b/src/test/java/org/elasticsearch/index/query/fquery-with-empty-bool-query.json index 58efd910a76..6a6a48c9edd 100644 --- a/src/test/java/org/elasticsearch/index/query/fquery-with-empty-bool-query.json +++ b/src/test/java/org/elasticsearch/index/query/fquery-with-empty-bool-query.json @@ -3,7 +3,9 @@ "query": { "filtered": { "query": { - "bool": {} + "term": { + "text": "apache" + } }, "filter": { "term": { @@ -13,4 +15,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java index 259ed60ae4e..0fce3aa691a 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java @@ -25,8 +25,16 @@ import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermFilter; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.PostingsEnum; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.SlowCompositeReaderWrapper; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryUtils; @@ -37,6 +45,7 @@ import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; import org.elasticsearch.common.lease.Releasables; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; import org.elasticsearch.index.mapper.Uid; @@ -57,8 +66,13 @@ import java.util.NavigableSet; import java.util.Random; import java.util.TreeSet; -import static org.elasticsearch.index.query.FilterBuilders.*; -import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.index.query.FilterBuilders.hasChildFilter; +import static org.elasticsearch.index.query.FilterBuilders.notFilter; +import static org.elasticsearch.index.query.FilterBuilders.termFilter; +import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; +import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; +import static org.elasticsearch.index.query.QueryBuilders.hasChildQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.hamcrest.Matchers.equalTo; public class ChildrenConstantScoreQueryTests extends AbstractChildTests { @@ -80,7 +94,7 @@ public class ChildrenConstantScoreQueryTests extends AbstractChildTests { Query childQuery = new TermQuery(new Term("field", "value")); ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper(); ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper); - BitDocIdSetFilter parentFilter = wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "parent"))); + BitDocIdSetFilter parentFilter = wrapWithBitSetFilter(Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, "parent")))); Query query = new ChildrenConstantScoreQuery(parentChildIndexFieldData, childQuery, "parent", "child", parentFilter, 12, wrapWithBitSetFilter(NonNestedDocsFilter.INSTANCE)); QueryUtils.check(query); } @@ -113,7 +127,7 @@ public class ChildrenConstantScoreQueryTests extends AbstractChildTests { )); TermQuery childQuery = new TermQuery(new Term("field1", "value" + (1 + random().nextInt(3)))); - BitDocIdSetFilter parentFilter = wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "parent"))); + BitDocIdSetFilter parentFilter = wrapWithBitSetFilter(Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, "parent")))); int shortCircuitParentDocSet = random().nextInt(5); ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper(); ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper); diff --git a/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java index d5c91ad862d..07938b53e3f 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java @@ -28,14 +28,33 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.DoubleField; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.search.*; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.PostingsEnum; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.SlowCompositeReaderWrapper; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.LeafCollector; +import org.apache.lucene.search.MultiCollector; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryUtils; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TopDocs; +import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.store.Directory; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.common.lease.Releasables; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; import org.elasticsearch.index.mapper.Uid; @@ -54,10 +73,20 @@ import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; -import java.util.*; +import java.util.Locale; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Random; +import java.util.TreeMap; -import static org.elasticsearch.index.query.FilterBuilders.*; -import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.index.query.FilterBuilders.notFilter; +import static org.elasticsearch.index.query.FilterBuilders.termFilter; +import static org.elasticsearch.index.query.FilterBuilders.typeFilter; +import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; +import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; +import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery; +import static org.elasticsearch.index.query.QueryBuilders.hasChildQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.lessThanOrEqualTo; @@ -81,7 +110,7 @@ public class ChildrenQueryTests extends AbstractChildTests { ScoreType scoreType = ScoreType.values()[random().nextInt(ScoreType.values().length)]; ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper(); ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper); - BitDocIdSetFilter parentFilter = wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "parent"))); + BitDocIdSetFilter parentFilter = wrapWithBitSetFilter(Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, "parent")))); int minChildren = random().nextInt(10); int maxChildren = scaledRandomIntBetween(minChildren, 10); Query query = new ChildrenQuery(parentChildIndexFieldData, "parent", "child", parentFilter, childQuery, scoreType, minChildren, diff --git a/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java index 48451930579..49496d8f6e6 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java @@ -25,8 +25,16 @@ import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermFilter; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.PostingsEnum; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.SlowCompositeReaderWrapper; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryUtils; @@ -36,6 +44,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.common.lease.Releasables; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; import org.elasticsearch.index.mapper.Uid; @@ -55,8 +64,13 @@ import java.util.NavigableSet; import java.util.Random; import java.util.TreeSet; -import static org.elasticsearch.index.query.FilterBuilders.*; -import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.index.query.FilterBuilders.hasParentFilter; +import static org.elasticsearch.index.query.FilterBuilders.notFilter; +import static org.elasticsearch.index.query.FilterBuilders.termFilter; +import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; +import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; +import static org.elasticsearch.index.query.QueryBuilders.hasParentQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; /** */ @@ -79,7 +93,7 @@ public class ParentConstantScoreQueryTests extends AbstractChildTests { Query parentQuery = new TermQuery(new Term("field", "value")); ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper(); ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper); - BitDocIdSetFilter childrenFilter = wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "child"))); + BitDocIdSetFilter childrenFilter = wrapWithBitSetFilter(Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, "child")))); Query query = new ParentConstantScoreQuery(parentChildIndexFieldData, parentQuery, "parent", childrenFilter); QueryUtils.check(query); } diff --git a/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java index 218b2a514db..79b4a9bc79e 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java @@ -26,14 +26,30 @@ import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.search.*; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.PostingsEnum; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.SlowCompositeReaderWrapper; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.LeafCollector; +import org.apache.lucene.search.MultiCollector; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryUtils; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.store.Directory; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.common.lease.Releasables; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; import org.elasticsearch.index.mapper.Uid; @@ -56,7 +72,10 @@ import java.util.TreeMap; import static org.elasticsearch.index.query.FilterBuilders.notFilter; import static org.elasticsearch.index.query.FilterBuilders.termFilter; -import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; +import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; +import static org.elasticsearch.index.query.QueryBuilders.hasParentQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; public class ParentQueryTests extends AbstractChildTests { @@ -77,7 +96,7 @@ public class ParentQueryTests extends AbstractChildTests { Query parentQuery = new TermQuery(new Term("field", "value")); ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper(); ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper); - BitDocIdSetFilter childrenFilter = wrapWithBitSetFilter(new TermFilter(new Term(TypeFieldMapper.NAME, "child"))); + BitDocIdSetFilter childrenFilter = wrapWithBitSetFilter(Queries.wrap(new TermQuery(new Term(TypeFieldMapper.NAME, "child")))); Query query = new ParentQuery(parentChildIndexFieldData, parentQuery, "parent", childrenFilter); QueryUtils.check(query); } diff --git a/src/test/java/org/elasticsearch/index/search/nested/AbstractNumberNestedSortingTests.java b/src/test/java/org/elasticsearch/index/search/nested/AbstractNumberNestedSortingTests.java index db413fc462e..4af03801c94 100644 --- a/src/test/java/org/elasticsearch/index/search/nested/AbstractNumberNestedSortingTests.java +++ b/src/test/java/org/elasticsearch/index/search/nested/AbstractNumberNestedSortingTests.java @@ -24,7 +24,6 @@ import org.apache.lucene.document.StringField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; import org.apache.lucene.search.FieldDoc; import org.apache.lucene.search.Filter; import org.apache.lucene.search.FilteredQuery; @@ -39,7 +38,7 @@ import org.apache.lucene.search.TopFieldDocs; import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter; import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ToParentBlockJoinQuery; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.fielddata.AbstractFieldDataTests; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource; @@ -217,8 +216,8 @@ public abstract class AbstractNumberNestedSortingTests extends AbstractFieldData MultiValueMode sortMode = MultiValueMode.SUM; IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false)); - Filter parentFilter = new TermFilter(new Term("__type", "parent")); - Filter childFilter = new NotFilter(parentFilter); + Filter parentFilter = Queries.wrap(new TermQuery(new Term("__type", "parent"))); + Filter childFilter = Queries.wrap(Queries.not(parentFilter)); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, null, createNested(parentFilter, childFilter)); ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new FilteredQuery(new MatchAllDocsQuery(), childFilter), new BitDocIdSetCachingWrapperFilter(parentFilter), ScoreMode.None); @@ -252,7 +251,7 @@ public abstract class AbstractNumberNestedSortingTests extends AbstractFieldData assertThat(topDocs.scoreDocs[4].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9)); - childFilter = new TermFilter(new Term("filter_1", "T")); + childFilter = Queries.wrap(new TermQuery(new Term("filter_1", "T"))); nestedComparatorSource = createFieldComparator("field2", sortMode, null, createNested(parentFilter, childFilter)); query = new ToParentBlockJoinQuery( new FilteredQuery(new MatchAllDocsQuery(), childFilter), @@ -329,7 +328,7 @@ public abstract class AbstractNumberNestedSortingTests extends AbstractFieldData protected void assertAvgScoreMode(Filter parentFilter, IndexSearcher searcher) throws IOException { MultiValueMode sortMode = MultiValueMode.AVG; - Filter childFilter = new NotFilter(parentFilter); + Filter childFilter = Queries.wrap(Queries.not(parentFilter)); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(parentFilter, childFilter)); Query query = new ToParentBlockJoinQuery(new FilteredQuery(new MatchAllDocsQuery(), childFilter), new BitDocIdSetCachingWrapperFilter(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("field2", nestedComparatorSource)); diff --git a/src/test/java/org/elasticsearch/index/search/nested/DoubleNestedSortingTests.java b/src/test/java/org/elasticsearch/index/search/nested/DoubleNestedSortingTests.java index b1e91b95787..800320323cc 100644 --- a/src/test/java/org/elasticsearch/index/search/nested/DoubleNestedSortingTests.java +++ b/src/test/java/org/elasticsearch/index/search/nested/DoubleNestedSortingTests.java @@ -33,7 +33,7 @@ import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter; import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ToParentBlockJoinQuery; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource; @@ -69,7 +69,7 @@ public class DoubleNestedSortingTests extends AbstractNumberNestedSortingTests { @Override protected void assertAvgScoreMode(Filter parentFilter, IndexSearcher searcher) throws IOException { MultiValueMode sortMode = MultiValueMode.AVG; - Filter childFilter = new NotFilter(parentFilter); + Filter childFilter = Queries.wrap(Queries.not(parentFilter)); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(parentFilter, childFilter)); Query query = new ToParentBlockJoinQuery(new FilteredQuery(new MatchAllDocsQuery(), childFilter), new BitDocIdSetCachingWrapperFilter(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("field2", nestedComparatorSource)); diff --git a/src/test/java/org/elasticsearch/index/search/nested/FloatNestedSortingTests.java b/src/test/java/org/elasticsearch/index/search/nested/FloatNestedSortingTests.java index ee2f8cf809c..b1b1433cdfc 100644 --- a/src/test/java/org/elasticsearch/index/search/nested/FloatNestedSortingTests.java +++ b/src/test/java/org/elasticsearch/index/search/nested/FloatNestedSortingTests.java @@ -33,7 +33,7 @@ import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter; import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ToParentBlockJoinQuery; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource; @@ -68,7 +68,7 @@ public class FloatNestedSortingTests extends DoubleNestedSortingTests { protected void assertAvgScoreMode(Filter parentFilter, IndexSearcher searcher, IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException { MultiValueMode sortMode = MultiValueMode.AVG; - Filter childFilter = new NotFilter(parentFilter); + Filter childFilter = Queries.wrap(Queries.not(parentFilter)); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(parentFilter, childFilter)); Query query = new ToParentBlockJoinQuery(new FilteredQuery(new MatchAllDocsQuery(), childFilter), new BitDocIdSetCachingWrapperFilter(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("field2", nestedComparatorSource)); diff --git a/src/test/java/org/elasticsearch/index/search/nested/NestedSortingTests.java b/src/test/java/org/elasticsearch/index/search/nested/NestedSortingTests.java index b2fc6f65eeb..1029523a325 100644 --- a/src/test/java/org/elasticsearch/index/search/nested/NestedSortingTests.java +++ b/src/test/java/org/elasticsearch/index/search/nested/NestedSortingTests.java @@ -24,7 +24,8 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.FieldDoc; import org.apache.lucene.search.Filter; @@ -34,6 +35,7 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopFieldDocs; import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter; @@ -41,8 +43,7 @@ import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ToParentBlockJoinQuery; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.TestUtil; -import org.elasticsearch.common.lucene.search.AndFilter; -import org.elasticsearch.common.lucene.search.NotFilter; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.fielddata.AbstractFieldDataTests; import org.elasticsearch.index.fielddata.FieldDataType; @@ -116,8 +117,8 @@ public class NestedSortingTests extends AbstractFieldDataTests { } private TopDocs getTopDocs(IndexSearcher searcher, IndexFieldData indexFieldData, String missingValue, MultiValueMode sortMode, int n, boolean reverse) throws IOException { - Filter parentFilter = new TermFilter(new Term("__type", "parent")); - Filter childFilter = new TermFilter(new Term("__type", "child")); + Filter parentFilter = Queries.wrap(new TermQuery(new Term("__type", "parent"))); + Filter childFilter = Queries.wrap(new TermQuery(new Term("__type", "child"))); XFieldComparatorSource nestedComparatorSource = indexFieldData.comparatorSource(missingValue, sortMode, createNested(parentFilter, childFilter)); Query query = new ConstantScoreQuery(parentFilter); Sort sort = new Sort(new SortField("f", nestedComparatorSource, reverse)); @@ -282,8 +283,8 @@ public class NestedSortingTests extends AbstractFieldDataTests { MultiValueMode sortMode = MultiValueMode.MIN; IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false)); PagedBytesIndexFieldData indexFieldData = getForField("field2"); - Filter parentFilter = new TermFilter(new Term("__type", "parent")); - Filter childFilter = new NotFilter(parentFilter); + Filter parentFilter = Queries.wrap(new TermQuery(new Term("__type", "parent"))); + Filter childFilter = Queries.wrap(Queries.not(parentFilter)); BytesRefFieldComparatorSource nestedComparatorSource = new BytesRefFieldComparatorSource(indexFieldData, null, sortMode, createNested(parentFilter, childFilter)); ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new FilteredQuery(new MatchAllDocsQuery(), childFilter), new BitDocIdSetCachingWrapperFilter(parentFilter), ScoreMode.None); @@ -320,7 +321,10 @@ public class NestedSortingTests extends AbstractFieldDataTests { assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("g")); - childFilter = new AndFilter(Arrays.asList(new NotFilter(parentFilter), new TermFilter(new Term("filter_1", "T")))); + BooleanQuery bq = new BooleanQuery(); + bq.add(parentFilter, Occur.MUST_NOT); + bq.add(new TermQuery(new Term("filter_1", "T")), Occur.MUST); + childFilter = Queries.wrap(bq); nestedComparatorSource = new BytesRefFieldComparatorSource(indexFieldData, null, sortMode, createNested(parentFilter, childFilter)); query = new ToParentBlockJoinQuery( new FilteredQuery(new MatchAllDocsQuery(), childFilter), diff --git a/src/test/java/org/elasticsearch/indices/stats/IndexStatsTests.java b/src/test/java/org/elasticsearch/indices/stats/IndexStatsTests.java index cd43c297dc6..df7ba00d390 100644 --- a/src/test/java/org/elasticsearch/indices/stats/IndexStatsTests.java +++ b/src/test/java/org/elasticsearch/indices/stats/IndexStatsTests.java @@ -22,8 +22,12 @@ package org.elasticsearch.indices.stats; import org.apache.lucene.util.Version; import org.apache.lucene.util.LuceneTestCase.SuppressCodecs; import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; -import org.elasticsearch.action.admin.indices.stats.*; +import org.elasticsearch.action.admin.indices.stats.CommonStats; +import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags; import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag; +import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder; +import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; +import org.elasticsearch.action.admin.indices.stats.ShardStats; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchResponse; @@ -34,8 +38,8 @@ import org.elasticsearch.common.io.stream.BytesStreamInput; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.cache.filter.AutoFilterCachingPolicy; import org.elasticsearch.index.cache.filter.FilterCacheModule; +import org.elasticsearch.index.cache.filter.FilterCacheModule.FilterCacheSettings; import org.elasticsearch.index.cache.filter.weighted.WeightedFilterCache; import org.elasticsearch.index.merge.policy.TieredMergePolicyProvider; import org.elasticsearch.index.merge.scheduler.ConcurrentMergeSchedulerProvider; @@ -58,7 +62,12 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; @ClusterScope(scope = Scope.SUITE, numDataNodes = 2, numClientNodes = 0, randomDynamicTemplates = false) @SuppressCodecs("*") // requires custom completion format @@ -70,7 +79,7 @@ public class IndexStatsTests extends ElasticsearchIntegrationTest { return ImmutableSettings.settingsBuilder().put(super.nodeSettings(nodeOrdinal)) .put("indices.cache.filter.clean_interval", "1ms") .put(IndicesQueryCache.INDICES_CACHE_QUERY_CLEAN_INTERVAL, "1ms") - .put(AutoFilterCachingPolicy.AGGRESSIVE_CACHING_SETTINGS) + .put(FilterCacheSettings.FILTER_CACHE_EVERYTHING, true) .put(FilterCacheModule.FilterCacheSettings.FILTER_CACHE_TYPE, WeightedFilterCache.class) .build(); } diff --git a/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java b/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java index f7a254389e8..c7456fa3a3a 100644 --- a/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java +++ b/src/test/java/org/elasticsearch/nested/SimpleNestedTests.java @@ -532,7 +532,7 @@ public class SimpleNestedTests extends ElasticsearchIntegrationTest { assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); Explanation explanation = searchResponse.getHits().hits()[0].explanation(); assertThat(explanation.getValue(), equalTo(2f)); - assertThat(explanation.getDescription(), equalTo("Score based on child doc range from 0 to 1")); + assertThat(explanation.toString(), equalTo("2.0 = (MATCH) sum of:\n 2.0 = (MATCH) Score based on child doc range from 0 to 1\n 0.0 = match on required clause, product of:\n 0.0 = # clause\n 0.0 = (MATCH) Match on id 2\n")); // TODO: Enable when changes from BlockJoinQuery#explain are added to Lucene (Most likely version 4.2) // assertThat(explanation.getDetails().length, equalTo(2)); // assertThat(explanation.getDetails()[0].getValue(), equalTo(1f)); diff --git a/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTest.java b/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTest.java index 9a9060c9ed0..b84716bf557 100644 --- a/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTest.java +++ b/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTest.java @@ -21,21 +21,24 @@ package org.elasticsearch.search.aggregations.bucket.nested; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; -import org.apache.lucene.index.*; -import org.apache.lucene.queries.TermFilter; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.NoMergePolicy; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; -import org.apache.lucene.search.FilterCachingPolicy; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.compress.CompressedString; -import org.elasticsearch.common.lucene.search.AndFilter; -import org.elasticsearch.common.lucene.search.NotFilter; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.index.search.nested.NonNestedDocsFilter; -import org.elasticsearch.search.aggregations.AggregationPhase; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.BucketCollector; @@ -120,7 +123,7 @@ public class NestedAggregatorTest extends ElasticsearchSingleNodeTest { AggregationContext context = new AggregationContext(searchContext); AggregatorFactories.Builder builder = AggregatorFactories.builder(); - builder.add(new NestedAggregator.Factory("test", "nested_field", FilterCachingPolicy.ALWAYS_CACHE)); + builder.add(new NestedAggregator.Factory("test", "nested_field", QueryCachingPolicy.ALWAYS_CACHE)); AggregatorFactories factories = builder.build(); searchContext.aggregations(new SearchContextAggregations(factories)); Aggregator[] aggs = factories.createTopLevelAggregators(context); @@ -129,7 +132,10 @@ public class NestedAggregatorTest extends ElasticsearchSingleNodeTest { // A regular search always exclude nested docs, so we use NonNestedDocsFilter.INSTANCE here (otherwise MatchAllDocsQuery would be sufficient) // We exclude root doc with uid type#2, this will trigger the bug if we don't reset the root doc when we process a new segment, because // root doc type#3 and root doc type#1 have the same segment docid - searcher.search(new ConstantScoreQuery(new AndFilter(Arrays.asList(NonNestedDocsFilter.INSTANCE, new NotFilter(new TermFilter(new Term(UidFieldMapper.NAME, "type#2")))))), collector); + BooleanQuery bq = new BooleanQuery(); + bq.add(NonNestedDocsFilter.INSTANCE, Occur.MUST); + bq.add(new TermQuery(new Term(UidFieldMapper.NAME, "type#2")), Occur.MUST_NOT); + searcher.search(new ConstantScoreQuery(bq), collector); collector.postCollection(); Nested nested = (Nested) aggs[0].buildAggregation(0); diff --git a/src/test/java/org/elasticsearch/search/child/SimpleChildQuerySearchTests.java b/src/test/java/org/elasticsearch/search/child/SimpleChildQuerySearchTests.java index 10795adc37a..bbb4d01f96d 100644 --- a/src/test/java/org/elasticsearch/search/child/SimpleChildQuerySearchTests.java +++ b/src/test/java/org/elasticsearch/search/child/SimpleChildQuerySearchTests.java @@ -38,8 +38,8 @@ import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.cache.filter.AutoFilterCachingPolicy; import org.elasticsearch.index.cache.filter.FilterCacheModule; +import org.elasticsearch.index.cache.filter.FilterCacheModule.FilterCacheSettings; import org.elasticsearch.index.cache.filter.weighted.WeightedFilterCache; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.FieldMapper.Loading; @@ -128,7 +128,7 @@ public class SimpleChildQuerySearchTests extends ElasticsearchIntegrationTest { return ImmutableSettings.settingsBuilder().put(super.nodeSettings(nodeOrdinal)) // aggressive filter caching so that we can assert on the filter cache size .put(FilterCacheModule.FilterCacheSettings.FILTER_CACHE_TYPE, WeightedFilterCache.class) - .put(AutoFilterCachingPolicy.AGGRESSIVE_CACHING_SETTINGS) + .put(FilterCacheSettings.FILTER_CACHE_EVERYTHING, true) .build(); } @@ -852,7 +852,7 @@ public class SimpleChildQuerySearchTests extends ElasticsearchIntegrationTest { .setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreType("max")) .get(); assertThat(explainResponse.isExists(), equalTo(true)); - assertThat(explainResponse.getExplanation().getDescription(), equalTo("not implemented yet...")); + assertThat(explainResponse.getExplanation().toString(), equalTo("1.0 = (MATCH) sum of:\n 1.0 = not implemented yet...\n 0.0 = match on required clause, product of:\n 0.0 = # clause\n 0.0 = (MATCH) Match on id 0\n")); } List createDocBuilders() { @@ -1115,7 +1115,7 @@ public class SimpleChildQuerySearchTests extends ElasticsearchIntegrationTest { )).get(); assertSearchHit(searchResponse, 1, hasId("1")); // Can't start with ConstantScore(cache(BooleanFilter( - assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), startsWith("ConstantScore(BooleanFilter(")); + assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), startsWith("ConstantScore(CustomQueryWrappingFilter(")); searchResponse = client().prepareSearch("test") .setExplain(true) @@ -1125,7 +1125,7 @@ public class SimpleChildQuerySearchTests extends ElasticsearchIntegrationTest { )).get(); assertSearchHit(searchResponse, 1, hasId("1")); // Can't start with ConstantScore(cache(BooleanFilter( - assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), startsWith("ConstantScore(BooleanFilter(")); + assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), startsWith("ConstantScore(CustomQueryWrappingFilter(")); } } diff --git a/src/test/java/org/elasticsearch/search/fetch/innerhits/NestedChildrenFilterTest.java b/src/test/java/org/elasticsearch/search/fetch/innerhits/NestedChildrenFilterTest.java index 0edc47a0123..8e390c4dcfd 100644 --- a/src/test/java/org/elasticsearch/search/fetch/innerhits/NestedChildrenFilterTest.java +++ b/src/test/java/org/elasticsearch/search/fetch/innerhits/NestedChildrenFilterTest.java @@ -27,11 +27,16 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; -import org.apache.lucene.queries.TermFilter; -import org.apache.lucene.search.*; +import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.Filter; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TotalHitCountCollector; import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter; import org.apache.lucene.search.join.BitDocIdSetFilter; import org.apache.lucene.store.Directory; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.search.fetch.FetchSubPhase; import org.elasticsearch.search.fetch.innerhits.InnerHitsContext.NestedInnerHits.NestedChildrenFilter; import org.elasticsearch.test.ElasticsearchTestCase; @@ -74,8 +79,8 @@ public class NestedChildrenFilterTest extends ElasticsearchTestCase { IndexSearcher searcher = new IndexSearcher(reader); FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext(); - BitDocIdSetFilter parentFilter = new BitDocIdSetCachingWrapperFilter(new TermFilter(new Term("type", "parent"))); - Filter childFilter = new TermFilter(new Term("type", "child")); + BitDocIdSetFilter parentFilter = new BitDocIdSetCachingWrapperFilter(Queries.wrap(new TermQuery(new Term("type", "parent")))); + Filter childFilter = Queries.wrap(new TermQuery(new Term("type", "child"))); int checkedParents = 0; for (LeafReaderContext leaf : reader.leaves()) { DocIdSetIterator parents = parentFilter.getDocIdSet(leaf).iterator(); diff --git a/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java b/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java index cf0468732b5..5c7859a57d8 100644 --- a/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java +++ b/src/test/java/org/elasticsearch/search/functionscore/FunctionScoreTests.java @@ -179,7 +179,7 @@ public class FunctionScoreTests extends ElasticsearchIntegrationTest { ).explain(true))).actionGet(); assertThat(responseWithWeights.getHits().getAt(0).getExplanation().toString(), - equalTo("6.0 = (MATCH) function score, product of:\n 1.0 = (MATCH) ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 6.0 = (MATCH) Math.min of\n 6.0 = (MATCH) function score, score mode [multiply]\n 1.0 = (MATCH) function score, product of:\n 1.0 = match filter: *:*\n 1.0 = (MATCH) Function for field geo_point_field:\n 1.0 = exp(-0.5*pow(MIN of: [Math.max(arcDistance([10.0, 20.0](=doc value),[10.0, 20.0](=origin)) - 0.0(=offset), 0)],2.0)/7.213475204444817E11)\n 2.0 = (MATCH) function score, product of:\n 1.0 = match filter: *:*\n 2.0 = (MATCH) product of:\n 1.0 = field value function: ln(doc['double_field'].value * factor=1.0)\n 2.0 = weight\n 3.0 = (MATCH) function score, product of:\n 1.0 = match filter: *:*\n 3.0 = (MATCH) product of:\n 1.0 = script score function, computed with script:\"_index['text_field']['value'].tf()\n 1.0 = _score: \n 1.0 = (MATCH) ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 3.0 = weight\n 3.4028235E38 = maxBoost\n 1.0 = queryBoost\n") + equalTo("6.0 = (MATCH) function score, product of:\n 1.0 = (MATCH) ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 6.0 = (MATCH) Math.min of\n 6.0 = (MATCH) function score, score mode [multiply]\n 1.0 = (MATCH) function score, product of:\n 1.0 = match filter: QueryWrapperFilter(*:*)\n 1.0 = (MATCH) Function for field geo_point_field:\n 1.0 = exp(-0.5*pow(MIN of: [Math.max(arcDistance([10.0, 20.0](=doc value),[10.0, 20.0](=origin)) - 0.0(=offset), 0)],2.0)/7.213475204444817E11)\n 2.0 = (MATCH) function score, product of:\n 1.0 = match filter: QueryWrapperFilter(*:*)\n 2.0 = (MATCH) product of:\n 1.0 = field value function: ln(doc['double_field'].value * factor=1.0)\n 2.0 = weight\n 3.0 = (MATCH) function score, product of:\n 1.0 = match filter: QueryWrapperFilter(*:*)\n 3.0 = (MATCH) product of:\n 1.0 = script score function, computed with script:\"_index['text_field']['value'].tf()\n 1.0 = _score: \n 1.0 = (MATCH) ConstantScore(text_field:value), product of:\n 1.0 = boost\n 1.0 = queryNorm\n 3.0 = weight\n 3.4028235E38 = maxBoost\n 1.0 = queryBoost\n") ); responseWithWeights = client().search( searchRequest().source( diff --git a/src/test/java/org/elasticsearch/search/highlight/HighlighterSearchTests.java b/src/test/java/org/elasticsearch/search/highlight/HighlighterSearchTests.java index 1834d9d63df..9de55b2a304 100644 --- a/src/test/java/org/elasticsearch/search/highlight/HighlighterSearchTests.java +++ b/src/test/java/org/elasticsearch/search/highlight/HighlighterSearchTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.highlight; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.google.common.base.Joiner; import com.google.common.collect.Iterables; + import org.apache.lucene.util.LuceneTestCase.Slow; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.index.IndexRequestBuilder; @@ -29,9 +30,15 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.settings.ImmutableSettings.Builder; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.query.*; +import org.elasticsearch.index.query.BoostableQueryBuilder; +import org.elasticsearch.index.query.FilterBuilders; +import org.elasticsearch.index.query.IdsQueryBuilder; +import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder.Operator; import org.elasticsearch.index.query.MatchQueryBuilder.Type; +import org.elasticsearch.index.query.MultiMatchQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -50,12 +57,38 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.missingFilter; import static org.elasticsearch.index.query.FilterBuilders.typeFilter; -import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; +import static org.elasticsearch.index.query.QueryBuilders.boostingQuery; +import static org.elasticsearch.index.query.QueryBuilders.commonTermsQuery; +import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; +import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; +import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; +import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; +import static org.elasticsearch.index.query.QueryBuilders.prefixQuery; +import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery; +import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; +import static org.elasticsearch.index.query.QueryBuilders.regexpQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.highlight; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHighlight; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNotHighlighted; import static org.elasticsearch.test.hamcrest.RegexMatcher.matches; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.anyOf; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.startsWith; /** * diff --git a/src/test/java/org/elasticsearch/search/scriptfilter/ScriptFilterSearchTests.java b/src/test/java/org/elasticsearch/search/scriptfilter/ScriptFilterSearchTests.java index 7e0413757ee..6b199d9970a 100644 --- a/src/test/java/org/elasticsearch/search/scriptfilter/ScriptFilterSearchTests.java +++ b/src/test/java/org/elasticsearch/search/scriptfilter/ScriptFilterSearchTests.java @@ -23,8 +23,8 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.cache.filter.AutoFilterCachingPolicy; import org.elasticsearch.index.cache.filter.FilterCacheModule; +import org.elasticsearch.index.cache.filter.FilterCacheModule.FilterCacheSettings; import org.elasticsearch.index.cache.filter.weighted.WeightedFilterCache; import org.elasticsearch.script.groovy.GroovyScriptEngineService; import org.elasticsearch.search.sort.SortOrder; @@ -35,7 +35,9 @@ import java.util.concurrent.atomic.AtomicInteger; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.scriptFilter; -import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; @@ -51,7 +53,7 @@ public class ScriptFilterSearchTests extends ElasticsearchIntegrationTest { .put(GroovyScriptEngineService.GROOVY_SCRIPT_SANDBOX_ENABLED, false) // aggressive filter caching so that we can assert on the number of iterations of the script filters .put(FilterCacheModule.FilterCacheSettings.FILTER_CACHE_TYPE, WeightedFilterCache.class) - .put(AutoFilterCachingPolicy.AGGRESSIVE_CACHING_SETTINGS) + .put(FilterCacheSettings.FILTER_CACHE_EVERYTHING, true) .build(); } diff --git a/src/test/java/org/elasticsearch/test/InternalTestCluster.java b/src/test/java/org/elasticsearch/test/InternalTestCluster.java index dea966dbf55..b685448881b 100644 --- a/src/test/java/org/elasticsearch/test/InternalTestCluster.java +++ b/src/test/java/org/elasticsearch/test/InternalTestCluster.java @@ -77,8 +77,8 @@ import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.cache.filter.AutoFilterCachingPolicy; import org.elasticsearch.index.cache.filter.FilterCacheModule; +import org.elasticsearch.index.cache.filter.FilterCacheModule.FilterCacheSettings; import org.elasticsearch.index.cache.filter.none.NoneFilterCache; import org.elasticsearch.index.cache.filter.weighted.WeightedFilterCache; import org.elasticsearch.index.shard.IndexShardModule; @@ -131,6 +131,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static junit.framework.Assert.fail; import static org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY; +import static org.apache.lucene.util.LuceneTestCase.random; import static org.apache.lucene.util.LuceneTestCase.rarely; import static org.apache.lucene.util.LuceneTestCase.usually; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; @@ -454,18 +455,7 @@ public final class InternalTestCluster extends TestCluster { } if (random.nextBoolean()) { - final int freqCacheable = 1 + random.nextInt(5); - final int freqCostly = 1 + random.nextInt(5); - final int freqOther = Math.max(freqCacheable, freqCostly) + random.nextInt(2); - int historySize = 3 + random.nextInt(100); - historySize = Math.max(historySize, freqCacheable); - historySize = Math.max(historySize, freqCostly); - historySize = Math.max(historySize, freqOther); - builder.put(AutoFilterCachingPolicy.HISTORY_SIZE, historySize); - builder.put(AutoFilterCachingPolicy.MIN_FREQUENCY_CACHEABLE, freqCacheable); - builder.put(AutoFilterCachingPolicy.MIN_FREQUENCY_COSTLY, freqCostly); - builder.put(AutoFilterCachingPolicy.MIN_FREQUENCY_OTHER, freqOther); - builder.put(AutoFilterCachingPolicy.MIN_SEGMENT_SIZE_RATIO, random.nextFloat()); + builder.put(FilterCacheSettings.FILTER_CACHE_EVERYTHING, random.nextBoolean()); } return builder.build(); diff --git a/src/test/java/org/elasticsearch/validate/SimpleValidateQueryTests.java b/src/test/java/org/elasticsearch/validate/SimpleValidateQueryTests.java index 8ef7cfc2428..fa5f2f58973 100644 --- a/src/test/java/org/elasticsearch/validate/SimpleValidateQueryTests.java +++ b/src/test/java/org/elasticsearch/validate/SimpleValidateQueryTests.java @@ -24,16 +24,11 @@ import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.cache.filter.AutoFilterCachingPolicy; -import org.elasticsearch.index.cache.filter.FilterCacheModule; -import org.elasticsearch.index.cache.filter.none.NoneFilterCache; -import org.elasticsearch.index.cache.filter.weighted.WeightedFilterCache; import org.elasticsearch.index.query.FilterBuilders; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -45,7 +40,6 @@ import org.hamcrest.Matcher; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.ISODateTimeFormat; -import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; @@ -62,27 +56,6 @@ import static org.hamcrest.Matchers.*; @ClusterScope(randomDynamicTemplates = false, scope = Scope.SUITE) public class SimpleValidateQueryTests extends ElasticsearchIntegrationTest { - static Boolean hasFilterCache; - - @BeforeClass - public static void enableFilterCache() { - assert hasFilterCache == null; - hasFilterCache = randomBoolean(); - } - - @Override - protected Settings nodeSettings(int nodeOrdinal) { - ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder().put(super.nodeSettings(nodeOrdinal)); - if (hasFilterCache) { - // cache everything - builder.put(FilterCacheModule.FilterCacheSettings.FILTER_CACHE_TYPE, WeightedFilterCache.class) - .put(AutoFilterCachingPolicy.AGGRESSIVE_CACHING_SETTINGS); - } else { - builder.put(FilterCacheModule.FilterCacheSettings.FILTER_CACHE_TYPE, NoneFilterCache.class); - } - return builder.build(); - } - @Test public void simpleValidateQuery() throws Exception { createIndex("test"); @@ -108,132 +81,6 @@ public class SimpleValidateQueryTests extends ElasticsearchIntegrationTest { assertThat(client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.queryStringQuery("foo:1 AND")).execute().actionGet().isValid(), equalTo(false)); } - private static String filter(String uncachedFilter) { - String filter = uncachedFilter; - if (hasFilterCache) { - filter = "cache(" + filter + ")"; - } - return filter; - } - - private String filtered(String query) { - return "filtered(" + query + ")"; - } - - @Test - public void explainValidateQuery() throws Exception { - createIndex("test"); - ensureGreen(); - client().admin().indices().preparePutMapping("test").setType("type1") - .setSource(XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("foo").field("type", "string").endObject() - .startObject("bar").field("type", "integer").endObject() - .startObject("baz").field("type", "string").field("analyzer", "snowball").endObject() - .startObject("pin").startObject("properties").startObject("location").field("type", "geo_point").endObject().endObject().endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); - client().admin().indices().preparePutMapping("test").setType("child-type") - .setSource(XContentFactory.jsonBuilder().startObject().startObject("child-type") - .startObject("_parent").field("type", "type1").endObject() - .startObject("properties") - .startObject("foo").field("type", "string").endObject() - .endObject() - .endObject().endObject()) - .execute().actionGet(); - - refresh(); - - ValidateQueryResponse response; - response = client().admin().indices().prepareValidateQuery("test") - .setSource("foo".getBytes(Charsets.UTF_8)) - .setExplain(true) - .execute().actionGet(); - assertThat(response.isValid(), equalTo(false)); - assertThat(response.getQueryExplanation().size(), equalTo(1)); - assertThat(response.getQueryExplanation().get(0).getError(), containsString("Failed to parse")); - assertThat(response.getQueryExplanation().get(0).getExplanation(), nullValue()); - - final String typeFilter = "->" + filter("_type:type1"); - assertExplanation(QueryBuilders.queryStringQuery("_id:1"), equalTo(filtered("ConstantScore(_uid:type1#1)") + typeFilter)); - - assertExplanation(QueryBuilders.idsQuery("type1").addIds("1").addIds("2"), - equalTo(filtered("ConstantScore(_uid:type1#1 _uid:type1#2)") + typeFilter)); - - assertExplanation(QueryBuilders.queryStringQuery("foo"), equalTo(filtered("_all:foo") + typeFilter)); - - assertExplanation(QueryBuilders.filteredQuery( - QueryBuilders.termQuery("foo", "1"), - FilterBuilders.orFilter( - FilterBuilders.termFilter("bar", "2"), - FilterBuilders.termFilter("baz", "3") - ) - ), equalTo(filtered("filtered(foo:1)->" + filter(filter("bar:[2 TO 2]") + " " + filter("baz:3"))) + typeFilter)); - - assertExplanation(QueryBuilders.filteredQuery( - QueryBuilders.termQuery("foo", "1"), - FilterBuilders.orFilter( - FilterBuilders.termFilter("bar", "2") - ) - ), equalTo(filtered("filtered(foo:1)->" + filter(filter("bar:[2 TO 2]"))) + typeFilter)); - - assertExplanation(QueryBuilders.filteredQuery( - QueryBuilders.matchAllQuery(), - FilterBuilders.geoPolygonFilter("pin.location") - .addPoint(40, -70) - .addPoint(30, -80) - .addPoint(20, -90) - .addPoint(40, -70) // closing polygon - ), equalTo(filtered("ConstantScore(" + filter("GeoPolygonFilter(pin.location, [[40.0, -70.0], [30.0, -80.0], [20.0, -90.0], [40.0, -70.0]]))")) + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoBoundingBoxFilter("pin.location") - .topLeft(40, -80) - .bottomRight(20, -70) - ), equalTo(filtered("ConstantScore(" + filter("GeoBoundingBoxFilter(pin.location, [40.0, -80.0], [20.0, -70.0]))")) + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter("pin.location") - .lat(10).lon(20).distance(15, DistanceUnit.DEFAULT).geoDistance(GeoDistance.PLANE) - ), equalTo(filtered("ConstantScore(" + filter("GeoDistanceFilter(pin.location, PLANE, 15.0, 10.0, 20.0))")) + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter("pin.location") - .lat(10).lon(20).distance(15, DistanceUnit.DEFAULT).geoDistance(GeoDistance.PLANE) - ), equalTo(filtered("ConstantScore(" + filter("GeoDistanceFilter(pin.location, PLANE, 15.0, 10.0, 20.0))")) + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceRangeFilter("pin.location") - .lat(10).lon(20).from("15m").to("25m").geoDistance(GeoDistance.PLANE) - ), equalTo(filtered("ConstantScore(" + filter("GeoDistanceRangeFilter(pin.location, PLANE, [15.0 - 25.0], 10.0, 20.0))")) + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceRangeFilter("pin.location") - .lat(10).lon(20).from("15miles").to("25miles").geoDistance(GeoDistance.PLANE) - ), equalTo(filtered("ConstantScore(" + filter("GeoDistanceRangeFilter(pin.location, PLANE, [" + DistanceUnit.DEFAULT.convert(15.0, DistanceUnit.MILES) + " - " + DistanceUnit.DEFAULT.convert(25.0, DistanceUnit.MILES) + "], 10.0, 20.0))")) + typeFilter)); - - assertExplanation(QueryBuilders.filteredQuery( - QueryBuilders.termQuery("foo", "1"), - FilterBuilders.andFilter( - FilterBuilders.termFilter("bar", "2"), - FilterBuilders.termFilter("baz", "3") - ) - ), equalTo(filtered("filtered(foo:1)->" + filter("+" + filter("bar:[2 TO 2]") + " +" + filter("baz:3"))) + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.termsFilter("foo", "1", "2", "3")), - equalTo(filtered("ConstantScore(" + filter("foo:1 foo:2 foo:3") + ")") + typeFilter)); - - assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.notFilter(FilterBuilders.termFilter("foo", "bar"))), - equalTo(filtered("ConstantScore(" + filter("NotFilter(" + filter("foo:bar") + ")") + ")") + typeFilter)); - - assertExplanation(QueryBuilders.filteredQuery( - QueryBuilders.termQuery("foo", "1"), - FilterBuilders.hasChildFilter( - "child-type", - QueryBuilders.matchQuery("foo", "1") - ) - ), equalTo(filtered("filtered(foo:1)->CustomQueryWrappingFilter(child_filter[child-type/type1](filtered(foo:1)->" + filter("_type:child-type") + "))") + typeFilter)); - - assertExplanation(QueryBuilders.filteredQuery( - QueryBuilders.termQuery("foo", "1"), - FilterBuilders.scriptFilter("true") - ), equalTo(filtered("filtered(foo:1)->" + filter("ScriptFilter(true)")) + typeFilter)); - } - @Test public void explainValidateQueryTwoNodes() throws IOException { createIndex("test"); @@ -313,7 +160,7 @@ public class SimpleValidateQueryTests extends ElasticsearchIntegrationTest { assertThat(validateQueryResponse.isValid(), equalTo(true)); assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1)); assertThat(validateQueryResponse.getQueryExplanation().get(0).getIndex(), equalTo("test")); - assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), equalTo("ConstantScore(*:*)")); + assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), equalTo("*:*")); } @Test