Search in sources :

Example 41 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project elasticsearch by elastic.

the class CustomFieldQuery method flatten.

@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
        flatten(sourceQuery, reader, flatQueries, boost);
    } else if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ESToParentBlockJoinQuery) {
        ESToParentBlockJoinQuery blockJoinQuery = (ESToParentBlockJoinQuery) sourceQuery;
        flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof BoostingQuery) {
        BoostingQuery boostingQuery = (BoostingQuery) sourceQuery;
        //flatten positive query with query boost
        flatten(boostingQuery.getMatch(), reader, flatQueries, boost);
        //flatten negative query with negative boost
        flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost());
    } else if (sourceQuery instanceof SynonymQuery) {
        // SynonymQuery should be handled by the parent class directly.
        // This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)

Example 42 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project elasticsearch by elastic.

the class GeoShapeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) {
    if (shape == null) {
        throw new UnsupportedOperationException("query must be rewritten first");
    }
    final ShapeBuilder shapeToQuery = shape;
    final MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_shape field [" + fieldName + "]");
        }
    }
    // TODO: This isn't the nicest way to check this
    if (!(fieldType instanceof GeoShapeFieldMapper.GeoShapeFieldType)) {
        throw new QueryShardException(context, "Field [" + fieldName + "] is not a geo_shape");
    }
    final GeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
    PrefixTreeStrategy strategy = shapeFieldType.defaultStrategy();
    if (this.strategy != null) {
        strategy = shapeFieldType.resolveStrategy(this.strategy);
    }
    Query query;
    if (strategy instanceof RecursivePrefixTreeStrategy && relation == ShapeRelation.DISJOINT) {
        // this strategy doesn't support disjoint anymore: but it did
        // before, including creating lucene fieldcache (!)
        // in this case, execute disjoint as exists && !intersects
        BooleanQuery.Builder bool = new BooleanQuery.Builder();
        Query exists = ExistsQueryBuilder.newFilter(context, fieldName);
        Query intersects = strategy.makeQuery(getArgs(shapeToQuery, ShapeRelation.INTERSECTS));
        bool.add(exists, BooleanClause.Occur.MUST);
        bool.add(intersects, BooleanClause.Occur.MUST_NOT);
        query = new ConstantScoreQuery(bool.build());
    } else {
        query = new ConstantScoreQuery(strategy.makeQuery(getArgs(shapeToQuery, relation)));
    }
    return query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeFieldMapper(org.elasticsearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)

Example 43 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project elasticsearch by elastic.

the class QueryPhase method execute.

/**
     * In a package-private method so that it can be tested without having to
     * wire everything (mapperService, etc.)
     * @return whether the rescoring phase should be executed
     */
