Search in sources :

Example 1 with ObjectHashSet

use of com.carrotsearch.hppc.ObjectHashSet in project elasticsearch by elastic.

the class MultiPhrasePrefixQuery method rewrite.

@Override
public Query rewrite(IndexReader reader) throws IOException {
    Query rewritten = super.rewrite(reader);
    if (rewritten != this) {
        return rewritten;
    }
    if (termArrays.isEmpty()) {
        return new MatchNoDocsQuery();
    }
    MultiPhraseQuery.Builder query = new MultiPhraseQuery.Builder();
    query.setSlop(slop);
    int sizeMinus1 = termArrays.size() - 1;
    for (int i = 0; i < sizeMinus1; i++) {
        query.add(termArrays.get(i), positions.get(i));
    }
    Term[] suffixTerms = termArrays.get(sizeMinus1);
    int position = positions.get(sizeMinus1);
    ObjectHashSet<Term> terms = new ObjectHashSet<>();
    for (Term term : suffixTerms) {
        getPrefixTerms(terms, term, reader);
        if (terms.size() > maxExpansions) {
            break;
        }
    }
    if (terms.isEmpty()) {
        // which rewrites query with an empty reader.
        return new BooleanQuery.Builder().add(query.build(), BooleanClause.Occur.MUST).add(Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName()), BooleanClause.Occur.MUST).build();
    }
    query.add(terms.toArray(Term.class), position);
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ObjectHashSet(com.carrotsearch.hppc.ObjectHashSet) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term)

Example 2 with ObjectHashSet

use of com.carrotsearch.hppc.ObjectHashSet in project elasticsearch by elastic.

the class DfsPhase method execute.

@Override
public void execute(SearchContext context) {
    final ObjectHashSet<Term> termsSet = new ObjectHashSet<>();
    try {
        context.searcher().createNormalizedWeight(context.query(), true).extractTerms(new DelegateSet(termsSet));
        for (RescoreSearchContext rescoreContext : context.rescore()) {
            rescoreContext.rescorer().extractTerms(context, rescoreContext, new DelegateSet(termsSet));
        }
        Term[] terms = termsSet.toArray(Term.class);
        TermStatistics[] termStatistics = new TermStatistics[terms.length];
        IndexReaderContext indexReaderContext = context.searcher().getTopReaderContext();
        for (int i = 0; i < terms.length; i++) {
            if (context.isCancelled()) {
                throw new TaskCancelledException("cancelled");
            }
            // LUCENE 4 UPGRADE: cache TermContext?
            TermContext termContext = TermContext.build(indexReaderContext, terms[i]);
            termStatistics[i] = context.searcher().termStatistics(terms[i], termContext);
        }
        ObjectObjectHashMap<String, CollectionStatistics> fieldStatistics = HppcMaps.newNoNullKeysMap();
        for (Term term : terms) {
            assert term.field() != null : "field is null";
            if (!fieldStatistics.containsKey(term.field())) {
                final CollectionStatistics collectionStatistics = context.searcher().collectionStatistics(term.field());
                fieldStatistics.put(term.field(), collectionStatistics);
                if (context.isCancelled()) {
                    throw new TaskCancelledException("cancelled");
                }
            }
        }
        context.dfsResult().termsStatistics(terms, termStatistics).fieldStatistics(fieldStatistics).maxDoc(context.searcher().getIndexReader().maxDoc());
    } catch (Exception e) {
        throw new DfsPhaseExecutionException(context, "Exception during dfs phase", e);
    } finally {
        // don't hold on to terms
        termsSet.clear();
    }
}
Also used : RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) Term(org.apache.lucene.index.Term) TermStatistics(org.apache.lucene.search.TermStatistics) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) TermContext(org.apache.lucene.index.TermContext) SearchContextException(org.elasticsearch.search.SearchContextException) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) CollectionStatistics(org.apache.lucene.search.CollectionStatistics) ObjectHashSet(com.carrotsearch.hppc.ObjectHashSet) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException)

Example 3 with ObjectHashSet

use of com.carrotsearch.hppc.ObjectHashSet in project crate by crate.

the class MultiPhrasePrefixQuery method rewrite.

@Override
public Query rewrite(IndexReader reader) throws IOException {
    Query rewritten = super.rewrite(reader);
    if (rewritten != this) {
        return rewritten;
    }
    if (termArrays.isEmpty()) {
        return new MatchNoDocsQuery();
    }
    MultiPhraseQuery.Builder query = new MultiPhraseQuery.Builder();
    query.setSlop(slop);
    int sizeMinus1 = termArrays.size() - 1;
    for (int i = 0; i < sizeMinus1; i++) {
        query.add(termArrays.get(i), positions.get(i));
    }
    Term[] suffixTerms = termArrays.get(sizeMinus1);
    int position = positions.get(sizeMinus1);
    ObjectHashSet<Term> terms = new ObjectHashSet<>();
    for (Term term : suffixTerms) {
        getPrefixTerms(terms, term, reader);
        if (terms.size() > maxExpansions) {
            break;
        }
    }
    if (terms.isEmpty()) {
        if (sizeMinus1 == 0) {
            // no prefix and the phrase query is empty
            return Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName());
        }
        // which rewrites query with an empty reader.
        return new BooleanQuery.Builder().add(query.build(), BooleanClause.Occur.MUST).add(Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName()), BooleanClause.Occur.MUST).build();
    }
    query.add(terms.toArray(Term.class), position);
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ObjectHashSet(com.carrotsearch.hppc.ObjectHashSet) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term)

Aggregations

ObjectHashSet (com.carrotsearch.hppc.ObjectHashSet)3 Term (org.apache.lucene.index.Term)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)2 Query (org.apache.lucene.search.Query)2 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)1 TermContext (org.apache.lucene.index.TermContext)1 CollectionStatistics (org.apache.lucene.search.CollectionStatistics)1 TermStatistics (org.apache.lucene.search.TermStatistics)1 SearchContextException (org.elasticsearch.search.SearchContextException)1 RescoreSearchContext (org.elasticsearch.search.rescore.RescoreSearchContext)1 TaskCancelledException (org.elasticsearch.tasks.TaskCancelledException)1