Search in sources :

Example 16 with SpanQuery

use of org.apache.lucene.queries.spans.SpanQuery in project OpenSearch by opensearch-project.

the class SpanBooleanQueryRewriteWithMaxClause method rewrite.

@Override
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
    final MultiTermQuery.RewriteMethod delegate = new MultiTermQuery.RewriteMethod() {

        @Override
        public Query rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
            Collection<SpanQuery> queries = collectTerms(reader, query);
            if (queries.size() == 0) {
                return new SpanMatchNoDocsQuery(query.getField(), "no expansion found for " + query.toString());
            } else if (queries.size() == 1) {
                return queries.iterator().next();
            } else {
                return new SpanOrQuery(queries.toArray(new SpanQuery[0]));
            }
        }

        private Collection<SpanQuery> collectTerms(IndexReader reader, MultiTermQuery query) throws IOException {
            Set<SpanQuery> queries = new HashSet<>();
            IndexReaderContext topReaderContext = reader.getContext();
            for (LeafReaderContext context : topReaderContext.leaves()) {
                final Terms terms = context.reader().terms(query.getField());
                if (terms == null) {
                    // field does not exist
                    continue;
                }
                final TermsEnum termsEnum = getTermsEnum(query, terms, new AttributeSource());
                assert termsEnum != null;
                if (termsEnum == TermsEnum.EMPTY) {
                    continue;
                }
                BytesRef bytes;
                while ((bytes = termsEnum.next()) != null) {
                    if (queries.size() >= maxExpansions) {
                        if (hardLimit) {
                            throw new RuntimeException("[" + query.toString() + " ] " + "exceeds maxClauseCount [ Boolean maxClauseCount is set to " + BooleanQuery.getMaxClauseCount() + "]");
                        } else {
                            return queries;
                        }
                    }
                    queries.add(new SpanTermQuery(new Term(query.getField(), bytes)));
                }
            }
            return queries;
        }
    };
    return (SpanQuery) delegate.rewrite(reader, query);
}
Also used : AttributeSource(org.apache.lucene.util.AttributeSource) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Terms(org.apache.lucene.index.Terms) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) TermsEnum(org.apache.lucene.index.TermsEnum) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) IndexReader(org.apache.lucene.index.IndexReader) SpanMatchNoDocsQuery(org.apache.lucene.queries.SpanMatchNoDocsQuery) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) HashSet(java.util.HashSet)

Aggregations

SpanQuery (org.apache.lucene.queries.spans.SpanQuery)16 Query (org.apache.lucene.search.Query)10 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)9 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)8 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)8 Term (org.apache.lucene.index.Term)6 SpanMatchNoDocsQuery (org.apache.lucene.queries.SpanMatchNoDocsQuery)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 IndexReader (org.apache.lucene.index.IndexReader)2 FieldMaskingSpanQuery (org.apache.lucene.queries.spans.FieldMaskingSpanQuery)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 SynonymQuery (org.apache.lucene.search.SynonymQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 MockSynonymAnalyzer (org.apache.lucene.tests.analysis.MockSynonymAnalyzer)2 BytesRef (org.apache.lucene.util.BytesRef)2