Java 类org.elasticsearch.index.query.ParsedQuery 实例源码

项目:elasticsearch_my    文件:QueryPhaseTests.java   
private void countTestCase(Query query, IndexReader reader, boolean shouldCollect) throws Exception {
    TestSearchContext context = new TestSearchContext(null);
    context.parsedQuery(new ParsedQuery(query));
    context.setSize(0);
    context.setTask(new SearchTask(123L, "", "", "", null));

    IndexSearcher searcher = new IndexSearcher(reader);
    final AtomicBoolean collected = new AtomicBoolean();
    IndexSearcher contextSearcher = new IndexSearcher(reader) {
        protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
            collected.set(true);
            super.search(leaves, weight, collector);
        }
    };

    final boolean rescore = QueryPhase.execute(context, contextSearcher);
    assertFalse(rescore);
    assertEquals(searcher.count(query), context.queryResult().topDocs().totalHits);
    assertEquals(shouldCollect, collected.get());
}
项目:elasticsearch_my    文件:QueryPhaseTests.java   
public void testPostFilterDisablesCountOptimization() throws Exception {
    TestSearchContext context = new TestSearchContext(null);
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    context.setSize(0);
    context.setTask(new SearchTask(123L, "", "", "", null));

    final AtomicBoolean collected = new AtomicBoolean();
    IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) {
        protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
            collected.set(true);
            super.search(leaves, weight, collector);
        }
    };

    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertFalse(collected.get());

    context.parsedPostFilter(new ParsedQuery(new MatchNoDocsQuery()));
    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertTrue(collected.get());
}
项目:elasticsearch_my    文件:QueryPhaseTests.java   
public void testMinScoreDisablesCountOptimization() throws Exception {
    TestSearchContext context = new TestSearchContext(null);
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    context.setSize(0);
    context.setTask(new SearchTask(123L, "", "", "", null));

    final AtomicBoolean collected = new AtomicBoolean();
    IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) {
        protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
            collected.set(true);
            super.search(leaves, weight, collector);
        }
    };

    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertFalse(collected.get());

    context.minimumScore(1);
    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertTrue(collected.get());
}
项目:Elasticsearch    文件:SearchContextFactory.java   
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;
}
项目:Elasticsearch    文件:QueryCollector.java   
@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);
    }
}
项目:Elasticsearch    文件:QueryCollector.java   
@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);
    }
}
项目:Elasticsearch    文件:PostFilterParseElement.java   
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    ParsedQuery postFilter = context.queryParserService().parseInnerFilter(parser);
    if (postFilter != null) {
        context.parsedPostFilter(postFilter);
    }
}
项目:Elasticsearch    文件:FilterBinaryParseElement.java   
@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);
        }
    }
}
项目:Elasticsearch    文件:InnerHitsContext.java   
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;
    }
}
项目:Elasticsearch    文件:DefaultSearchContext.java   
/**
 * 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);
    }
}
项目:Elasticsearch    文件:QueryRescorer.java   
@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;
}
项目:Elasticsearch    文件:TranslogRecoveryPerformer.java   
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);
}
项目:Elasticsearch    文件:IndexAliasesService.java   
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);
    }
}
项目:elasticsearch_my    文件:TestSearchContext.java   
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    this.postFilter = postFilter;
    return this;
}
项目:elasticsearch_my    文件:TestSearchContext.java   
@Override
public ParsedQuery parsedPostFilter() {
    return postFilter;
}
项目:elasticsearch_my    文件:TestSearchContext.java   
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    this.originalQuery = query;
    this.query = query.query();
    return this;
}
项目:elasticsearch_my    文件:TestSearchContext.java   
@Override
public ParsedQuery parsedQuery() {
    return originalQuery;
}
项目:elasticsearch_my    文件:FilteredSearchContext.java   
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    return in.parsedPostFilter(postFilter);
}
项目:elasticsearch_my    文件:FilteredSearchContext.java   
@Override
public ParsedQuery parsedPostFilter() {
    return in.parsedPostFilter();
}
项目:elasticsearch_my    文件:FilteredSearchContext.java   
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    return in.parsedQuery(query);
}
项目:elasticsearch_my    文件:FilteredSearchContext.java   
@Override
public ParsedQuery parsedQuery() {
    return in.parsedQuery();
}
项目:elasticsearch_my    文件:SubSearchContext.java   
@Override
public ParsedQuery parsedQuery() {
    return parsedQuery;
}
项目:elasticsearch_my    文件:SubSearchContext.java   
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    throw new UnsupportedOperationException("Not supported");
}
项目:elasticsearch_my    文件:DefaultSearchContext.java   
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    this.postFilter = postFilter;
    return this;
}
项目:elasticsearch_my    文件:DefaultSearchContext.java   
@Override
public ParsedQuery parsedPostFilter() {
    return this.postFilter;
}
项目:elasticsearch_my    文件:DefaultSearchContext.java   
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    this.originalQuery = query;
    this.query = query.query();
    return this;
}
项目:elasticsearch_my    文件:DefaultSearchContext.java   
@Override
public ParsedQuery parsedQuery() {
    return this.originalQuery;
}
项目:Elasticsearch    文件:PercolateContext.java   
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    this.parsedQuery = query;
    this.query = query.query();
    return this;
}
项目:Elasticsearch    文件:PercolateContext.java   
@Override
public ParsedQuery parsedQuery() {
    return parsedQuery;
}
项目:Elasticsearch    文件:PercolateContext.java   
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    throw new UnsupportedOperationException();
}
项目:Elasticsearch    文件:PercolateContext.java   
@Override
public ParsedQuery parsedPostFilter() {
    return null;
}
项目:Elasticsearch    文件:FilterParser.java   
@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());
}
项目:Elasticsearch    文件:InnerHitsContext.java   
@Override
public ParsedQuery parsedQuery() {
    return query;
}
项目:Elasticsearch    文件:InnerHitsContext.java   
public NestedInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, ObjectMapper parentObjectMapper, ObjectMapper childObjectMapper) {
    super(context, query, childInnerHits);
    this.parentObjectMapper = parentObjectMapper;
    this.childObjectMapper = childObjectMapper;
}
项目:Elasticsearch    文件:InnerHitsContext.java   
public ParentChildInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, MapperService mapperService, DocumentMapper documentMapper) {
    super(context, query, childInnerHits);
    this.mapperService = mapperService;
    this.documentMapper = documentMapper;
}
项目:Elasticsearch    文件:InnerHitsParseElement.java   
private ParseResult(SubSearchContext context, ParsedQuery query, Map<String, InnerHitsContext.BaseInnerHits> childInnerHits) {
    this.context = context;
    this.query = query;
    this.childInnerHits = childInnerHits;
}
项目:Elasticsearch    文件:InnerHitsParseElement.java   
public ParsedQuery query() {
    return query;
}
项目:Elasticsearch    文件:FilteredSearchContext.java   
@Override
public SearchContext parsedPostFilter(ParsedQuery postFilter) {
    return in.parsedPostFilter(postFilter);
}
项目:Elasticsearch    文件:FilteredSearchContext.java   
@Override
public ParsedQuery parsedPostFilter() {
    return in.parsedPostFilter();
}
项目:Elasticsearch    文件:FilteredSearchContext.java   
@Override
public SearchContext parsedQuery(ParsedQuery query) {
    return in.parsedQuery(query);
}