static boolean execute(SearchContext searchContext, final IndexSearcher searcher) throws QueryPhaseExecutionException {
    QuerySearchResult queryResult = searchContext.queryResult();
    queryResult.searchTimedOut(false);
    final boolean doProfile = searchContext.getProfilers() != null;
    final SearchType searchType = searchContext.searchType();
    boolean rescore = false;
    try {
        queryResult.from(searchContext.from());
        queryResult.size(searchContext.size());
        Query query = searchContext.query();
        final int totalNumDocs = searcher.getIndexReader().numDocs();
        int numDocs = Math.min(searchContext.from() + searchContext.size(), totalNumDocs);
        Collector collector;
        Callable<TopDocs> topDocsCallable;
        DocValueFormat[] sortValueFormats = new DocValueFormat[0];
        // already rewritten
        assert query == searcher.rewrite(query);
        if (searchContext.size() == 0) {
            // no matter what the value of from is
            final TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
            collector = totalHitCountCollector;
            if (searchContext.getProfilers() != null) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_COUNT, Collections.emptyList());
            }
            topDocsCallable = new Callable<TopDocs>() {

                @Override
                public TopDocs call() throws Exception {
                    return new TopDocs(totalHitCountCollector.getTotalHits(), Lucene.EMPTY_SCORE_DOCS, 0);
                }
            };
        } else {
            // Perhaps have a dedicated scroll phase?
            final ScrollContext scrollContext = searchContext.scrollContext();
            assert (scrollContext != null) == (searchContext.request().scroll() != null);
            final Collector topDocsCollector;
            ScoreDoc after = null;
            if (searchContext.request().scroll() != null) {
                numDocs = Math.min(searchContext.size(), totalNumDocs);
                after = scrollContext.lastEmittedDoc;
                if (returnsDocsInOrder(query, searchContext.sort())) {
                    if (scrollContext.totalHits == -1) {
                        // first round
                        assert scrollContext.lastEmittedDoc == null;
                    // there is not much that we can optimize here since we want to collect all
                    // documents in order to get the total number of hits
                    } else {
                        // skip to the desired doc and stop collecting after ${size} matches
                        if (scrollContext.lastEmittedDoc != null) {
                            BooleanQuery bq = new BooleanQuery.Builder().add(query, BooleanClause.Occur.MUST).add(new MinDocQuery(after.doc + 1), BooleanClause.Occur.FILTER).build();
                            query = bq;
                        }
                        searchContext.terminateAfter(numDocs);
                    }
                }
            } else {
                after = searchContext.searchAfter();
            }
            if (totalNumDocs == 0) {
                // top collectors don't like a size of 0
                numDocs = 1;
            }
            assert numDocs > 0;
            if (searchContext.collapse() == null) {
                if (searchContext.sort() != null) {
                    SortAndFormats sf = searchContext.sort();
                    topDocsCollector = TopFieldCollector.create(sf.sort, numDocs, (FieldDoc) after, true, searchContext.trackScores(), searchContext.trackScores());
                    sortValueFormats = sf.formats;
                } else {
                    rescore = !searchContext.rescore().isEmpty();
                    for (RescoreSearchContext rescoreContext : searchContext.rescore()) {
                        numDocs = Math.max(rescoreContext.window(), numDocs);
                    }
                    topDocsCollector = TopScoreDocCollector.create(numDocs, after);
                }
            } else {
                Sort sort = Sort.RELEVANCE;
                if (searchContext.sort() != null) {
                    sort = searchContext.sort().sort;
                }
                CollapseContext collapse = searchContext.collapse();
                topDocsCollector = collapse.createTopDocs(sort, numDocs, searchContext.trackScores());
                if (searchContext.sort() == null) {
                    sortValueFormats = new DocValueFormat[] { DocValueFormat.RAW };
                } else {
                    sortValueFormats = searchContext.sort().formats;
                }
            }
            collector = topDocsCollector;
            if (doProfile) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_TOP_HITS, Collections.emptyList());
            }
            topDocsCallable = new Callable<TopDocs>() {

                @Override
                public TopDocs call() throws Exception {
                    final TopDocs topDocs;
                    if (topDocsCollector instanceof TopDocsCollector) {
                        topDocs = ((TopDocsCollector<?>) topDocsCollector).topDocs();
                    } else if (topDocsCollector instanceof CollapsingTopDocsCollector) {
                        topDocs = ((CollapsingTopDocsCollector) topDocsCollector).getTopDocs();
                    } else {
                        throw new IllegalStateException("Unknown top docs collector " + topDocsCollector.getClass().getName());
                    }
                    if (scrollContext != null) {
                        if (scrollContext.totalHits == -1) {
                            // first round
                            scrollContext.totalHits = topDocs.totalHits;
                            scrollContext.maxScore = topDocs.getMaxScore();
                        } else {
                            // subsequent round: the total number of hits and
                            // the maximum score were computed on the first round
                            topDocs.totalHits = scrollContext.totalHits;
                            topDocs.setMaxScore(scrollContext.maxScore);
                        }
                        if (searchContext.request().numberOfShards() == 1) {
                            // if we fetch the document in the same roundtrip, we already know the last emitted doc
                            if (topDocs.scoreDocs.length > 0) {
                                // set the last emitted doc
                                scrollContext.lastEmittedDoc = topDocs.scoreDocs[topDocs.scoreDocs.length - 1];
                            }
                        }
                    }
                    return topDocs;
                }
            };
        }
        final boolean terminateAfterSet = searchContext.terminateAfter() != SearchContext.DEFAULT_TERMINATE_AFTER;
        if (terminateAfterSet) {
            final Collector child = collector;
            // throws Lucene.EarlyTerminationException when given count is reached
            collector = Lucene.wrapCountBasedEarlyTerminatingCollector(collector, searchContext.terminateAfter());
            if (doProfile) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_TERMINATE_AFTER_COUNT, Collections.singletonList((InternalProfileCollector) child));
            }
        }
        if (searchContext.parsedPostFilter() != null) {
            final Collector child = collector;
            // this will only get applied to the actual search collector and not
            // to any scoped collectors, also, it will only be applied to the main collector
            // since that is where the filter should only work
            final Weight filterWeight = searcher.createNormalizedWeight(searchContext.parsedPostFilter().query(), false);
            collector = new FilteredCollector(collector, filterWeight);
            if (doProfile) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_POST_FILTER, Collections.singletonList((InternalProfileCollector) child));
            }
        }
        // plug in additional collectors, like aggregations
        final List<Collector> subCollectors = new ArrayList<>();
        subCollectors.add(collector);
        subCollectors.addAll(searchContext.queryCollectors().values());
        collector = MultiCollector.wrap(subCollectors);
        if (doProfile && collector instanceof InternalProfileCollector == false) {
            // When there is a single collector to wrap, MultiCollector returns it
            // directly, so only wrap in the case that there are several sub collectors
            final List<InternalProfileCollector> children = new AbstractList<InternalProfileCollector>() {

                @Override
                public InternalProfileCollector get(int index) {
                    return (InternalProfileCollector) subCollectors.get(index);
                }

                @Override
                public int size() {
                    return subCollectors.size();
                }
            };
            collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_MULTI, children);
        }
        // apply the minimum score after multi collector so we filter aggs as well
        if (searchContext.minimumScore() != null) {
            final Collector child = collector;
            collector = new MinimumScoreCollector(collector, searchContext.minimumScore());
            if (doProfile) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_MIN_SCORE, Collections.singletonList((InternalProfileCollector) child));
            }
        }
        if (collector.getClass() == TotalHitCountCollector.class) {
            // instead of using a collector
            while (true) {
                // a constant_score query
                if (query instanceof ConstantScoreQuery) {
                    query = ((ConstantScoreQuery) query).getQuery();
                } else {
                    break;
                }
            }
            if (query.getClass() == MatchAllDocsQuery.class) {
                collector = null;
                topDocsCallable = new Callable<TopDocs>() {

                    @Override
                    public TopDocs call() throws Exception {
                        int count = searcher.getIndexReader().numDocs();
                        return new TopDocs(count, Lucene.EMPTY_SCORE_DOCS, 0);
                    }
                };
            } else if (query.getClass() == TermQuery.class && searcher.getIndexReader().hasDeletions() == false) {
                final Term term = ((TermQuery) query).getTerm();
                collector = null;
                topDocsCallable = new Callable<TopDocs>() {

                    @Override
                    public TopDocs call() throws Exception {
                        int count = 0;
                        for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
                            count += context.reader().docFreq(term);
                        }
                        return new TopDocs(count, Lucene.EMPTY_SCORE_DOCS, 0);
                    }
                };
            }
        }
        final boolean timeoutSet = searchContext.timeout() != null && !searchContext.timeout().equals(SearchService.NO_TIMEOUT);
        if (timeoutSet && collector != null) {
            // collector might be null if no collection is actually needed
            final Collector child = collector;
            // TODO: change to use our own counter that uses the scheduler in ThreadPool
            // throws TimeLimitingCollector.TimeExceededException when timeout has reached
            collector = Lucene.wrapTimeLimitingCollector(collector, searchContext.timeEstimateCounter(), searchContext.timeout().millis());
            if (doProfile) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_TIMEOUT, Collections.singletonList((InternalProfileCollector) child));
            }
        }
        if (collector != null) {
            final Collector child = collector;
            collector = new CancellableCollector(searchContext.getTask()::isCancelled, searchContext.lowLevelCancellation(), collector);
            if (doProfile) {
                collector = new InternalProfileCollector(collector, CollectorResult.REASON_SEARCH_CANCELLED, Collections.singletonList((InternalProfileCollector) child));
            }
        }
        try {
            if (collector != null) {
                if (doProfile) {
                    searchContext.getProfilers().getCurrentQueryProfiler().setCollector((InternalProfileCollector) collector);
                }
                searcher.search(query, collector);
            }
        } catch (TimeLimitingCollector.TimeExceededException e) {
            assert timeoutSet : "TimeExceededException thrown even though timeout wasn't set";
            queryResult.searchTimedOut(true);
        } catch (Lucene.EarlyTerminationException e) {
            assert terminateAfterSet : "EarlyTerminationException thrown even though terminateAfter wasn't set";
            queryResult.terminatedEarly(true);
        } finally {
            searchContext.clearReleasables(SearchContext.Lifetime.COLLECTION);
        }
        if (terminateAfterSet && queryResult.terminatedEarly() == null) {
            queryResult.terminatedEarly(false);
        }
        queryResult.topDocs(topDocsCallable.call(), sortValueFormats);
        if (searchContext.getProfilers() != null) {
            ProfileShardResult shardResults = SearchProfileShardResults.buildShardResults(searchContext.getProfilers());
            searchContext.queryResult().profileResults(shardResults);
        }
        return rescore;
    } catch (Exception e) {
        throw new QueryPhaseExecutionException(searchContext, "Failed to execute main query", e);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MinDocQuery(org.apache.lucene.queries.MinDocQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) FieldDoc(org.apache.lucene.search.FieldDoc) RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) ArrayList(java.util.ArrayList) TimeLimitingCollector(org.apache.lucene.search.TimeLimitingCollector) Lucene(org.elasticsearch.common.lucene.Lucene) Callable(java.util.concurrent.Callable) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) MinDocQuery(org.apache.lucene.queries.MinDocQuery) FilteredCollector(org.elasticsearch.common.lucene.search.FilteredCollector) MinimumScoreCollector(org.elasticsearch.common.lucene.MinimumScoreCollector) TimeLimitingCollector(org.apache.lucene.search.TimeLimitingCollector) FilteredCollector(org.elasticsearch.common.lucene.search.FilteredCollector) MultiCollector(org.apache.lucene.search.MultiCollector) InternalProfileCollector(org.elasticsearch.search.profile.query.InternalProfileCollector) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) Collector(org.apache.lucene.search.Collector) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) MinimumScoreCollector(org.elasticsearch.common.lucene.MinimumScoreCollector) TopFieldCollector(org.apache.lucene.search.TopFieldCollector) TopDocsCollector(org.apache.lucene.search.TopDocsCollector) CollapsingTopDocsCollector(org.apache.lucene.search.grouping.CollapsingTopDocsCollector) Sort(org.apache.lucene.search.Sort) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) SearchType(org.elasticsearch.action.search.SearchType) TopDocsCollector(org.apache.lucene.search.TopDocsCollector) CollapsingTopDocsCollector(org.apache.lucene.search.grouping.CollapsingTopDocsCollector) ProfileShardResult(org.elasticsearch.search.profile.ProfileShardResult) AbstractList(java.util.AbstractList) DocValueFormat(org.elasticsearch.search.DocValueFormat) ScrollContext(org.elasticsearch.search.internal.ScrollContext) Term(org.apache.lucene.index.Term) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats) Weight(org.apache.lucene.search.Weight) CollapsingTopDocsCollector(org.apache.lucene.search.grouping.CollapsingTopDocsCollector) InternalProfileCollector(org.elasticsearch.search.profile.query.InternalProfileCollector) CollapseContext(org.elasticsearch.search.collapse.CollapseContext)

