@Override public QueryShardContext getQueryShardContext() { return in.getQueryShardContext(); }
public FilterAggregatorFactory(String name, QueryBuilder filterBuilder, SearchContext context, AggregatorFactory<?> parent, AggregatorFactories.Builder subFactoriesBuilder, Map<String, Object> metaData) throws IOException { super(name, context, parent, subFactoriesBuilder, metaData); filter = filterBuilder.toFilter(context.getQueryShardContext()); }
protected ValuesSourceConfig<VS> resolveConfig(SearchContext context) { ValueType valueType = this.valueType != null ? this.valueType : targetValueType; return ValuesSourceConfig.resolve(context.getQueryShardContext(), valueType, field, script, missing, timeZone, format); }
public SearchLookup lookup() { return getQueryShardContext().lookup(); }
public final CompositeValuesSourceConfig build(SearchContext context) throws IOException { ValuesSourceConfig<?> config = ValuesSourceConfig.resolve(context.getQueryShardContext(), valueType, field, script, missing, null, format); if (missingBucket && missing != null) { throw new QueryShardException(context.getQueryShardContext(), "cannot use [missing] option in conjunction with [missing_bucket]"); } return innerBuild(context, config); } }
public FiltersAggregatorFactory(String name, List<KeyedFilter> filters, boolean keyed, boolean otherBucket, String otherBucketKey, SearchContext context, AggregatorFactory<?> parent, AggregatorFactories.Builder subFactories, Map<String, Object> metaData) throws IOException { super(name, context, parent, subFactories, metaData); this.keyed = keyed; this.otherBucket = otherBucket; this.otherBucketKey = otherBucketKey; keys = new String[filters.size()]; this.filters = new Query[filters.size()]; for (int i = 0; i < filters.size(); ++i) { KeyedFilter keyedFilter = filters.get(i); this.keys[i] = keyedFilter.key(); this.filters[i] = keyedFilter.filter().toFilter(context.getQueryShardContext()); } }
/** * Parse the bounds and perform any delayed validation. Returns the result of the parsing. */ ExtendedBounds parseAndValidate(String aggName, SearchContext context, DocValueFormat format) { Long min = this.min; Long max = this.max; assert format != null; if (minAsStr != null) { min = format.parseLong(minAsStr, false, context.getQueryShardContext()::nowInMillis); } if (maxAsStr != null) { // TODO: Should we rather pass roundUp=true? max = format.parseLong(maxAsStr, false, context.getQueryShardContext()::nowInMillis); } if (min != null && max != null && min.compareTo(max) > 0) { throw new SearchParseException(context, "[extended_bounds.min][" + min + "] cannot be greater than " + "[extended_bounds.max][" + max + "] for histogram aggregation [" + aggName + "]", null); } return new ExtendedBounds(min, max, minAsStr, maxAsStr); }
public AdjacencyMatrixAggregatorFactory(String name, List<KeyedFilter> filters, String separator, SearchContext context, AggregatorFactory<?> parent, AggregatorFactories.Builder subFactories, Map<String, Object> metaData) throws IOException { super(name, context, parent, subFactories, metaData); IndexSearcher contextSearcher = context.searcher(); this.separator = separator; weights = new Weight[filters.size()]; keys = new String[filters.size()]; for (int i = 0; i < filters.size(); ++i) { KeyedFilter keyedFilter = filters.get(i); this.keys[i] = keyedFilter.key(); Query filter = keyedFilter.filter().toFilter(context.getQueryShardContext()); this.weights[i] = contextSearcher.createNormalizedWeight(filter, false); } }
@Override public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { VS vs = config.toValuesSource(context.getQueryShardContext()); if (vs == null) { return createUnmapped(parent, pipelineAggregators, metaData); } return doCreateInternal(vs, parent, collectsFromSingleBucket, pipelineAggregators, metaData); }
@Override protected final MultiValuesSourceAggregatorFactory<VS, ?> doBuild(SearchContext context, AggregatorFactory<?> parent, AggregatorFactories.Builder subFactoriesBuilder) throws IOException { ValueType finalValueType = this.valueType != null ? this.valueType : targetValueType; Map<String, ValuesSourceConfig<VS>> configs = new HashMap<>(fields.size()); fields.forEach((key, value) -> { ValuesSourceConfig<VS> config = ValuesSourceConfig.resolve(context.getQueryShardContext(), finalValueType, value.getFieldName(), value.getScript(), value.getMissing(), value.getTimeZone(), format); configs.put(key, config); }); DocValueFormat docValueFormat = resolveFormat(format, finalValueType); return innerBuild(context, configs, docValueFormat, parent, subFactoriesBuilder); }
/** * Try to load the query results from the cache or execute the query phase directly if the cache cannot be used. */ private void loadOrExecuteQueryPhase(final ShardSearchRequest request, final SearchContext context) throws Exception { final boolean canCache = indicesService.canCache(request, context); context.getQueryShardContext().freezeContext(); if (canCache) { indicesService.loadIntoContext(request, context, queryPhase); } else { queryPhase.execute(context); } }
@Override public SignificanceHeuristic rewrite(SearchContext context) { QueryShardContext shardContext = context.getQueryShardContext(); SignificantTermsHeuristicScoreScript.Factory compiledScript = shardContext.getScriptService().compile(script, SignificantTermsHeuristicScoreScript.CONTEXT); return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); }
@Override protected Aggregator doCreateInternal(Map<String, ValuesSourceConfig<Numeric>> configs, DocValueFormat format, Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { MultiValuesSource.NumericMultiValuesSource numericMultiVS = new MultiValuesSource.NumericMultiValuesSource(configs, context.getQueryShardContext()); if (numericMultiVS.areValuesSourcesEmpty()) { return createUnmapped(parent, pipelineAggregators, metaData); } return new WeightedAvgAggregator(name, numericMultiVS, format, context, parent, pipelineAggregators, metaData); } }
@Override protected AggregatorFactory<?> doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException { int maxFilters = context.indexShard().indexSettings().getMaxAdjacencyMatrixFilters(); if (filters.size() > maxFilters){ throw new QueryPhaseExecutionException(context, "Number of filters is too large, must be less than or equal to: [" + maxFilters + "] but was [" + filters.size() + "]." + "This limit can be set by changing the [" + IndexSettings.MAX_ADJACENCY_MATRIX_FILTERS_SETTING.getKey() + "] index level setting."); } List<KeyedFilter> rewrittenFilters = new ArrayList<>(filters.size()); for (KeyedFilter kf : filters) { rewrittenFilters.add(new KeyedFilter(kf.key(), Rewriteable.rewrite(kf.filter(), context.getQueryShardContext(), true))); } return new AdjacencyMatrixAggregatorFactory(name, rewrittenFilters, separator, context, parent, subFactoriesBuilder, metaData); }
@Override protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig<?> config) throws IOException { ValuesSource orig = config.toValuesSource(context.getQueryShardContext()); if (orig == null) { orig = ValuesSource.Numeric.EMPTY; } if (orig instanceof ValuesSource.Numeric) { ValuesSource.Numeric numeric = (ValuesSource.Numeric) orig; final HistogramValuesSource vs = new HistogramValuesSource(numeric, interval); final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; return new CompositeValuesSourceConfig(name, fieldType, vs, config.format(), order(), missingBucket(), missing()); } else { throw new IllegalArgumentException("invalid source, expected numeric, got " + orig.getClass().getSimpleName()); } } }
@Override protected AggregatorFactory<?> doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException { ObjectMapper childObjectMapper = context.getObjectMapper(path); if (childObjectMapper == null) { // in case the path has been unmapped: return new NestedAggregatorFactory(name, null, null, context, parent, subFactoriesBuilder, metaData); } if (childObjectMapper.nested().isNested() == false) { throw new AggregationExecutionException("[nested] nested path [" + path + "] is not nested"); } try { ObjectMapper parentObjectMapper = context.getQueryShardContext().nestedScope().nextLevel(childObjectMapper); return new NestedAggregatorFactory(name, parentObjectMapper, childObjectMapper, context, parent, subFactoriesBuilder, metaData); } finally { context.getQueryShardContext().nestedScope().previousLevel(); } }
@Override protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig<?> config) throws IOException { Rounding rounding = createRounding(); ValuesSource orig = config.toValuesSource(context.getQueryShardContext()); if (orig == null) { orig = ValuesSource.Numeric.EMPTY; } if (orig instanceof ValuesSource.Numeric) { ValuesSource.Numeric numeric = (ValuesSource.Numeric) orig; RoundingValuesSource vs = new RoundingValuesSource(numeric, rounding); // is specified in the builder. final DocValueFormat docValueFormat = format() == null ? DocValueFormat.RAW : config.format(); final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; return new CompositeValuesSourceConfig(name, fieldType, vs, docValueFormat, order(), missingBucket(), missing()); } else { throw new IllegalArgumentException("invalid source, expected numeric, got " + orig.getClass().getSimpleName()); } } }
@Override protected ValuesSourceAggregatorFactory<Numeric, ?> innerBuild(SearchContext context, ValuesSourceConfig<Numeric> config, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException { final DateTimeZone tz = timeZone(); final Rounding rounding = createRounding(tz); final DateTimeZone rewrittenTimeZone = rewriteTimeZone(context.getQueryShardContext()); final Rounding shardRounding; if (tz == rewrittenTimeZone) { shardRounding = rounding; } else { shardRounding = createRounding(rewrittenTimeZone); } ExtendedBounds roundedBounds = null; if (this.extendedBounds != null) { // parse any string bounds to longs and round roundedBounds = this.extendedBounds.parseAndValidate(name, context, config.format()).round(rounding); } return new DateHistogramAggregatorFactory(name, config, offset, order, keyed, minDocCount, rounding, shardRounding, roundedBounds, context, parent, subFactoriesBuilder, metaData); }
@Override protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig<?> config) throws IOException { ValuesSource vs = config.toValuesSource(context.getQueryShardContext()); if (vs == null) { vs = ValuesSource.Numeric.EMPTY; } final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; final DocValueFormat format; if (format() == null && fieldType instanceof DateFieldMapper.DateFieldType) { // defaults to the raw format on date fields (preserve timestamp as longs). format = DocValueFormat.RAW; } else { format = config.format(); } return new CompositeValuesSourceConfig(name, fieldType, vs, format, order(), missingBucket(), missing()); } }
private long getBackgroundFrequency(String value) throws IOException { Query query = fieldType.termQuery(value, context.getQueryShardContext()); if (query instanceof TermQuery) { // for types that use the inverted index, we prefer using a caching terms // enum that will do a better job at reusing index inputs Term term = ((TermQuery) query).getTerm(); FilterableTermsEnum termsEnum = getTermsEnum(term.field()); if (termsEnum.seekExact(term.bytes())) { return termsEnum.docFreq(); } else { return 0; } } // otherwise do it the naive way if (filter != null) { query = new BooleanQuery.Builder() .add(query, Occur.FILTER) .add(filter, Occur.FILTER) .build(); } return context.searcher().count(query); }