Search in sources :

Example 6 with ParsedQuery

use of org.elasticsearch.index.query.ParsedQuery in project elasticsearch by elastic.

the class QueryPhaseTests method testMinScoreDisablesCountOptimization.

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());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSearchContext(org.elasticsearch.test.TestSearchContext) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) SearchTask(org.elasticsearch.action.search.SearchTask) MultiReader(org.apache.lucene.index.MultiReader) Collector(org.apache.lucene.search.Collector) List(java.util.List) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Weight(org.apache.lucene.search.Weight)

Example 7 with ParsedQuery

use of org.elasticsearch.index.query.ParsedQuery in project elasticsearch by elastic.

the class PercolatorHighlightSubFetchPhase method hitsExecute.

@Override
public void hitsExecute(SearchContext context, SearchHit[] hits) {
    if (hitsExecutionNeeded(context) == false) {
        return;
    }
    PercolateQuery percolateQuery = locatePercolatorQuery(context.query());
    if (percolateQuery == null) {
        // shouldn't happen as we checked for the existence of a percolator query in hitsExecutionNeeded(...)
        throw new IllegalStateException("couldn't locate percolator query");
    }
    List<LeafReaderContext> ctxs = context.searcher().getIndexReader().leaves();
    IndexSearcher percolatorIndexSearcher = percolateQuery.getPercolatorIndexSearcher();
    PercolateQuery.QueryStore queryStore = percolateQuery.getQueryStore();
    LeafReaderContext percolatorLeafReaderContext = percolatorIndexSearcher.getIndexReader().leaves().get(0);
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    SubSearchContext subSearchContext = createSubSearchContext(context, percolatorLeafReaderContext, percolateQuery.getDocumentSource());
    for (SearchHit hit : hits) {
        final Query query;
        try {
            LeafReaderContext ctx = ctxs.get(ReaderUtil.subIndex(hit.docId(), ctxs));
            int segmentDocId = hit.docId() - ctx.docBase;
            query = queryStore.getQueries(ctx).apply(segmentDocId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (query != null) {
            subSearchContext.parsedQuery(new ParsedQuery(query));
            hitContext.reset(new SearchHit(0, "unknown", new Text(percolateQuery.getDocumentType()), Collections.emptyMap()), percolatorLeafReaderContext, 0, percolatorIndexSearcher);
            hitContext.cache().clear();
            super.hitExecute(subSearchContext, hitContext);
            hit.getHighlightFields().putAll(hitContext.hit().getHighlightFields());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SearchHit(org.elasticsearch.search.SearchHit) Query(org.apache.lucene.search.Query) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) Text(org.elasticsearch.common.text.Text) IOException(java.io.IOException) SubSearchContext(org.elasticsearch.search.internal.SubSearchContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase)

Aggregations

ParsedQuery (org.elasticsearch.index.query.ParsedQuery)7 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 IOException (java.io.IOException)3 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Collector (org.apache.lucene.search.Collector)3 Weight (org.apache.lucene.search.Weight)3 SearchTask (org.elasticsearch.action.search.SearchTask)3 TestSearchContext (org.elasticsearch.test.TestSearchContext)3 MultiReader (org.apache.lucene.index.MultiReader)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)2 Text (org.elasticsearch.common.text.Text)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 IndexReader (org.apache.lucene.index.IndexReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1