Example 44 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project elasticsearch by elastic.

the class HasChildQueryBuilderTests method assertLateParsingQuery.

static void assertLateParsingQuery(Query query, String type, String id) throws IOException {
    assertThat(query, instanceOf(HasChildQueryBuilder.LateParsingQuery.class));
    HasChildQueryBuilder.LateParsingQuery lateParsingQuery = (HasChildQueryBuilder.LateParsingQuery) query;
    assertThat(lateParsingQuery.getInnerQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) lateParsingQuery.getInnerQuery();
    assertThat(booleanQuery.clauses().size(), equalTo(2));
    //check the inner ids query, we have to call rewrite to get to check the type it's executed against
    assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class));
    TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery();
    Query rewrittenTermsQuery = termsQuery.rewrite(null);
    assertThat(rewrittenTermsQuery, instanceOf(ConstantScoreQuery.class));
    ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) rewrittenTermsQuery;
    assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery booleanTermsQuery = (BooleanQuery) constantScoreQuery.getQuery();
    assertThat(booleanTermsQuery.clauses().toString(), booleanTermsQuery.clauses().size(), equalTo(1));
    assertThat(booleanTermsQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(booleanTermsQuery.clauses().get(0).getQuery(), instanceOf(TermQuery.class));
    TermQuery termQuery = (TermQuery) booleanTermsQuery.clauses().get(0).getQuery();
    assertThat(termQuery.getTerm().field(), equalTo(UidFieldMapper.NAME));
    //we want to make sure that the inner ids query gets executed against the child type rather than the main type we initially set to the context
    BytesRef[] ids = Uid.createUidsForTypesAndIds(Collections.singletonList(type), Collections.singletonList(id));
    assertThat(termQuery.getTerm().bytes(), equalTo(ids[0]));
    //check the type filter
    assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER));
    assertEquals(new TypeFieldMapper.TypesQuery(new BytesRef(type)), booleanQuery.clauses().get(1).getQuery());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) TypeFieldMapper(org.elasticsearch.index.mapper.TypeFieldMapper) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 45 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project elasticsearch by elastic.

