From 4c4bbb3e45931c67d4fbdc71cebb7ecd0645eedd Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 24 Mar 2016 18:27:18 +0100 Subject: [PATCH] Replace FieldStatsProvider with a method on MappedFieldType. #17334 FieldStatsProvider had to perform instanceof calls to properly handle dates or ip addresses. By moving the logic to MappedFieldType, each field type can check whether all values are within bounds its way. Note that this commit only keeps rewriting support for dates, which are the only field for which the rewriting mechanism is likely to help (because of time-based indices). --- .../query/TransportValidateQueryAction.java | 4 +- .../fieldstats/FieldStatsShardResponse.java | 8 +- .../TransportFieldStatsTransportAction.java | 23 +- .../org/elasticsearch/index/IndexService.java | 14 +- .../index/fieldstats/FieldStatsProvider.java | 181 ------- .../index/mapper/MappedFieldType.java | 38 +- .../index/mapper/core/ByteFieldMapper.java | 8 +- .../index/mapper/core/DateFieldMapper.java | 57 ++- .../index/mapper/core/DoubleFieldMapper.java | 8 +- .../index/mapper/core/FloatFieldMapper.java | 8 +- .../index/mapper/core/IntegerFieldMapper.java | 8 +- .../index/mapper/core/LongFieldMapper.java | 8 +- .../index/mapper/core/ShortFieldMapper.java | 8 +- .../index/mapper/ip/IpFieldMapper.java | 11 +- .../index/query/QueryRewriteContext.java | 31 +- .../index/query/QueryShardContext.java | 17 +- .../index/query/RangeQueryBuilder.java | 64 ++- .../elasticsearch/search/SearchService.java | 2 - .../search/internal/DefaultSearchContext.java | 2 +- .../fieldstats/FieldStatsProviderTests.java | 446 ------------------ .../index/mapper/core/DateFieldTypeTests.java | 70 +++ .../mapper/core/DoubleFieldTypeTests.java | 11 + .../mapper/core/FloatFieldTypeTests.java | 11 + .../mapper/core/IntegerFieldTypeTests.java | 11 + .../mapper/core/KeywordFieldTypeTests.java | 15 + .../index/mapper/core/LongFieldTypeTests.java | 11 + .../percolator/PercolatorQueryCacheTests.java | 2 +- .../index/query/AbstractQueryTestCase.java | 3 +- .../index/query/QueryShardContextTests.java | 2 +- .../index/query/RangeQueryBuilderTests.java | 432 +---------------- .../index/query/RangeQueryRewriteTests.java | 81 ++++ .../indices/IndicesRequestCacheIT.java | 50 +- .../highlight/HighlightBuilderTests.java | 2 +- .../rescore/QueryRescoreBuilderTests.java | 2 +- .../search/sort/AbstractSortTestCase.java | 2 +- .../messy/tests/TemplateQueryParserTests.java | 2 +- 36 files changed, 492 insertions(+), 1161 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/index/fieldstats/FieldStatsProvider.java delete mode 100644 core/src/test/java/org/elasticsearch/index/fieldstats/FieldStatsProviderTests.java create mode 100644 core/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java b/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java index 320f06966059c..7c8d52b6a60f3 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java @@ -168,8 +168,6 @@ protected ValidateQueryResponse newResponse(ValidateQueryRequest request, Atomic protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) { IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex()); IndexShard indexShard = indexService.getShard(request.shardId().id()); - final QueryShardContext queryShardContext = indexService.newQueryShardContext(); - queryShardContext.setTypes(request.types()); boolean valid; String explanation = null; @@ -182,7 +180,7 @@ protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest re parseFieldMatcher, SearchService.NO_TIMEOUT, fetchPhase); SearchContext.setCurrent(searchContext); try { - searchContext.parsedQuery(queryShardContext.toQuery(request.query())); + searchContext.parsedQuery(searchContext.getQueryShardContext().toQuery(request.query())); searchContext.preProcess(); valid = true; diff --git a/core/src/main/java/org/elasticsearch/action/fieldstats/FieldStatsShardResponse.java b/core/src/main/java/org/elasticsearch/action/fieldstats/FieldStatsShardResponse.java index c1094ce4d3e2c..d6e6b5fc0a332 100644 --- a/core/src/main/java/org/elasticsearch/action/fieldstats/FieldStatsShardResponse.java +++ b/core/src/main/java/org/elasticsearch/action/fieldstats/FieldStatsShardResponse.java @@ -32,17 +32,17 @@ */ public class FieldStatsShardResponse extends BroadcastShardResponse { - private Map fieldStats; + private Map> fieldStats; public FieldStatsShardResponse() { } - public FieldStatsShardResponse(ShardId shardId, Map fieldStats) { + public FieldStatsShardResponse(ShardId shardId, Map> fieldStats) { super(shardId); this.fieldStats = fieldStats; } - public Map getFieldStats() { + public Map> getFieldStats() { return fieldStats; } @@ -63,7 +63,7 @@ public void readFrom(StreamInput in) throws IOException { public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeVInt(fieldStats.size()); - for (Map.Entry entry : fieldStats.entrySet()) { + for (Map.Entry> entry : fieldStats.entrySet()) { out.writeString(entry.getKey()); entry.getValue().writeTo(out); } diff --git a/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java b/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java index de56a0f5c2ec4..5981128126e60 100644 --- a/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java +++ b/core/src/main/java/org/elasticsearch/action/fieldstats/TransportFieldStatsTransportAction.java @@ -19,9 +19,6 @@ package org.elasticsearch.action.fieldstats; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.MultiFields; -import org.apache.lucene.index.Terms; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.support.ActionFilters; @@ -102,9 +99,9 @@ protected FieldStatsResponse newResponse(FieldStatsRequest request, AtomicRefere indicesMergedFieldStats.put(indexName, indexMergedFieldStats = new HashMap<>()); } - Map fieldStats = shardResponse.getFieldStats(); - for (Map.Entry entry : fieldStats.entrySet()) { - FieldStats existing = indexMergedFieldStats.get(entry.getKey()); + Map> fieldStats = shardResponse.getFieldStats(); + for (Map.Entry> entry : fieldStats.entrySet()) { + FieldStats existing = indexMergedFieldStats.get(entry.getKey()); if (existing != null) { if (existing.getType() != entry.getValue().getType()) { throw new IllegalStateException( @@ -156,22 +153,20 @@ protected FieldStatsShardResponse newShardResponse() { @Override protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request) { ShardId shardId = request.shardId(); - Map fieldStats = new HashMap<>(); + Map> fieldStats = new HashMap<>(); IndexService indexServices = indicesService.indexServiceSafe(shardId.getIndex()); MapperService mapperService = indexServices.mapperService(); IndexShard shard = indexServices.getShard(shardId.id()); try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) { for (String field : request.getFields()) { MappedFieldType fieldType = mapperService.fullName(field); - if (fieldType != null) { - IndexReader reader = searcher.reader(); - Terms terms = MultiFields.getTerms(reader, field); - if (terms != null) { - fieldStats.put(field, fieldType.stats(terms, reader.maxDoc())); - } - } else { + if (fieldType == null) { throw new IllegalArgumentException("field [" + field + "] doesn't exist"); } + FieldStats stats = fieldType.stats(searcher.reader()); + if (stats != null) { + fieldStats.put(field, stats); + } } } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); diff --git a/core/src/main/java/org/elasticsearch/index/IndexService.java b/core/src/main/java/org/elasticsearch/index/IndexService.java index 1f1037ea18ed3..5a38a194b0acc 100644 --- a/core/src/main/java/org/elasticsearch/index/IndexService.java +++ b/core/src/main/java/org/elasticsearch/index/IndexService.java @@ -19,6 +19,7 @@ package org.elasticsearch.index; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Query; @@ -438,14 +439,23 @@ public IndexSettings getIndexSettings() { * Creates a new QueryShardContext. The context has not types set yet, if types are required set them via * {@link QueryShardContext#setTypes(String...)} */ - public QueryShardContext newQueryShardContext() { + public QueryShardContext newQueryShardContext(IndexReader indexReader) { return new QueryShardContext( indexSettings, indexCache.bitsetFilterCache(), indexFieldData, mapperService(), similarityService(), nodeServicesProvider.getScriptService(), nodeServicesProvider.getIndicesQueriesRegistry(), - indexCache.getPercolatorQueryCache() + indexCache.getPercolatorQueryCache(), indexReader ); } + /** + * Creates a new QueryShardContext. The context has not types set yet, if types are required set them via + * {@link QueryShardContext#setTypes(String...)}. This context may be used for query parsing but cannot be + * used for rewriting since it does not know about the current {@link IndexReader}. + */ + public QueryShardContext newQueryShardContext() { + return newQueryShardContext(null); + } + public ThreadPool getThreadPool() { return threadPool; } diff --git a/core/src/main/java/org/elasticsearch/index/fieldstats/FieldStatsProvider.java b/core/src/main/java/org/elasticsearch/index/fieldstats/FieldStatsProvider.java deleted file mode 100644 index 066d1207e3f04..0000000000000 --- a/core/src/main/java/org/elasticsearch/index/fieldstats/FieldStatsProvider.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.index.fieldstats; - -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.MultiFields; -import org.apache.lucene.index.Terms; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.action.fieldstats.FieldStats; -import org.elasticsearch.action.fieldstats.IndexConstraint; -import org.elasticsearch.action.fieldstats.IndexConstraint.Comparison; -import org.elasticsearch.action.fieldstats.IndexConstraint.Property; -import org.elasticsearch.common.joda.DateMathParser; -import org.elasticsearch.index.engine.Engine.Searcher; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.core.DateFieldMapper.DateFieldType; -import org.elasticsearch.index.mapper.ip.IpFieldMapper.IpFieldType; -import org.joda.time.DateTimeZone; - -import java.io.IOException; - -/** - * Provides a service for gettings the {@link FieldStats} for a given field from - * the index. - */ -public class FieldStatsProvider { - - private final Searcher searcher; - private final MapperService mapperService; - - /** - * @param searcher - * the {@link Searcher}to use when retrieving the - * {@link FieldStats} - * @param mapperService - * the {@link MapperService} - */ - public FieldStatsProvider(Searcher searcher, MapperService mapperService) { - this.searcher = searcher; - this.mapperService = mapperService; - } - - /** - * @param field - * the name of the field to return {@link FieldStats} for. - * @return a {@link FieldStats} object for the given field - * @throws IOException - * if the field statistics cannot be read - */ - public > FieldStats get(String field) throws IOException { - MappedFieldType mappedFieldType = mapperService.fullName(field); - if (mappedFieldType != null) { - IndexReader reader = searcher.reader(); - Terms terms = MultiFields.getTerms(reader, field); - if (terms != null) { - return mappedFieldType.stats(terms, reader.maxDoc()); - } - } - return null; - } - - /** - * @param fieldName - * the fieldName to check - * @param from - * the minimum value for the query - * @param to - * the maximum value for the query - * @param includeLower - * whether the from value is inclusive - * @param includeUpper - * whether the to value is inclusive - * @param timeZone - * the timeZone to use for date fields - * @param dateMathParser - * the {@link DateMathParser} to use for date fields - * @return A {@link Relation} indicating the overlap of the range of terms - * for the field with the query range. This method will return: - *
    - *
  • {@link Relation#WITHIN} if the range of terms for the field - * in the shard is completely within the query range
  • - *
  • {@link Relation#DISJOINT} if the range of terms for the field - * in the shard is completely outside the query range
  • - *
  • {@link Relation#INTERSECTS} if the range of terms for the - * field in the shard intersects with the query range
  • - *
- * @throws IOException - * if the index cannot be read - */ - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - MappedFieldType mappedFieldType = mapperService.fullName(fieldName); - FieldStats fieldStats = get(fieldName); - if (fieldStats == null) { - // No fieldStats for the field so the field doesn't exist on - // this shard, so relation is DISJOINT - return Relation.DISJOINT; - } else { - // Convert the from and to values to Strings so they can be used - // in the IndexConstraints. Since DateTime is represented as a - // Long field in Lucene we need to use the millisecond value of - // the DateTime in that case - String fromString = null; - if (from != null) { - if (mappedFieldType instanceof DateFieldType) { - long millis = ((DateFieldType) mappedFieldType).parseToMilliseconds(from, !includeLower, timeZone, dateMathParser); - fromString = fieldStats.stringValueOf(millis, null); - } else if (mappedFieldType instanceof IpFieldType) { - if (from instanceof BytesRef) { - from = ((BytesRef) from).utf8ToString(); - } - long ipAsLong = ((IpFieldType) mappedFieldType).value(from); - fromString = fieldStats.stringValueOf(ipAsLong, null); - } else { - fromString = fieldStats.stringValueOf(from, null); - } - } - String toString = null; - if (to != null) { - if (mappedFieldType instanceof DateFieldType) { - long millis = ((DateFieldType) mappedFieldType).parseToMilliseconds(to, includeUpper, timeZone, dateMathParser); - toString = fieldStats.stringValueOf(millis, null); - } else if (mappedFieldType instanceof IpFieldType) { - if (to instanceof BytesRef) { - to = ((BytesRef) to).utf8ToString(); - } - long ipAsLong = ((IpFieldType) mappedFieldType).value(to); - toString = fieldStats.stringValueOf(ipAsLong, null); - } else { - toString = fieldStats.stringValueOf(to, null); - } - } - if ((from == null || fieldStats - .match(new IndexConstraint(fieldName, Property.MIN, includeLower ? Comparison.GTE : Comparison.GT, fromString))) - && (to == null || fieldStats.match( - new IndexConstraint(fieldName, Property.MAX, includeUpper ? Comparison.LTE : Comparison.LT, toString)))) { - // If the min and max terms for the field are both within - // the query range then all documents will match so relation is - // WITHIN - return Relation.WITHIN; - } else if ((to != null && fieldStats - .match(new IndexConstraint(fieldName, Property.MIN, includeUpper ? Comparison.GT : Comparison.GTE, toString))) - || (from != null && fieldStats.match( - new IndexConstraint(fieldName, Property.MAX, includeLower ? Comparison.LT : Comparison.LTE, fromString)))) { - // If the min and max terms are both outside the query range - // then no document will match so relation is DISJOINT (N.B. - // since from <= to we only need - // to check one bould for each side of the query range) - return Relation.DISJOINT; - } - } - // Range of terms doesn't match any of the constraints so must INTERSECT - return Relation.INTERSECTS; - } - - /** - * An enum used to describe the relation between the range of terms in a - * shard when compared with a query range - */ - public static enum Relation { - WITHIN, INTERSECTS, DISJOINT; - } -} diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index 9eafa8ccd71cc..42f77bb6c23eb 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -21,6 +21,8 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.Term; import org.apache.lucene.index.Terms; import org.apache.lucene.queries.TermsQuery; @@ -37,6 +39,7 @@ import org.elasticsearch.Version; import org.elasticsearch.action.fieldstats.FieldStats; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -44,6 +47,7 @@ import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; import org.elasticsearch.index.similarity.SimilarityProvider; +import org.joda.time.DateTimeZone; import java.io.IOException; import java.util.List; @@ -394,14 +398,43 @@ public Query nullValueQuery() { } /** - * @return a {@link FieldStats} instance that maps to the type of this field based on the provided {@link Terms} instance. + * @return a {@link FieldStats} instance that maps to the type of this + * field or {@code null} if the provided index has no stats about the + * current field */ - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } return new FieldStats.Text( maxDoc, terms.getDocCount(), terms.getSumDocFreq(), terms.getSumTotalTermFreq(), terms.getMin(), terms.getMax() ); } + /** + * An enum used to describe the relation between the range of terms in a + * shard when compared with a query range + */ + public static enum Relation { + WITHIN, + INTERSECTS, + DISJOINT; + } + + /** Return whether all values of the given {@link IndexReader} are within the range, + * outside the range or cross the range. The default implementation returns + * {@link Relation#INTERSECTS}, which is always fine to return when there is + * no way to check whether values are actually within bounds. */ + public Relation isFieldWithinQuery( + IndexReader reader, + Object from, Object to, + boolean includeLower, boolean includeUpper, + DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { + return Relation.INTERSECTS; + } + /** A term query to use when parsing a query string. Can return null. */ @Nullable public Query queryStringTermQuery(Term term) { @@ -424,4 +457,5 @@ public void setEagerGlobalOrdinals(boolean eagerGlobalOrdinals) { checkIfFrozen(); this.eagerGlobalOrdinals = eagerGlobalOrdinals; } + } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java index c42f0b7cd42f4..758095b714957 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java @@ -22,6 +22,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -181,7 +182,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } long minValue = LegacyNumericUtils.getMinInt(terms); long maxValue = LegacyNumericUtils.getMaxInt(terms); return new FieldStats.Long( diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index 5d66fb62c766c..17c8671c6625a 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -22,6 +22,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -398,7 +399,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } long minValue = LegacyNumericUtils.getMinLong(terms); long maxValue = LegacyNumericUtils.getMaxLong(terms); return new FieldStats.Date( @@ -417,6 +423,55 @@ private Query innerRangeQuery(Object lowerTerm, Object upperTerm, boolean includ includeLower, includeUpper); } + @Override + public Relation isFieldWithinQuery(IndexReader reader, + Object from, Object to, + boolean includeLower, boolean includeUpper, + DateTimeZone timeZone, DateMathParser dateParser) throws IOException { + if (dateParser == null) { + dateParser = this.dateMathParser; + } + + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + // no terms, so nothing matches + return Relation.DISJOINT; + } + + long minValue = LegacyNumericUtils.getMinLong(terms); + long maxValue = LegacyNumericUtils.getMaxLong(terms); + + long fromInclusive = Long.MIN_VALUE; + if (from != null) { + fromInclusive = parseToMilliseconds(from, !includeLower, timeZone, dateParser); + if (includeLower == false) { + if (fromInclusive == Long.MAX_VALUE) { + return Relation.DISJOINT; + } + ++fromInclusive; + } + } + + long toInclusive = Long.MAX_VALUE; + if (to != null) { + toInclusive = parseToMilliseconds(to, includeUpper, timeZone, dateParser); + if (includeUpper == false) { + if (toInclusive == Long.MIN_VALUE) { + return Relation.DISJOINT; + } + --toInclusive; + } + } + + if (minValue >= fromInclusive && maxValue <= toInclusive) { + return Relation.WITHIN; + } else if (maxValue < fromInclusive || minValue > toInclusive) { + return Relation.DISJOINT; + } else { + return Relation.INTERSECTS; + } + } + public long parseToMilliseconds(Object value, boolean inclusive, @Nullable DateTimeZone zone, @Nullable DateMathParser forcedDateParser) { if (value instanceof Long) { return ((Long) value).longValue(); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java index 69d56439b3337..bfebadb70cbfb 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -184,7 +185,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } double minValue = NumericUtils.sortableLongToDouble(LegacyNumericUtils.getMinLong(terms)); double maxValue = NumericUtils.sortableLongToDouble(LegacyNumericUtils.getMaxLong(terms)); return new FieldStats.Double( diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java index 48b9ca9587538..c63252ec9c7f3 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -185,7 +186,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } float minValue = NumericUtils.sortableIntToFloat(LegacyNumericUtils.getMinInt(terms)); float maxValue = NumericUtils.sortableIntToFloat(LegacyNumericUtils.getMaxInt(terms)); return new FieldStats.Float( diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java index 860377d087915..d467b9593003e 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -190,7 +191,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } long minValue = LegacyNumericUtils.getMinInt(terms); long maxValue = LegacyNumericUtils.getMaxInt(terms); return new FieldStats.Long( diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java index b261df7a2218e..a3d9e0eb5d355 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -188,7 +189,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } long minValue = LegacyNumericUtils.getMinLong(terms); long maxValue = LegacyNumericUtils.getMaxLong(terms); return new FieldStats.Long( diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java index eb00e0744c81e..6ba40ea98e169 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -186,7 +187,12 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } long minValue = LegacyNumericUtils.getMinInt(terms); long maxValue = LegacyNumericUtils.getMaxInt(terms); return new FieldStats.Long( diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java index b754c163669ea..b2b82529551ef 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java @@ -22,6 +22,7 @@ import org.apache.lucene.analysis.LegacyNumericTokenStream; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Terms; import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; @@ -267,10 +268,16 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public FieldStats stats(Terms terms, int maxDoc) throws IOException { + public FieldStats stats(IndexReader reader) throws IOException { + int maxDoc = reader.maxDoc(); + Terms terms = org.apache.lucene.index.MultiFields.getTerms(reader, name()); + if (terms == null) { + return null; + } long minValue = LegacyNumericUtils.getMinLong(terms); long maxValue = LegacyNumericUtils.getMaxLong(terms); - return new FieldStats.Ip(maxDoc, terms.getDocCount(), terms.getSumDocFreq(), terms.getSumTotalTermFreq(), minValue, maxValue); + return new FieldStats.Ip(maxDoc, terms.getDocCount(), terms.getSumDocFreq(), + terms.getSumTotalTermFreq(), minValue, maxValue); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java b/core/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java index 11164659b3f7b..70de640539f4a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java @@ -18,9 +18,10 @@ */ package org.elasticsearch.index.query; +import org.apache.lucene.index.IndexReader; import org.elasticsearch.client.Client; import org.elasticsearch.index.IndexSettings; -import org.elasticsearch.index.fieldstats.FieldStatsProvider; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.script.ScriptService; @@ -28,25 +29,21 @@ * Context object used to rewrite {@link QueryBuilder} instances into simplified version. */ public class QueryRewriteContext { + protected final MapperService mapperService; protected final ScriptService scriptService; protected final IndexSettings indexSettings; protected final IndicesQueriesRegistry indicesQueriesRegistry; protected final QueryParseContext parseContext; - protected FieldStatsProvider fieldStatsProvider; + protected final IndexReader reader; - public QueryRewriteContext(IndexSettings indexSettings, ScriptService scriptService, IndicesQueriesRegistry indicesQueriesRegistry) { + public QueryRewriteContext(IndexSettings indexSettings, MapperService mapperService, ScriptService scriptService, + IndicesQueriesRegistry indicesQueriesRegistry, IndexReader reader) { + this.mapperService = mapperService; this.scriptService = scriptService; this.indexSettings = indexSettings; this.indicesQueriesRegistry = indicesQueriesRegistry; this.parseContext = new QueryParseContext(indicesQueriesRegistry); - } - - public void setFieldStatsProvider(FieldStatsProvider fieldStatsProvider) { - this.fieldStatsProvider = fieldStatsProvider; - } - - public FieldStatsProvider getFieldStatsProvider() { - return fieldStatsProvider; + this.reader = reader; } /** @@ -71,6 +68,18 @@ public final ScriptService getScriptService() { return scriptService; } + /** + * Return the MapperService. + */ + public final MapperService getMapperService() { + return mapperService; + } + + /** Return the current {@link IndexReader}, or {@code null} if we are on the coordinating node. */ + public IndexReader getIndexReader() { + return reader; + } + /** * Returns a new {@link QueryParseContext} to parse template or wrapped queries. */ diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 77aa1dc0c3666..b516e44ea280b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.MapperQueryParser; import org.apache.lucene.queryparser.classic.QueryParserSettings; import org.apache.lucene.search.Query; @@ -95,8 +96,8 @@ public String[] getTypes() { boolean isFilter; // pkg private for testing public QueryShardContext(IndexSettings indexSettings, BitsetFilterCache bitsetFilterCache, IndexFieldDataService indexFieldDataService, MapperService mapperService, SimilarityService similarityService, ScriptService scriptService, - final IndicesQueriesRegistry indicesQueriesRegistry, PercolatorQueryCache percolatorQueryCache) { - super(indexSettings, scriptService, indicesQueriesRegistry); + final IndicesQueriesRegistry indicesQueriesRegistry, PercolatorQueryCache percolatorQueryCache, IndexReader reader) { + super(indexSettings, mapperService, scriptService, indicesQueriesRegistry, reader); this.indexSettings = indexSettings; this.similarityService = similarityService; this.mapperService = mapperService; @@ -109,16 +110,10 @@ public QueryShardContext(IndexSettings indexSettings, BitsetFilterCache bitsetFi } public QueryShardContext(QueryShardContext source) { - this(source.indexSettings, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.indicesQueriesRegistry, source.percolatorQueryCache); + this(source.indexSettings, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.indicesQueriesRegistry, source.percolatorQueryCache, source.reader); this.types = source.getTypes(); } - - @Override - public QueryShardContext clone() { - return new QueryShardContext(indexSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, percolatorQueryCache); - } - public void parseFieldMatcher(ParseFieldMatcher parseFieldMatcher) { this.parseContext.parseFieldMatcher(parseFieldMatcher); } @@ -145,10 +140,6 @@ public AnalysisService getAnalysisService() { return mapperService.analysisService(); } - public MapperService getMapperService() { - return mapperService; - } - public PercolatorQueryCache getPercolatorQueryCache() { return percolatorQueryCache; } diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index b1132c42ea9f9..28f13dc8cf100 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; @@ -30,8 +31,8 @@ import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.index.fieldstats.FieldStatsProvider; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.joda.time.DateTimeZone; @@ -254,34 +255,47 @@ public String getWriteableName() { return NAME; } + // Overridable for testing only + protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException { + IndexReader reader = queryRewriteContext.getIndexReader(); + // If the reader is null we are not on the shard and cannot + // rewrite so just pretend there is an intersection so that the rewrite is a noop + if (reader == null) { + return MappedFieldType.Relation.INTERSECTS; + } + final MapperService mapperService = queryRewriteContext.getMapperService(); + final MappedFieldType fieldType = mapperService.fullName(fieldName); + if (fieldType == null) { + // no field means we have no values + return MappedFieldType.Relation.DISJOINT; + } else { + DateMathParser dateMathParser = format == null ? null : new DateMathParser(format); + return fieldType.isFieldWithinQuery(queryRewriteContext.getIndexReader(), from, to, includeLower, includeUpper, timeZone, dateMathParser); + } + } + @Override protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { - FieldStatsProvider fieldStatsProvider = queryRewriteContext.getFieldStatsProvider(); - // If the fieldStatsProvider is null we are not on the shard and cannot - // rewrite so just return without rewriting - if (fieldStatsProvider != null) { - DateMathParser dateMathParser = format == null ? null : new DateMathParser(format); - FieldStatsProvider.Relation relation = fieldStatsProvider.isFieldWithinQuery(fieldName, from, to, includeLower, includeUpper, - timeZone, dateMathParser); - switch (relation) { - case DISJOINT: - return new MatchNoneQueryBuilder(); - case WITHIN: - if (from != null || to != null) { - RangeQueryBuilder newRangeQuery = new RangeQueryBuilder(fieldName); - newRangeQuery.from(null); - newRangeQuery.to(null); - newRangeQuery.format = format; - newRangeQuery.timeZone = timeZone; - return newRangeQuery; - } else { - return this; - } - case INTERSECTS: - break; + final MappedFieldType.Relation relation = getRelation(queryRewriteContext); + switch (relation) { + case DISJOINT: + return new MatchNoneQueryBuilder(); + case WITHIN: + if (from != null || to != null) { + RangeQueryBuilder newRangeQuery = new RangeQueryBuilder(fieldName); + newRangeQuery.from(null); + newRangeQuery.to(null); + newRangeQuery.format = format; + newRangeQuery.timeZone = timeZone; + return newRangeQuery; + } else { + return this; } + case INTERSECTS: + return this; + default: + throw new AssertionError(); } - return this; } @Override diff --git a/core/src/main/java/org/elasticsearch/search/SearchService.java b/core/src/main/java/org/elasticsearch/search/SearchService.java index 4aaa96378a4ad..995bf19e8aeec 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchService.java +++ b/core/src/main/java/org/elasticsearch/search/SearchService.java @@ -48,7 +48,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine; -import org.elasticsearch.index.fieldstats.FieldStatsProvider; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.support.InnerHitBuilder; @@ -552,7 +551,6 @@ final SearchContext createContext(ShardSearchRequest request, @Nullable Engine.S indexService, indexShard, scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher, defaultSearchTimeout, fetchPhase); - context.getQueryShardContext().setFieldStatsProvider(new FieldStatsProvider(engineSearcher, indexService.mapperService())); SearchContext.setCurrent(context); request.rewrite(context.getQueryShardContext()); // reset that we have used nowInMillis from the context since it may diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index f3fe48f6682cd..cdaf080de9ac2 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -176,7 +176,7 @@ public DefaultSearchContext(long id, ShardSearchRequest request, SearchShardTarg this.searcher = new ContextIndexSearcher(engineSearcher, indexService.cache().query(), indexShard.getQueryCachingPolicy()); this.timeEstimateCounter = timeEstimateCounter; this.timeoutInMillis = timeout.millis(); - queryShardContext = indexService.newQueryShardContext(); + queryShardContext = indexService.newQueryShardContext(searcher.getIndexReader()); queryShardContext.setTypes(request.types()); } diff --git a/core/src/test/java/org/elasticsearch/index/fieldstats/FieldStatsProviderTests.java b/core/src/test/java/org/elasticsearch/index/fieldstats/FieldStatsProviderTests.java deleted file mode 100644 index 9cad8d3fc8dd3..0000000000000 --- a/core/src/test/java/org/elasticsearch/index/fieldstats/FieldStatsProviderTests.java +++ /dev/null @@ -1,446 +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.fieldstats; - -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.store.BaseDirectoryWrapper; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.env.Environment; -import org.elasticsearch.index.IndexSettings; -import org.elasticsearch.index.analysis.AnalysisRegistry; -import org.elasticsearch.index.analysis.AnalysisService; -import org.elasticsearch.index.engine.Engine; -import org.elasticsearch.index.engine.Engine.Searcher; -import org.elasticsearch.index.fieldstats.FieldStatsProvider.Relation; -import org.elasticsearch.index.mapper.DocumentMapper; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.MapperService.MergeReason; -import org.elasticsearch.index.mapper.ParsedDocument; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.index.similarity.SimilarityService; -import org.elasticsearch.indices.IndicesModule; -import org.elasticsearch.indices.mapper.MapperRegistry; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.IndexSettingsModule; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.chrono.ISOChronology; -import org.junit.After; -import org.junit.Before; - -import java.io.IOException; -import java.util.Collections; - -import static org.elasticsearch.common.settings.Settings.settingsBuilder; -import static org.hamcrest.Matchers.equalTo; - -public class FieldStatsProviderTests extends ESTestCase { - - private DirectoryReader directoryReader; - private Searcher searcher; - private FieldStatsProvider fieldStatsProvider; - private BaseDirectoryWrapper dir; - private AnalysisRegistry analysisRegistry; - - @Before - public void setup() throws IOException { - Settings nodeSettings = settingsBuilder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build(); - IndexSettings settings = IndexSettingsModule.newIndexSettings("_na", nodeSettings); - SimilarityService similarityService = new SimilarityService(settings, Collections.emptyMap()); - analysisRegistry = new AnalysisRegistry(null, new Environment(nodeSettings)); - AnalysisService analysisService = analysisRegistry.build(settings); - IndicesModule indicesModule = new IndicesModule(); - MapperRegistry mapperRegistry = indicesModule.getMapperRegistry(); - MapperService service = new MapperService(settings, analysisService, similarityService, mapperRegistry, () -> null); - putMapping(service); - dir = newDirectory(); - IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); - indexDocument(service, w, "1", 50L, 50.2f, 50.2, "cherry", new DateTime(2014, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()), - "10.10.0.10"); - indexDocument(service, w, "2", 60L, 60.1f, 60.1, "damson", new DateTime(2014, 2, 1, 0, 0, 0, ISOChronology.getInstanceUTC()), - "10.10.0.20"); - indexDocument(service, w, "3", 70L, 70.6f, 70.6, "grape", new DateTime(2014, 3, 1, 0, 0, 0, ISOChronology.getInstanceUTC()), - "10.10.0.30"); - indexDocument(service, w, "4", 80L, 80.2f, 80.2, "kiwi", new DateTime(2014, 4, 1, 0, 0, 0, ISOChronology.getInstanceUTC()), - "10.10.0.40"); - indexDocument(service, w, "5", 90L, 90.4f, 90.4, "lemon", new DateTime(2014, 5, 1, 0, 0, 0, ISOChronology.getInstanceUTC()), - "10.10.0.50"); - indexDocument(service, w, "6", 100L, 100.3f, 100.3, "orange", new DateTime(2014, 6, 1, 0, 0, 0, ISOChronology.getInstanceUTC()), - "10.10.0.60"); - directoryReader = DirectoryReader.open(w, true, true); - w.close(); - ShardId shard = new ShardId("index", "_na_", 0); - directoryReader = ElasticsearchDirectoryReader.wrap(directoryReader, shard); - IndexSearcher s = new IndexSearcher(directoryReader); - searcher = new Engine.Searcher("test", s); - fieldStatsProvider = new FieldStatsProvider(searcher, service); - } - - @After - public void teardown() throws IOException { - searcher.close(); - directoryReader.close(); - dir.close(); - analysisRegistry.close(); - } - - public void testiIsFieldWithinQueryLong() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 10L, 200L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 10L, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", null, 200L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 10L, 100L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 50L, 200L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 30L, 80L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 80L, 200L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 60L, 80L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 10L, 100L, true, false, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 50L, 200L, false, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 100L, 200L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 1L, 50L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 150L, 200L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 1L, 8L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", null, 8L, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 150L, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 100L, 200L, false, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 1L, 50L, true, false, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - } - - public void testiIsFieldWithinQueryFloat() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 10.8f, 200.5f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 10.8f, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", null, 200.5f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 10.8f, 100.3f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 50.2f, 200.5f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 30.5f, 80.1f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 80.1f, 200.5f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 10.8f, 100.3f, true, false, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 50.2f, 200.5f, false, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 100.3f, 200.5f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 1.9f, 50.2f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 60.9f, 80.1f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 150.4f, 200.5f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 1.9f, 8.1f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", null, 8.1f, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 150.4f, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 100.3f, 200.5f, false, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("float_field", 1.9f, 50.2f, true, false, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - } - - public void testiIsFieldWithinQueryDouble() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 10.8, 200.5, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 10.8, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", null, 200.5, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 10.8, 100.3, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 50.2, 200.5, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 30.5, 80.1, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 80.1, 200.5, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 60.9, 80.1, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 10.8, 100.3, true, false, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 50.2, 200.5, false, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 100.3, 200.5, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 1.9, 50.2, true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 150.4, 200.5, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 1.9, 8.1, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", null, 8.1, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("double_field", 150.4, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 100.3, 200.5, false, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("long_field", 1.9, 50.2, true, false, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - } - - public void testiIsFieldWithinQueryText() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("banana"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("banana"), null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", null, new BytesRef("zebra"), true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("banana"), new BytesRef("orange"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("cherry"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("banana"), new BytesRef("grape"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("grape"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("lime"), new BytesRef("mango"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("banana"), new BytesRef("orange"), true, false, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("cherry"), new BytesRef("zebra"), false, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("orange"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("apple"), new BytesRef("cherry"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("peach"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("apple"), new BytesRef("banana"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", null, new BytesRef("banana"), true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("peach"), null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("orange"), new BytesRef("zebra"), false, true, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("text_field", new BytesRef("apple"), new BytesRef("cherry"), true, false, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - } - - public void testiIsFieldWithinQueryKeyword() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("banana"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("banana"), null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", null, new BytesRef("zebra"), true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("banana"), new BytesRef("orange"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("cherry"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("banana"), new BytesRef("grape"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("grape"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("lime"), new BytesRef("mango"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("banana"), new BytesRef("orange"), true, false, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("cherry"), new BytesRef("zebra"), false, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("orange"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("apple"), new BytesRef("cherry"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("peach"), new BytesRef("zebra"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("apple"), new BytesRef("banana"), true, true, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", null, new BytesRef("banana"), true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("peach"), null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("orange"), new BytesRef("zebra"), false, true, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("keyword_field", new BytesRef("apple"), new BytesRef("cherry"), true, false, - DateTimeZone.UTC, null), equalTo(Relation.DISJOINT)); - } - - public void testiIsFieldWithinQueryDate() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "now", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", null, "now", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "2014-06-01", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2014-01-01", "now", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "2014-03-01", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2014-03-01", "now", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2014-03-01", "2014-05-01", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "2014-06-01", true, false, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2014-01-01", "now", false, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2014-06-01", "now", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "2014-01-01", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2015-01-01", "now", true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "2013-09-01", true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", null, "2013-09-01", true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2015-01-01", null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2014-06-01", "now", false, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("date_field", "2013-01-01", "2014-01-01", true, false, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - } - - public void testiIsFieldWithinQueryIp() throws IOException { - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.1", "10.20.0.1", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.1", null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", null, "10.20.0.1", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", null, null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.1", "10.10.0.60", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.10", "10.20.0.1", true, true, DateTimeZone.UTC, null), - equalTo(Relation.WITHIN)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.1", "10.10.0.40", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.40", "10.20.0.1", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.30", "10.10.0.40", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.1", "10.10.0.60", true, false, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.10", "10.20.0.1", false, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.60", "10.20.0.1", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.0.0.1", "10.10.0.10", true, true, DateTimeZone.UTC, null), - equalTo(Relation.INTERSECTS)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.20.0.10", "10.20.0.1", true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.0.0.1", "10.0.0.100", true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", null, "10.0.0.100", true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.20.0.10", null, true, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.10.0.60", "10.20.0.1", false, true, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - assertThat(fieldStatsProvider.isFieldWithinQuery("ip_field", "10.0.0.1", "10.10.0.10", true, false, DateTimeZone.UTC, null), - equalTo(Relation.DISJOINT)); - } - - private void putMapping(MapperService service) throws IOException { - XContentBuilder mappingbuilder = JsonXContent.contentBuilder(); - mappingbuilder.startObject(); - mappingbuilder.startObject("type"); - mappingbuilder.startObject("properties"); - mappingbuilder.startObject("long_field"); - mappingbuilder.field("type", "long"); - mappingbuilder.endObject(); - mappingbuilder.startObject("float_field"); - mappingbuilder.field("type", "float"); - mappingbuilder.endObject(); - mappingbuilder.startObject("double_field"); - mappingbuilder.field("type", "double"); - mappingbuilder.endObject(); - mappingbuilder.startObject("text_field"); - mappingbuilder.field("type", "text"); - mappingbuilder.endObject(); - mappingbuilder.startObject("keyword_field"); - mappingbuilder.field("type", "keyword"); - mappingbuilder.endObject(); - mappingbuilder.startObject("date_field"); - mappingbuilder.field("type", "date"); - mappingbuilder.endObject(); - mappingbuilder.startObject("ip_field"); - mappingbuilder.field("type", "ip"); - mappingbuilder.endObject(); - mappingbuilder.endObject(); - mappingbuilder.endObject(); - mappingbuilder.endObject(); - service.merge("type", new CompressedXContent(mappingbuilder.bytes()), MergeReason.MAPPING_UPDATE, true); - } - - private void indexDocument(MapperService service, IndexWriter writer, String id, long longValue, float floatValue, double doubleValue, - String stringValue, DateTime dateValue, String ipValue) throws IOException { - XContentBuilder docBuilder = JsonXContent.contentBuilder(); - docBuilder.startObject(); - docBuilder.field("long_field", longValue); - docBuilder.field("float_field", floatValue); - docBuilder.field("double_field", doubleValue); - docBuilder.field("text_field", stringValue); - docBuilder.field("keyword_field", stringValue); - docBuilder.field("date_field", dateValue); - docBuilder.field("ip_field", ipValue); - docBuilder.endObject(); - DocumentMapper documentMapper = service.documentMapper("type"); - ParsedDocument doc = documentMapper.parse("index", "type", id, docBuilder.bytes()); - writer.addDocument(doc.rootDoc()); - } -} diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java index 0e009891cf28b..089533023d8d2 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java @@ -18,11 +18,27 @@ */ package org.elasticsearch.index.mapper.core; +import org.apache.lucene.document.Field.Store; +import org.apache.lucene.document.LegacyLongField; +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.MultiReader; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.IOUtils; +import org.elasticsearch.common.joda.DateMathParser; +import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.ParseContext.Document; +import org.elasticsearch.index.mapper.core.DateFieldMapper.DateFieldType; +import org.joda.time.DateTimeZone; import org.junit.Before; +import java.io.IOException; import java.util.Locale; import java.util.concurrent.TimeUnit; @@ -54,4 +70,58 @@ public void modify(MappedFieldType ft) { } }); } + + public void testIsFieldWithinQueryEmptyReader() throws IOException { + IndexReader reader = new MultiReader(); + DateFieldType ft = new DateFieldType(); + ft.setName("my_date"); + assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", + randomBoolean(), randomBoolean(), null, null)); + } + + private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader, + DateTimeZone zone, DateMathParser alternateFormat) throws IOException { + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", + randomBoolean(), randomBoolean(), null, null)); + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2016-01-02", "2016-06-20", + randomBoolean(), randomBoolean(), null, null)); + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2016-01-02", "2016-02-12", + randomBoolean(), randomBoolean(), null, null)); + assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2014-01-02", "2015-02-12", + randomBoolean(), randomBoolean(), null, null)); + assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2016-05-11", "2016-08-30", + randomBoolean(), randomBoolean(), null, null)); + assertEquals(Relation.WITHIN, ft.isFieldWithinQuery(reader, "2015-09-25", "2016-05-29", + randomBoolean(), randomBoolean(), null, null)); + assertEquals(Relation.WITHIN, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", + true, true, null, null)); + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", + false, false, null, null)); + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", + false, true, null, null)); + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", + true, false, null, null)); + } + + public void testIsFieldWithinQuery() throws IOException { + Directory dir = newDirectory(); + IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null)); + long instant1 = DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-12").getMillis(); + long instant2 = DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime("2016-04-03").getMillis(); + Document doc = new Document(); + LegacyLongField field = new LegacyLongField("my_date", instant1, Store.NO); + doc.add(field); + w.addDocument(doc); + field.setLongValue(instant2); + w.addDocument(doc); + DirectoryReader reader = DirectoryReader.open(w); + DateFieldType ft = new DateFieldType(); + ft.setName("my_date"); + DateMathParser alternateFormat = new DateMathParser(DateFieldMapper.Defaults.DATE_TIME_FORMATTER); + doTestIsFieldWithinQuery(ft, reader, null, null); + doTestIsFieldWithinQuery(ft, reader, null, alternateFormat); + doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null); + doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat); + IOUtils.close(reader, w, dir); + } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java index 5f34e746ecead..1c8cd4fda3665 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DoubleFieldTypeTests.java @@ -20,8 +20,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.core.DoubleFieldMapper.DoubleFieldType; import org.junit.Before; +import java.io.IOException; + public class DoubleFieldTypeTests extends FieldTypeTestCase { @Override protected MappedFieldType createDefaultFieldType() { @@ -32,4 +36,11 @@ protected MappedFieldType createDefaultFieldType() { public void setupProperties() { setDummyNullValue(10.0D); } + + public void testIsFieldWithinQuery() throws IOException { + DoubleFieldType ft = new DoubleFieldType(); + // current impl ignores args and shourd always return INTERSECTS + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomDouble(), randomDouble(), + randomBoolean(), randomBoolean(), null, null)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java index 73d593ac2f6e4..b9c222bc4029b 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/FloatFieldTypeTests.java @@ -20,8 +20,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.core.FloatFieldMapper.FloatFieldType; import org.junit.Before; +import java.io.IOException; + public class FloatFieldTypeTests extends FieldTypeTestCase { @Override protected MappedFieldType createDefaultFieldType() { @@ -32,4 +36,11 @@ protected MappedFieldType createDefaultFieldType() { public void setupProperties() { setDummyNullValue(10.0); } + + public void testIsFieldWithinQuery() throws IOException { + FloatFieldType ft = new FloatFieldType(); + // current impl ignores args and shourd always return INTERSECTS + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomFloat(), randomFloat(), + randomBoolean(), randomBoolean(), null, null)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java index b8c40af72c5ae..47f8344513fa7 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/IntegerFieldTypeTests.java @@ -20,8 +20,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.core.IntegerFieldMapper.IntegerFieldType; import org.junit.Before; +import java.io.IOException; + public class IntegerFieldTypeTests extends FieldTypeTestCase { @Override protected MappedFieldType createDefaultFieldType() { @@ -32,4 +36,11 @@ protected MappedFieldType createDefaultFieldType() { public void setupProperties() { setDummyNullValue(10); } + + public void testIsFieldWithinQuery() throws IOException { + IntegerFieldType ft = new IntegerFieldType(); + // current impl ignores args and shourd always return INTERSECTS + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomInt(), randomInt(), + randomBoolean(), randomBoolean(), null, null)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/KeywordFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/KeywordFieldTypeTests.java index 699717b5893f7..0d8ab6b804975 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/KeywordFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/KeywordFieldTypeTests.java @@ -18,12 +18,27 @@ */ package org.elasticsearch.index.mapper.core; +import com.carrotsearch.randomizedtesting.generators.RandomStrings; + import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.core.KeywordFieldMapper.KeywordFieldType; + +import java.io.IOException; public class KeywordFieldTypeTests extends FieldTypeTestCase { @Override protected MappedFieldType createDefaultFieldType() { return new KeywordFieldMapper.KeywordFieldType(); } + + public void testIsFieldWithinQuery() throws IOException { + KeywordFieldType ft = new KeywordFieldType(); + // current impl ignores args and shourd always return INTERSECTS + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, + RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), + RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), + randomBoolean(), randomBoolean(), null, null)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java index e7b41bf21d1db..0b4b374a4802a 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/LongFieldTypeTests.java @@ -20,8 +20,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.core.LongFieldMapper.LongFieldType; import org.junit.Before; +import java.io.IOException; + public class LongFieldTypeTests extends FieldTypeTestCase { @Override protected MappedFieldType createDefaultFieldType() { @@ -32,4 +36,11 @@ protected MappedFieldType createDefaultFieldType() { public void setupProperties() { setDummyNullValue((long)10); } + + public void testIsFieldWithinQuery() throws IOException { + LongFieldType ft = new LongFieldType(); + // current impl ignores args and shourd always return INTERSECTS + assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomLong(), randomLong(), + randomBoolean(), randomBoolean(), null, null)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/percolator/PercolatorQueryCacheTests.java b/core/src/test/java/org/elasticsearch/index/percolator/PercolatorQueryCacheTests.java index e10a63bca2c3b..31c8a4c57d209 100644 --- a/core/src/test/java/org/elasticsearch/index/percolator/PercolatorQueryCacheTests.java +++ b/core/src/test/java/org/elasticsearch/index/percolator/PercolatorQueryCacheTests.java @@ -117,7 +117,7 @@ void initialize(Object... fields) throws IOException { MapperService.MergeReason.MAPPING_UPDATE, false); cache = new PercolatorQueryCache(idxSettings, () -> queryShardContext); queryShardContext = new QueryShardContext(idxSettings, null, null, mapperService, similarityService, null, - indicesQueriesRegistry, cache); + indicesQueriesRegistry, cache, null); } public void testLoadQueries() throws Exception { diff --git a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java index e404870d723f7..2eec53f008be1 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java @@ -287,7 +287,7 @@ public void onRemoval(ShardId shardId, Accountable accountable) { }); PercolatorQueryCache percolatorQueryCache = new PercolatorQueryCache(idxSettings, () -> queryShardContext); indicesQueriesRegistry = injector.getInstance(IndicesQueriesRegistry.class); - queryShardContext = new QueryShardContext(idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, percolatorQueryCache); + queryShardContext = new QueryShardContext(idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, percolatorQueryCache, null); //create some random type with some default field, those types will stick around for all of the subclasses currentTypes = new String[randomIntBetween(0, 5)]; for (int i = 0; i < currentTypes.length; i++) { @@ -341,7 +341,6 @@ protected void setSearchContext(String[] types) { @After public void afterTest() { - queryShardContext.setFieldStatsProvider(null); clientInvocationHandler.delegate = null; SearchContext.removeCurrent(); } diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java index f705db3a537a4..68332d755b6ad 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java @@ -47,7 +47,7 @@ public void testFailIfFieldMappingNotFound() { MapperService mapperService = mock(MapperService.class); when(mapperService.getIndexSettings()).thenReturn(indexSettings); QueryShardContext context = new QueryShardContext( - indexSettings, null, null, mapperService, null, null, null, null + indexSettings, null, null, mapperService, null, null, null, null, null ); context.setAllowUnmappedFields(false); diff --git a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index eb373539994a7..c2102ebb4482f 100644 --- a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -22,13 +22,11 @@ import org.apache.lucene.search.LegacyNumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermRangeQuery; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.action.fieldstats.FieldStats; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.lucene.BytesRefs; -import org.elasticsearch.index.fieldstats.FieldStatsProvider; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.chrono.ISOChronology; @@ -399,317 +397,13 @@ public void testNamedQueryParsing() throws IOException { } } - public void testRewriteLongToMatchAll() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - int queryFromValue = randomIntBetween(-1000000, 1000000); - int queryToValue = randomIntBetween(queryFromValue, 2000000); - long shardMinValue = randomIntBetween(queryFromValue, queryToValue); - long shardMaxValue = randomIntBetween((int) shardMinValue, queryToValue); - query.from((long) queryFromValue); - query.to((long) queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.WITHIN; - } - - @SuppressWarnings("unchecked") - @Override - public > FieldStats get(String field) throws IOException { - assertThat(field, equalTo(fieldName)); - return (FieldStats) new FieldStats.Long(randomLong(), randomLong(), randomLong(), randomLong(), shardMinValue, - shardMaxValue); - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); - RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; - assertThat(rewrittenRange.fieldName(), equalTo(fieldName)); - assertThat(rewrittenRange.from(), equalTo(null)); - assertThat(rewrittenRange.to(), equalTo(null)); - } - - public void testRewriteLongToMatchNone() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - int queryFromValue = randomIntBetween(-1000000, 1000000); - int queryToValue = randomIntBetween(queryFromValue, 2000000); - query.from((long) queryFromValue); - query.to((long) queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.DISJOINT; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); - } - - public void testRewriteLongToSame() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - int queryFromValue = randomIntBetween(-1000000, 1000000); - int queryToValue = randomIntBetween(queryFromValue, 2000000); - query.from((long) queryFromValue); - query.to((long) queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.INTERSECTS; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, sameInstance(query)); - } - - public void testRewriteDoubleToMatchAll() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - double queryFromValue = randomDoubleBetween(-1000000.0, 1000000.0, true); - double queryToValue = randomDoubleBetween(queryFromValue, 2000000, true); - double shardMinValue = randomDoubleBetween(queryFromValue, queryToValue, true); - double shardMaxValue = randomDoubleBetween(shardMinValue, queryToValue, true); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.WITHIN; - } - - @SuppressWarnings("unchecked") - @Override - public > FieldStats get(String field) throws IOException { - assertThat(field, equalTo(fieldName)); - return (FieldStats) new FieldStats.Double(randomLong(), randomLong(), randomLong(), randomLong(), shardMinValue, - shardMaxValue); - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); - RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; - assertThat(rewrittenRange.fieldName(), equalTo(fieldName)); - assertThat(rewrittenRange.from(), equalTo(null)); - assertThat(rewrittenRange.to(), equalTo(null)); - } - - public void testRewriteDoubleToMatchNone() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - double queryFromValue = randomDoubleBetween(-1000000, 1000000, true); - double queryToValue = randomDoubleBetween(queryFromValue, 2000000, true); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.DISJOINT; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); - } - - public void testRewriteDoubleToSame() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - double queryFromValue = randomDoubleBetween(-1000000, 1000000, true); - double queryToValue = randomDoubleBetween(queryFromValue, 2000000, true); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.INTERSECTS; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, sameInstance(query)); - } - - public void testRewriteFloatToMatchAll() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - float queryFromValue = (float) randomDoubleBetween(-1000000.0, 1000000.0, true); - float queryToValue = (float) randomDoubleBetween(queryFromValue, 2000000, true); - float shardMinValue = (float) randomDoubleBetween(queryFromValue, queryToValue, true); - float shardMaxValue = (float) randomDoubleBetween(shardMinValue, queryToValue, true); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.WITHIN; - } - - @SuppressWarnings("unchecked") - @Override - public > FieldStats get(String field) throws IOException { - assertThat(field, equalTo(fieldName)); - return (FieldStats) new FieldStats.Float(randomLong(), randomLong(), randomLong(), randomLong(), shardMinValue, - shardMaxValue); - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); - RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; - assertThat(rewrittenRange.fieldName(), equalTo(fieldName)); - assertThat(rewrittenRange.from(), equalTo(null)); - assertThat(rewrittenRange.to(), equalTo(null)); - } - - public void testRewriteFloatToMatchNone() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - float queryFromValue = (float) randomDoubleBetween(-1000000, 1000000, true); - float queryToValue = (float) randomDoubleBetween(queryFromValue, 2000000, true); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.DISJOINT; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); - } - - public void testRewriteFloatToSame() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - float queryFromValue = (float) randomDoubleBetween(-1000000, 1000000, true); - float queryToValue = (float) randomDoubleBetween(queryFromValue, 2000000, true); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.INTERSECTS; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, sameInstance(query)); - } - - public void testRewriteTextToMatchAll() throws IOException { + public void testRewriteDateToMatchAll() throws IOException { String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - String queryFromValue = "damson"; - String queryToValue = "plum"; - String shardMinValue = "grape"; - String shardMaxValue = "orange"; - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { + RangeQueryBuilder query = new RangeQueryBuilder(fieldName) { + protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException { return Relation.WITHIN; } - - @SuppressWarnings("unchecked") - @Override - public > FieldStats get(String field) throws IOException { - assertThat(field, equalTo(fieldName)); - return (FieldStats) new FieldStats.Text(randomLong(), randomLong(), randomLong(), randomLong(), - new BytesRef(shardMinValue), new BytesRef(shardMaxValue)); - } }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); - RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; - assertThat(rewrittenRange.fieldName(), equalTo(fieldName)); - assertThat(rewrittenRange.from(), equalTo(null)); - assertThat(rewrittenRange.to(), equalTo(null)); - } - - public void testRewriteTextToMatchNone() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - String queryFromValue = "damson"; - String queryToValue = "plum"; - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.DISJOINT; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); - } - - public void testRewriteTextToSame() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - String queryFromValue = "damson"; - String queryToValue = "plum"; - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.INTERSECTS; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, sameInstance(query)); - } - - public void testRewriteDateToMatchAll() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); DateTime queryFromValue = new DateTime(2015, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); DateTime queryToValue = new DateTime(2016, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); DateTime shardMinValue = new DateTime(2015, 3, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); @@ -717,23 +411,6 @@ public void testRewriteDateToMatchAll() throws IOException { query.from(queryFromValue); query.to(queryToValue); QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.WITHIN; - } - - @SuppressWarnings("unchecked") - @Override - public > FieldStats get(String field) throws IOException { - assertThat(field, equalTo(fieldName)); - return (FieldStats) new FieldStats.Date(randomLong(), randomLong(), randomLong(), randomLong(), - shardMinValue.getMillis(), shardMaxValue.getMillis(), null); - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); QueryBuilder rewritten = query.rewrite(queryShardContext); assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; @@ -742,121 +419,46 @@ public > FieldStats get(String field) throws IOExcept assertThat(rewrittenRange.to(), equalTo(null)); } - public void testRewriteDateWithNowToMatchAll() throws IOException { + public void testRewriteDateToMatchNone() throws IOException { String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - String queryFromValue = "now-2d"; - String queryToValue = "now"; - DateTime shardMinValue = new DateTime().minusHours(12); - DateTime shardMaxValue = new DateTime().minusHours(24); - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.WITHIN; - } - - @SuppressWarnings("unchecked") - @Override - public > FieldStats get(String field) throws IOException { - assertThat(field, equalTo(fieldName)); - return (FieldStats) new FieldStats.Date(randomLong(), randomLong(), randomLong(), randomLong(), - shardMinValue.getMillis(), shardMaxValue.getMillis(), null); + RangeQueryBuilder query = new RangeQueryBuilder(fieldName) { + protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException { + return Relation.DISJOINT; } }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); - RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; - assertThat(rewrittenRange.fieldName(), equalTo(fieldName)); - assertThat(rewrittenRange.from(), equalTo(null)); - assertThat(rewrittenRange.to(), equalTo(null)); - } - - public void testRewriteDateToMatchNone() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); DateTime queryFromValue = new DateTime(2015, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); DateTime queryToValue = new DateTime(2016, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); query.from(queryFromValue); query.to(queryToValue); QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.DISJOINT; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); QueryBuilder rewritten = query.rewrite(queryShardContext); assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); } - public void testRewriteDateWithNowToMatchNone() throws IOException { + public void testRewriteDateToSame() throws IOException { String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - String queryFromValue = "now-2d"; - String queryToValue = "now"; - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.DISJOINT; + RangeQueryBuilder query = new RangeQueryBuilder(fieldName) { + protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException { + return Relation.INTERSECTS; } }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); - QueryBuilder rewritten = query.rewrite(queryShardContext); - assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); - } - - public void testRewriteDateToSame() throws IOException { - String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); DateTime queryFromValue = new DateTime(2015, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); DateTime queryToValue = new DateTime(2016, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC()); query.from(queryFromValue); query.to(queryToValue); QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { - return Relation.INTERSECTS; - } - }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); QueryBuilder rewritten = query.rewrite(queryShardContext); assertThat(rewritten, sameInstance(query)); } - public void testRewriteDateWithNowToSame() throws IOException { + public void testRewriteOpenBoundsToSame() throws IOException { String fieldName = randomAsciiOfLengthBetween(1, 20); - RangeQueryBuilder query = new RangeQueryBuilder(fieldName); - String queryFromValue = "now-2d"; - String queryToValue = "now"; - query.from(queryFromValue); - query.to(queryToValue); - QueryShardContext queryShardContext = queryShardContext(); - FieldStatsProvider fieldStatsProvider = new FieldStatsProvider(null, null) { - - @Override - public Relation isFieldWithinQuery(String fieldName, Object from, Object to, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser dateMathParser) throws IOException { + RangeQueryBuilder query = new RangeQueryBuilder(fieldName) { + protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException { return Relation.INTERSECTS; } }; - queryShardContext.setFieldStatsProvider(fieldStatsProvider); + QueryShardContext queryShardContext = queryShardContext(); QueryBuilder rewritten = query.rewrite(queryShardContext); assertThat(rewritten, sameInstance(query)); } diff --git a/core/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java b/core/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java new file mode 100644 index 0000000000000..71d70e286182f --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java @@ -0,0 +1,81 @@ +/* + * 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.query; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.MultiReader; +import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.index.IndexService; +import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.MapperService.MergeReason; +import org.elasticsearch.test.ESSingleNodeTestCase; + +// The purpose of this test case is to test RangeQueryBuilder.getRelation() +// Whether it should return INTERSECT/DISJOINT/WITHIN is already tested in +// RangeQueryBuilderTests +public class RangeQueryRewriteTests extends ESSingleNodeTestCase { + + public void testRewriteMissingField() throws Exception { + IndexService indexService = createIndex("test"); + IndexReader reader = new MultiReader(); + QueryRewriteContext context = new QueryRewriteContext(indexService.getIndexSettings(), + indexService.mapperService(), null, null, reader); + RangeQueryBuilder range = new RangeQueryBuilder("foo"); + assertEquals(Relation.DISJOINT, range.getRelation(context)); + } + + public void testRewriteMissingReader() throws Exception { + IndexService indexService = createIndex("test"); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties") + .startObject("foo") + .field("type", "date") + .endObject() + .endObject() + .endObject().endObject().string(); + indexService.mapperService().merge("type", + new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false); + QueryRewriteContext context = new QueryRewriteContext(indexService.getIndexSettings(), + indexService.mapperService(), null, null, null); + RangeQueryBuilder range = new RangeQueryBuilder("foo"); + // can't make assumptions on a missing reader, so it must return INTERSECT + assertEquals(Relation.INTERSECTS, range.getRelation(context)); + } + + public void testRewriteEmptyReader() throws Exception { + IndexService indexService = createIndex("test"); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties") + .startObject("foo") + .field("type", "date") + .endObject() + .endObject() + .endObject().endObject().string(); + indexService.mapperService().merge("type", + new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false); + IndexReader reader = new MultiReader(); + QueryRewriteContext context = new QueryRewriteContext(indexService.getIndexSettings(), + indexService.mapperService(), null, null, reader); + RangeQueryBuilder range = new RangeQueryBuilder("foo"); + // no values -> DISJOINT + assertEquals(Relation.DISJOINT, range.getRelation(context)); + } +} diff --git a/core/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java b/core/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java index fec50cf0a27ba..fdd7e4c2d671a 100644 --- a/core/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java +++ b/core/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java @@ -85,20 +85,20 @@ public void testCacheAggs() throws Exception { } public void testQueryRewrite() throws Exception { - assertAcked(client().admin().indices().prepareCreate("index").addMapping("type", "s", "type=text") + assertAcked(client().admin().indices().prepareCreate("index").addMapping("type", "s", "type=date") .setSettings(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true, IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) .get()); - indexRandom(true, client().prepareIndex("index", "type", "1").setRouting("1").setSource("s", "a"), - client().prepareIndex("index", "type", "2").setRouting("1").setSource("s", "b"), - client().prepareIndex("index", "type", "3").setRouting("1").setSource("s", "c"), - client().prepareIndex("index", "type", "4").setRouting("2").setSource("s", "d"), - client().prepareIndex("index", "type", "5").setRouting("2").setSource("s", "e"), - client().prepareIndex("index", "type", "6").setRouting("2").setSource("s", "f"), - client().prepareIndex("index", "type", "7").setRouting("3").setSource("s", "g"), - client().prepareIndex("index", "type", "8").setRouting("3").setSource("s", "h"), - client().prepareIndex("index", "type", "9").setRouting("3").setSource("s", "i")); + indexRandom(true, client().prepareIndex("index", "type", "1").setRouting("1").setSource("s", "2016-03-19"), + client().prepareIndex("index", "type", "2").setRouting("1").setSource("s", "2016-03-20"), + client().prepareIndex("index", "type", "3").setRouting("1").setSource("s", "2016-03-21"), + client().prepareIndex("index", "type", "4").setRouting("2").setSource("s", "2016-03-22"), + client().prepareIndex("index", "type", "5").setRouting("2").setSource("s", "2016-03-23"), + client().prepareIndex("index", "type", "6").setRouting("2").setSource("s", "2016-03-24"), + client().prepareIndex("index", "type", "7").setRouting("3").setSource("s", "2016-03-25"), + client().prepareIndex("index", "type", "8").setRouting("3").setSource("s", "2016-03-26"), + client().prepareIndex("index", "type", "9").setRouting("3").setSource("s", "2016-03-27")); ensureSearchable("index"); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -107,7 +107,7 @@ public void testQueryRewrite() throws Exception { equalTo(0L)); final SearchResponse r1 = client().prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0) - .setQuery(QueryBuilders.rangeQuery("s").gte("a").lte("g")).get(); + .setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-25")).get(); assertSearchResponse(r1); assertThat(r1.getHits().getTotalHits(), equalTo(7L)); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -116,7 +116,7 @@ public void testQueryRewrite() throws Exception { equalTo(5L)); final SearchResponse r2 = client().prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0) - .setQuery(QueryBuilders.rangeQuery("s").gte("b").lte("h")).get(); + .setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-20").lte("2016-03-26")).get(); assertSearchResponse(r2); assertThat(r2.getHits().getTotalHits(), equalTo(7L)); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -125,7 +125,7 @@ public void testQueryRewrite() throws Exception { equalTo(7L)); final SearchResponse r3 = client().prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0) - .setQuery(QueryBuilders.rangeQuery("s").gte("c").lte("i")).get(); + .setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-21").lte("2016-03-27")).get(); assertSearchResponse(r3); assertThat(r3.getHits().getTotalHits(), equalTo(7L)); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -135,19 +135,19 @@ public void testQueryRewrite() throws Exception { } public void testQueryRewriteMissingValues() throws Exception { - assertAcked(client().admin().indices().prepareCreate("index").addMapping("type", "s", "type=text") + assertAcked(client().admin().indices().prepareCreate("index").addMapping("type", "s", "type=date") .setSettings(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true, IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) .get()); - indexRandom(true, client().prepareIndex("index", "type", "1").setSource("s", "a"), - client().prepareIndex("index", "type", "2").setSource("s", "b"), - client().prepareIndex("index", "type", "3").setSource("s", "c"), - client().prepareIndex("index", "type", "4").setSource("s", "d"), - client().prepareIndex("index", "type", "5").setSource("s", "e"), - client().prepareIndex("index", "type", "6").setSource("s", "f"), + indexRandom(true, client().prepareIndex("index", "type", "1").setSource("s", "2016-03-19"), + client().prepareIndex("index", "type", "2").setSource("s", "2016-03-20"), + client().prepareIndex("index", "type", "3").setSource("s", "2016-03-21"), + client().prepareIndex("index", "type", "4").setSource("s", "2016-03-22"), + client().prepareIndex("index", "type", "5").setSource("s", "2016-03-23"), + client().prepareIndex("index", "type", "6").setSource("s", "2016-03-24"), client().prepareIndex("index", "type", "7").setSource("other", "value"), - client().prepareIndex("index", "type", "8").setSource("s", "h"), - client().prepareIndex("index", "type", "9").setSource("s", "i")); + client().prepareIndex("index", "type", "8").setSource("s", "2016-03-26"), + client().prepareIndex("index", "type", "9").setSource("s", "2016-03-27")); ensureSearchable("index"); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -156,7 +156,7 @@ public void testQueryRewriteMissingValues() throws Exception { equalTo(0L)); final SearchResponse r1 = client().prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0) - .setQuery(QueryBuilders.rangeQuery("s").gte("a").lte("j")).get(); + .setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-28")).get(); assertSearchResponse(r1); assertThat(r1.getHits().getTotalHits(), equalTo(8L)); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -165,7 +165,7 @@ public void testQueryRewriteMissingValues() throws Exception { equalTo(1L)); final SearchResponse r2 = client().prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0) - .setQuery(QueryBuilders.rangeQuery("s").gte("a").lte("j")).get(); + .setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-28")).get(); assertSearchResponse(r2); assertThat(r2.getHits().getTotalHits(), equalTo(8L)); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), @@ -174,7 +174,7 @@ public void testQueryRewriteMissingValues() throws Exception { equalTo(1L)); final SearchResponse r3 = client().prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0) - .setQuery(QueryBuilders.rangeQuery("s").gte("a").lte("j")).get(); + .setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-28")).get(); assertSearchResponse(r3); assertThat(r3.getHits().getTotalHits(), equalTo(8L)); assertThat(client().admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getHitCount(), diff --git a/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java b/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java index d885619cf1fe5..1ea4212511f7e 100644 --- a/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java @@ -291,7 +291,7 @@ public void testBuildSearchContextHighlight() throws IOException { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, indexSettings); // shard context will only need indicesQueriesRegistry for building Query objects nested in highlighter QueryShardContext mockShardContext = new QueryShardContext(idxSettings, null, null, null, null, null, indicesQueriesRegistry, - null) { + null, null) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); diff --git a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java index 7e4ff9449b663..4418fe848a83b 100644 --- a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java @@ -163,7 +163,7 @@ public void testBuildRescoreSearchContext() throws ElasticsearchParseException, IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAsciiOfLengthBetween(1, 10), indexSettings); // shard context will only need indicesQueriesRegistry for building Query objects nested in query rescorer QueryShardContext mockShardContext = new QueryShardContext(idxSettings, null, null, null, null, null, indicesQueriesRegistry, - null) { + null, null) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index 0112e252ac998..b84ec9b436d42 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -225,7 +225,7 @@ public void onCache(ShardId shardId, Accountable accountable) { } }); return new QueryShardContext(idxSettings, bitsetFilterCache, ifds, null, null, scriptService, - indicesQueriesRegistry, null) { + indicesQueriesRegistry, null, null) { @Override public MappedFieldType fieldMapper(String name) { return provideMappedFieldType(name); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/TemplateQueryParserTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/TemplateQueryParserTests.java index b8aff4fbdd187..358233cbca36d 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/TemplateQueryParserTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/TemplateQueryParserTests.java @@ -149,7 +149,7 @@ public void onRemoval(ShardId shardId, Accountable accountable) { } }); IndicesQueriesRegistry indicesQueriesRegistry = injector.getInstance(IndicesQueriesRegistry.class); - context = new QueryShardContext(idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, null); + context = new QueryShardContext(idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, null, null); } @Override