Java Code Examples for org.elasticsearch.index.query.ParsedQuery

The following examples show how to use org.elasticsearch.index.query.ParsedQuery. These examples are extracted from open source projects. 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 Project: Elasticsearch   Source File: SearchContextFactory.java    License: 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 Project: Elasticsearch   Source File: QueryCollector.java    License: 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 3
Source Project: Elasticsearch   Source File: QueryCollector.java    License: 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 4
Source Project: Elasticsearch   Source File: PostFilterParseElement.java    License: 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 5
Source Project: Elasticsearch   Source File: FilterBinaryParseElement.java    License: 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 Project: Elasticsearch   Source File: InnerHitsContext.java    License: 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 7
Source Project: Elasticsearch   Source File: DefaultSearchContext.java    License: 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 8
Source Project: Elasticsearch   Source File: QueryRescorer.java    License: 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 9
Source Project: Elasticsearch   Source File: TranslogRecoveryPerformer.java    License: 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 Project: Elasticsearch   Source File: IndexAliasesService.java    License: 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 Project: vind   Source File: ResultUtils.java    License: 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 12
Source Project: Elasticsearch   Source File: PercolateContext.java    License: 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 13
Source Project: Elasticsearch   Source File: PercolateContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedQuery() {
    return parsedQuery;
}
 
Example 14
Source Project: Elasticsearch   Source File: PercolateContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    throw new UnsupportedOperationException();
}
 
Example 15
Source Project: Elasticsearch   Source File: PercolateContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedPostFilter() {
    return null;
}
 
Example 16
Source Project: Elasticsearch   Source File: FilterParser.java    License: 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 17
Source Project: Elasticsearch   Source File: InnerHitsContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedQuery() {
    return query;
}
 
Example 18
Source Project: Elasticsearch   Source File: InnerHitsContext.java    License: 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 19
Source Project: Elasticsearch   Source File: InnerHitsContext.java    License: 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 20
Source Project: Elasticsearch   Source File: InnerHitsParseElement.java    License: 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;
}
 
Example 21
Source Project: Elasticsearch   Source File: InnerHitsParseElement.java    License: Apache License 2.0 4 votes vote down vote up
public ParsedQuery query() {
    return query;
}
 
Example 22
Source Project: Elasticsearch   Source File: FilteredSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    return in.parsedPostFilter(postFilter);
}
 
Example 23
Source Project: Elasticsearch   Source File: FilteredSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedPostFilter() {
    return in.parsedPostFilter();
}
 
Example 24
Source Project: Elasticsearch   Source File: FilteredSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    return in.parsedQuery(query);
}
 
Example 25
Source Project: Elasticsearch   Source File: FilteredSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedQuery() {
    return in.parsedQuery();
}
 
Example 26
Source Project: Elasticsearch   Source File: SubSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    throw new UnsupportedOperationException("Not supported");
}
 
Example 27
Source Project: Elasticsearch   Source File: DefaultSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    this.postFilter = postFilter;
    return this;
}
 
Example 28
Source Project: Elasticsearch   Source File: DefaultSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ParsedQuery parsedPostFilter() {
    return this.postFilter;
}
 
Example 29
Source Project: Elasticsearch   Source File: DefaultSearchContext.java    License: 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 30
Source Project: Elasticsearch   Source File: DefaultSearchContext.java    License: Apache License 2.0 4 votes vote down vote up
public ParsedQuery filteredQuery() {
    return filteredQuery;
}