org.elasticsearch.index.query.ParsedQuery Java Examples

The following examples show how to use org.elasticsearch.index.query.ParsedQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchContextFactory.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public CrateSearchContext createContext(
        int jobSearchContextId,
        IndexShard indexshard,
        Engine.Searcher engineSearcher,
        WhereClause whereClause) {

    ShardId shardId = indexshard.shardId();
    SearchShardTarget searchShardTarget = new SearchShardTarget(
            clusterService.state().nodes().localNodeId(),
            shardId.getIndex(),
            shardId.id()
    );
    IndexService indexService = indexshard.indexService();
    CrateSearchContext searchContext = new CrateSearchContext(
            jobSearchContextId,
            System.currentTimeMillis(),
            searchShardTarget,
            engineSearcher,
            indexService,
            indexshard,
            scriptService,
            pageCacheRecycler,
            bigArrays,
            threadPool.estimatedTimeInMillisCounter(),
            Optional.<Scroll>absent()
    );
    LuceneQueryBuilder.Context context = luceneQueryBuilder.convert(
            whereClause,  indexService.mapperService(), indexService.fieldData(), indexService.cache());
    searchContext.parsedQuery(new ParsedQuery(context.query(), EMPTY_NAMED_FILTERS));

    Float minScore = context.minScore();
    if (minScore != null) {
        searchContext.minimumScore(minScore);
    }

    return searchContext;
}
 
Example #2
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Should be called before executing the main query and after all other parameters have been set.
 */
@Override
public void preProcess() {
    if (scrollContext == null) {
        long from = from() == -1 ? 0 : from();
        long size = size() == -1 ? 10 : size();
        long resultWindow = from + size;
        // We need settingsService's view of the settings because its dynamic.
        // indexService's isn't.
        int maxResultWindow = indexService.indexSettings().getAsInt(MAX_RESULT_WINDOW, Defaults.MAX_RESULT_WINDOW);

        if (resultWindow > maxResultWindow) {
            throw new QueryPhaseExecutionException(this,
                    "Result window is too large, from + size must be less than or equal to: [" + maxResultWindow + "] but was ["
                            + resultWindow + "]. See the scroll api for a more efficient way to request large data sets. "
                            + "This limit can be set by changing the [" + DefaultSearchContext.MAX_RESULT_WINDOW
                            + "] index level parameter.");
        }
    }

    // initialize the filtering alias based on the provided filters
    aliasFilter = indexService.aliasesService().aliasFilter(request.filteringAliases());

    if (query() == null) {
        parsedQuery(ParsedQuery.parsedMatchAllQuery());
    }
    if (queryBoost() != 1.0f) {
        parsedQuery(new ParsedQuery(new FunctionScoreQuery(query(), new BoostScoreFunction(queryBoost)), parsedQuery()));
    }
    filteredQuery(buildFilteredQuery());
    try {
        this.query = searcher().rewrite(this.query);
    } catch (IOException e) {
        throw new QueryPhaseExecutionException(this, "Failed to rewrite main query", e);
    }
}
 
Example #3
Source File: QueryRescorer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public RescoreSearchContext parse(XContentParser parser, SearchContext context) throws IOException {
    Token token;
    String fieldName = null;
    QueryRescoreContext rescoreContext = new QueryRescoreContext(this);
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
            if ("rescore_query".equals(fieldName)) {
                ParsedQuery parsedQuery = context.queryParserService().parse(parser);
                rescoreContext.setParsedQuery(parsedQuery);
            }
        } else if (token.isValue()) {
            if ("query_weight".equals(fieldName)) {
                rescoreContext.setQueryWeight(parser.floatValue());
            } else if ("rescore_query_weight".equals(fieldName)) {
                rescoreContext.setRescoreQueryWeight(parser.floatValue());
            } else if ("score_mode".equals(fieldName)) {
                String sScoreMode = parser.text();
                if ("avg".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Avg);
                } else if ("max".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Max);
                } else if ("min".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Min);
                } else if ("total".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Total);
                } else if ("multiply".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Multiply);
                } else {
                    throw new IllegalArgumentException("[rescore] illegal score_mode [" + sScoreMode + "]");
                }
            } else {
                throw new IllegalArgumentException("rescore doesn't support [" + fieldName + "]");
            }
        }
    }
    return rescoreContext;
}
 
Example #4
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected BaseInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits) {
    super(context);
    this.query = query;
    if (childInnerHits != null && !childInnerHits.isEmpty()) {
        this.childInnerHits = new InnerHitsContext(childInnerHits);
    } else {
        this.childInnerHits = null;
    }
}
 
