Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@
*/
public class FieldStatsShardResponse extends BroadcastShardResponse {

private Map<String, FieldStats> fieldStats;
private Map<String, FieldStats<?>> fieldStats;

public FieldStatsShardResponse() {
}

public FieldStatsShardResponse(ShardId shardId, Map<String, FieldStats> fieldStats) {
public FieldStatsShardResponse(ShardId shardId, Map<String, FieldStats<?>> fieldStats) {
super(shardId);
this.fieldStats = fieldStats;
}

public Map<String, FieldStats> getFieldStats() {
public Map<String, FieldStats<?>> getFieldStats() {
return fieldStats;
}

Expand All @@ -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<String, FieldStats> entry : fieldStats.entrySet()) {
for (Map.Entry<String, FieldStats<?>> entry : fieldStats.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,9 +99,9 @@ protected FieldStatsResponse newResponse(FieldStatsRequest request, AtomicRefere
indicesMergedFieldStats.put(indexName, indexMergedFieldStats = new HashMap<>());
}

Map<String, FieldStats> fieldStats = shardResponse.getFieldStats();
for (Map.Entry<String, FieldStats> entry : fieldStats.entrySet()) {
FieldStats existing = indexMergedFieldStats.get(entry.getKey());
Map<String, FieldStats<?>> fieldStats = shardResponse.getFieldStats();
for (Map.Entry<String, FieldStats<?>> entry : fieldStats.entrySet()) {
FieldStats<?> existing = indexMergedFieldStats.get(entry.getKey());
if (existing != null) {
if (existing.getType() != entry.getValue().getType()) {
throw new IllegalStateException(
Expand Down Expand Up @@ -156,22 +153,20 @@ protected FieldStatsShardResponse newShardResponse() {
@Override
protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request) {
ShardId shardId = request.shardId();
Map<String, FieldStats> fieldStats = new HashMap<>();
Map<String, FieldStats<?>> 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);
Expand Down
14 changes: 12 additions & 2 deletions core/src/main/java/org/elasticsearch/index/IndexService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,13 +39,15 @@
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;
import org.elasticsearch.index.fielddata.IndexFieldData;
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;
Expand Down Expand Up @@ -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 <tt>null</tt>. */
@Nullable
public Query queryStringTermQuery(Term term) {
Expand All @@ -424,4 +457,5 @@ public void setEagerGlobalOrdinals(boolean eagerGlobalOrdinals) {
checkIfFrozen();
this.eagerGlobalOrdinals = eagerGlobalOrdinals;
}

}
Loading