the class QueryStringQueryBuilderTests method testExistsFieldQuery.

public void testExistsFieldQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryShardContext context = createShardContext();
    QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("foo:*");
    Query query = queryBuilder.toQuery(context);
    Query expected = new ConstantScoreQuery(new TermQuery(new Term("_field_names", "foo")));
    assertThat(query, equalTo(expected));
    queryBuilder = new QueryStringQueryBuilder("_all:*");
    query = queryBuilder.toQuery(context);
    expected = new ConstantScoreQuery(new TermQuery(new Term("_field_names", "_all")));
    assertThat(query, equalTo(expected));
    queryBuilder = new QueryStringQueryBuilder("*:*");
    query = queryBuilder.toQuery(context);
    expected = new MatchAllDocsQuery();
    assertThat(query, equalTo(expected));
    queryBuilder = new QueryStringQueryBuilder("*");
    query = queryBuilder.toQuery(context);
    List<Query> fieldQueries = new ArrayList<>();
    for (String type : QueryStringQueryBuilder.allQueryableDefaultFields(context).keySet()) {
        fieldQueries.add(new ConstantScoreQuery(new TermQuery(new Term("_field_names", type))));
    }
    expected = new DisjunctionMaxQuery(fieldQueries, 0f);
    assertThat(query, equalTo(expected));
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term) Matchers.containsString(org.hamcrest.Matchers.containsString) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)68 BooleanQuery (org.apache.lucene.search.BooleanQuery)43 TermQuery (org.apache.lucene.search.TermQuery)42 Query (org.apache.lucene.search.Query)40 Term (org.apache.lucene.index.Term)30 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 ArrayList (java.util.ArrayList)13 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)13 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)12 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)11 Document (org.apache.lucene.document.Document)10 Test (org.junit.Test)10 Sort (org.apache.lucene.search.Sort)9 TopDocs (org.apache.lucene.search.TopDocs)9 StringField (org.apache.lucene.document.StringField)8 BooleanClause (org.apache.lucene.search.BooleanClause)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)8 SortField (org.apache.lucene.search.SortField)8