Example #5
Source File: FilterBinaryParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    byte[] filterSource = parser.binaryValue();
    try (XContentParser fSourceParser = XContentFactory.xContent(filterSource).createParser(filterSource)) {
        ParsedQuery filter = context.queryParserService().parseInnerFilter(fSourceParser);
        if (filter != null) {
            context.parsedPostFilter(filter);
        }
    }
}
 
Example #6
Source File: PostFilterParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    ParsedQuery postFilter = context.queryParserService().parseInnerFilter(parser);
    if (postFilter != null) {
        context.parsedPostFilter(postFilter);
    }
}
 
Example #7
Source File: QueryCollector.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    final Query query = getQuery(doc);
    if (query == null) {
        // log???
        return;
    }
    Query existsQuery = query;
    if (isNestedDoc) {
        existsQuery = new BooleanQuery.Builder()
            .add(existsQuery, Occur.MUST)
            .add(Queries.newNonNestedFilter(), Occur.FILTER)
            .build();
    }
    // run the query
    try {
        if (context.highlight() != null) {
            context.parsedQuery(new ParsedQuery(query));
            context.hitContext().cache().clear();
        }
        if (Lucene.exists(searcher, existsQuery)) {
            if (!limit || counter < size) {
                matches.add(BytesRef.deepCopyOf(current));
                scores.add(scorer.score());
                if (context.highlight() != null) {
                    highlightPhase.hitExecute(context, context.hitContext());
                    hls.add(context.hitContext().hit().getHighlightFields());
                }
            }
            counter++;
            postMatch(doc);
        }
    } catch (IOException e) {
        logger.warn("[" + current.utf8ToString() + "] failed to execute query", e);
    }
}
 
Example #8
Source File: QueryCollector.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    final Query query = getQuery(doc);
    if (query == null) {
        // log???
        return;
    }
    Query existsQuery = query;
    if (isNestedDoc) {
        existsQuery = new BooleanQuery.Builder()
            .add(existsQuery, Occur.MUST)
            .add(Queries.newNonNestedFilter(), Occur.FILTER)
            .build();
    }
    // run the query
    try {
        if (context.highlight() != null) {
            context.parsedQuery(new ParsedQuery(query));
            context.hitContext().cache().clear();
        }

        if (Lucene.exists(searcher, existsQuery)) {
            if (!limit || counter < size) {
                matches.add(BytesRef.deepCopyOf(current));
                if (context.highlight() != null) {
                    highlightPhase.hitExecute(context, context.hitContext());
                    hls.add(context.hitContext().hit().getHighlightFields());
                }
            }
            counter++;
            postMatch(doc);
        }
    } catch (IOException e) {
        logger.warn("[" + current.utf8ToString() + "] failed to execute query", e);
    }
}
 
Example #9
Source File: TranslogRecoveryPerformer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static Engine.DeleteByQuery prepareDeleteByQuery(IndexQueryParserService queryParserService, MapperService mapperService, IndexAliasesService indexAliasesService, IndexCache indexCache, BytesReference source, @Nullable String[] filteringAliases, Engine.Operation.Origin origin, String... types) {
    long startTime = System.nanoTime();
    if (types == null) {
        types = Strings.EMPTY_ARRAY;
    }
    Query query;
    try {
        query = queryParserService.parseQuery(source).query();
    } catch (QueryParsingException ex) {
        // for BWC we try to parse directly the query since pre 1.0.0.Beta2 we didn't require a top level query field
        if (queryParserService.getIndexCreatedVersion().onOrBefore(Version.V_1_0_0_Beta2)) {
            try {
                XContentParser parser = XContentHelper.createParser(source);
                ParsedQuery parse = queryParserService.parse(parser);
                query = parse.query();
            } catch (Throwable t) {
                ex.addSuppressed(t);
                throw ex;
            }
        } else {
            throw ex;
        }
    }
    Query searchFilter = mapperService.searchFilter(types);
    if (searchFilter != null) {
        query = Queries.filtered(query, searchFilter);
    }

    Query aliasFilter = indexAliasesService.aliasFilter(filteringAliases);
    BitSetProducer parentFilter = mapperService.hasNested() ? indexCache.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter()) : null;
    return new Engine.DeleteByQuery(query, source, filteringAliases, aliasFilter, parentFilter, origin, startTime, types);
}
 
