Java Code Examples for org.elasticsearch.common.lucene.search.Queries#filtered()

The following examples show how to use org.elasticsearch.common.lucene.search.Queries#filtered() . 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: PercolatorService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void queryBasedPercolating(Engine.Searcher percolatorSearcher, PercolateContext context, QueryCollector percolateCollector) throws IOException {
    Query percolatorTypeFilter = context.indexService().mapperService().documentMapper(TYPE_NAME).typeFilter();

    final Query filter;
    if (context.aliasFilter() != null) {
        BooleanQuery.Builder booleanFilter = new BooleanQuery.Builder();
        booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST);
        booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST);
        filter = booleanFilter.build();
    } else {
        filter = percolatorTypeFilter;
    }

    Query query = Queries.filtered(context.percolateQuery(), filter);
    percolatorSearcher.searcher().search(query, percolateCollector);
    percolateCollector.aggregatorCollector.postCollection();
    if (context.aggregations() != null) {
        aggregationPhase.execute(context);
    }
}
 
Example 2
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
    Query rawParentFilter;
    if (parentObjectMapper == null) {
        rawParentFilter = Queries.newNonNestedFilter();
    } else {
        rawParentFilter = parentObjectMapper.nestedTypeFilter();
    }
    BitSetProducer parentFilter = context.bitsetFilterCache().getBitSetProducer(rawParentFilter);
    Query childFilter = childObjectMapper.nestedTypeFilter();
    Query q = Queries.filtered(query.query(), new NestedChildrenQuery(parentFilter, childFilter, hitContext));

    if (size() == 0) {
        return new TopDocs(context.searcher().count(q), Lucene.EMPTY_SCORE_DOCS, 0);
    } else {
        int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
        TopDocsCollector topDocsCollector;
        if (sort() != null) {
            try {
                topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
            } catch (IOException e) {
                throw ExceptionsHelper.convertToElastic(e);
            }
        } else {
            topDocsCollector = TopScoreDocCollector.create(topN);
        }
        try {
            context.searcher().search(q, topDocsCollector);
        } finally {
            clearReleasables(Lifetime.COLLECTION);
        }
        return topDocsCollector.topDocs(from(), size());
    }
}
 
Example 3
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 4
Source File: ScanContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Wrap the query so that it can skip directly to the right document.
 */
public Query wrapQuery(Query query) {
    return Queries.filtered(query, new MinDocQuery(docUpTo));
}
 
Example 5
Source File: FilteredQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    deprecationLogger.deprecated("The [filtered] query is deprecated, please use a [bool] query instead with a [must] "
            + "clause for the query part and a [filter] clause for the filter part.");

    XContentParser parser = parseContext.parser();

    Query query = Queries.newMatchAllQuery();
    Query filter = null;
    boolean filterFound = false;
    float boost = 1.0f;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("query".equals(currentFieldName)) {
                query = parseContext.parseInnerQuery();
            } else if ("filter".equals(currentFieldName)) {
                filterFound = true;
                filter = parseContext.parseInnerFilter();
            } else {
                throw new QueryParsingException(parseContext, "[filtered] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if ("strategy".equals(currentFieldName)) {
                // ignore
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new QueryParsingException(parseContext, "[filtered] query does not support [" + currentFieldName + "]");
            }
        }
    }

    // parsed internally, but returned null during parsing...
    if (query == null) {
        return null;
    }

    BooleanQuery filteredQuery = Queries.filtered(query, filter);
    filteredQuery.setBoost(boost);
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, filteredQuery);
    }
    return filteredQuery;
}