Example #10
Source File: IndexAliasesService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
Query parse(AliasMetaData alias) {
    if (alias.filter() == null) {
        return null;
    }
    try {
        byte[] filterSource = alias.filter().uncompressed();
        try (XContentParser parser = XContentFactory.xContent(filterSource).createParser(filterSource)) {
            ParsedQuery parsedFilter = indexQueryParser.parseInnerFilter(parser);
            return parsedFilter == null ? null : parsedFilter.query();
        }
    } catch (IOException ex) {
        throw new AliasFilterParsingException(index, alias.getAlias(), "Invalid alias filter", ex);
    }
}
 
Example #11
Source File: QueryRescorer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void setParsedQuery(ParsedQuery parsedQuery) {
    this.parsedQuery = parsedQuery;
}
 
Example #12
Source File: TransportTermsByQueryAction.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * The operation that executes the query and generates a {@link TermsByQueryShardResponse} for each shard.
 */
@Override
protected TermsByQueryShardResponse shardOperation(TermsByQueryShardRequest shardRequest) throws ElasticsearchException {
  IndexService indexService = indicesService.indexServiceSafe(shardRequest.shardId().getIndex());
  IndexShard indexShard = indexService.shardSafe(shardRequest.shardId().id());
  TermsByQueryRequest request = shardRequest.request();
  OrderByShardOperation orderByOperation = OrderByShardOperation.get(request.getOrderBy(), request.maxTermsPerShard());

  SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(),
                                                        shardRequest.shardId().getIndex(),
                                                        shardRequest.shardId().id());

  ShardSearchRequest shardSearchRequest = new ShardSearchLocalRequest(request.types(), request.nowInMillis(),
                                                                      shardRequest.filteringAliases());

  SearchContext context = new DefaultSearchContext(0, shardSearchRequest, shardTarget,
    indexShard.acquireSearcher("termsByQuery"), indexService, indexShard, scriptService,
    pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
    SearchService.NO_TIMEOUT);
  SearchContext.setCurrent(context);

  try {
    MappedFieldType fieldType = context.smartNameFieldType(request.field());
    if (fieldType == null) {
      throw new SearchContextException(context, "[termsByQuery] field '" + request.field() +
              "' not found for types " + Arrays.toString(request.types()));
    }

    IndexFieldData indexFieldData = context.fieldData().getForField(fieldType);

    BytesReference querySource = request.querySource();
    if (querySource != null && querySource.length() > 0) {
      XContentParser queryParser = null;
      try {
        queryParser = XContentFactory.xContent(querySource).createParser(querySource);
        QueryParseContext.setTypes(request.types());
        ParsedQuery parsedQuery = orderByOperation.getParsedQuery(queryParser, indexService);
        if (parsedQuery != null) {
          context.parsedQuery(parsedQuery);
        }
      }
      finally {
        QueryParseContext.removeTypes();
        if (queryParser != null) {
          queryParser.close();
        }
      }
    }

    context.preProcess();

    // execute the search only gathering the hit count and bitset for each segment
    logger.debug("{}: Executes search for collecting terms {}", Thread.currentThread().getName(),
      shardRequest.shardId());

    TermsCollector termsCollector = this.getTermsCollector(request.termsEncoding(), indexFieldData, context);
    if (request.expectedTerms() != null) termsCollector.setExpectedTerms(request.expectedTerms());
    if (request.maxTermsPerShard() != null) termsCollector.setMaxTerms(request.maxTermsPerShard());
    HitStream hitStream = orderByOperation.getHitStream(context);
    TermsSet terms = termsCollector.collect(hitStream);

    logger.debug("{}: Returns terms response with {} terms for shard {}", Thread.currentThread().getName(),
      terms.size(), shardRequest.shardId());

    return new TermsByQueryShardResponse(shardRequest.shardId(), terms);
  }
  catch (Throwable e) {
    logger.error("[termsByQuery] Error executing shard operation", e);
    throw new QueryPhaseExecutionException(context, "[termsByQuery] Failed to execute query", e);
  }
  finally {
    // this will also release the index searcher
    context.close();
    SearchContext.removeCurrent();
  }
}
 
Example #13
Source File: ResultUtils.java    From vind with Apache License 2.0 4 votes vote down vote up
private static Pair<String, List<PivotFacetResult>> getPivotFacetResults(Aggregation aggregation, Facet.PivotFacet pivotFacet, Map<String, Facet> vindFacets) {

        final FieldDescriptor field = pivotFacet.getFieldDescriptors().get(0);

        if( Objects.nonNull(aggregation) ){
            final ParsedTerms rootPivot = (ParsedTerms) aggregation;
            final List<PivotFacetResult> pivotFacetResult = rootPivot.getBuckets().stream()
                    .map(bucket -> {
                        final Map<String, Aggregation> aggMap = bucket.getAggregations().asMap();
                        final Aggregation pivotAgg = aggMap.get(pivotFacet.getFacetName());
                        final Map<String, RangeFacetResult<?>> rangeSubfacets = new HashMap<>();
                        final Map<String, QueryFacetResult<?>> querySubfacets = new HashMap<>();
                        final Map<String, StatsFacetResult<?>> statsSubfacets = new HashMap<>();

                        aggMap.values().forEach(agg -> {
                            if (ParsedExtendedStats.class.isAssignableFrom(agg.getClass())) {
                                final HashMap<String, Aggregation> statsMap = new HashMap<>();
                                statsMap.put(agg.getName(), agg);
                                statsSubfacets.put(
                                        agg.getName(),
                                        ResultUtils.getStatsFacetResults(statsMap, (Facet.StatsFacet) vindFacets.get(agg.getName())).getValue());
                            }
                            if (ParsedQuery.class.isAssignableFrom(agg.getClass())) {

                                querySubfacets.put(
                                        agg.getName(),
                                        ResultUtils.getQueryFacetResults(agg, (Facet.QueryFacet) vindFacets.get(agg.getName())).getValue());
                            }
                            if (ParsedRange.class.isAssignableFrom(agg.getClass())) {
                                rangeSubfacets.put(
                                        agg.getName(),
                                        ResultUtils.getRangeFacetResults(agg, vindFacets.get(agg.getName())).getValue());
                            }
                        });

                        final List<PivotFacetResult> subPivot = getPivotFacetResults(pivotAgg, pivotFacet, vindFacets).getValue();
                        return new PivotFacetResult(
                                subPivot,
                                bucket.getKey(),
                                field,
                                Long.valueOf(bucket.getDocCount()).intValue(),
                                rangeSubfacets,
                                querySubfacets,
                                statsSubfacets);
                    })
                    .collect(Collectors.toList());

            return Pair.of(pivotFacet.getFacetName(), pivotFacetResult);
        }
        return Pair.of(null, null);
    }
 
Example #14
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedQuery() {
    return this.originalQuery;
}
 
Example #15
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public ParsedQuery filteredQuery() {
    return filteredQuery;
}
 
Example #16
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    this.originalQuery = query;
    this.query = query.query();
    return this;
}
 
Example #17
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedPostFilter() {
    return this.postFilter;
}
 
Example #18
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    this.postFilter = postFilter;
    return this;
}
 
Example #19
Source File: TransportTermsByQueryAction.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Returns the {@link ParsedQuery} associated to this order by operation.
 */
protected ParsedQuery getParsedQuery(final XContentParser queryParser, final IndexService indexService) {
  return indexService.queryParserService().parse(queryParser);
}
 
Example #20
Source File: SubSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    throw new UnsupportedOperationException("Not supported");
}
 
Example #21
Source File: FilteredSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    return in.parsedQuery(query);
}
 
Example #22
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    this.parsedQuery = query;
    this.query = query.query();
    return this;
}
 
Example #23
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedQuery() {
    return parsedQuery;
}
 
Example #24
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    throw new UnsupportedOperationException();
}
 
Example #25
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedPostFilter() {
    return null;
}
 
Example #26
Source File: FilterParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    ParsedQuery filter = context.queryParserService().parseInnerFilter(parser);

    return new FilterAggregator.Factory(aggregationName, filter == null ? new MatchAllDocsQuery() : filter.query());
}
 
Example #27
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedQuery() {
    return query;
}
 
Example #28
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public NestedInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, ObjectMapper parentObjectMapper, ObjectMapper childObjectMapper) {
    super(context, query, childInnerHits);
    this.parentObjectMapper = parentObjectMapper;
    this.childObjectMapper = childObjectMapper;
}
 
Example #29
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public ParentChildInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, MapperService mapperService, DocumentMapper documentMapper) {
    super(context, query, childInnerHits);
    this.mapperService = mapperService;
    this.documentMapper = documentMapper;
}
 
Example #30
Source File: InnerHitsParseElement.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private ParseResult(SubSearchContext context, ParsedQuery query, Map<String, InnerHitsContext.BaseInnerHits> childInnerHits) {
    this.context = context;
    this.query = query;
    this.childInnerHits = childInnerHits;
}