Search in sources :

Example 46 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery in project lucene-solr by apache.

the class GraphTermsQParserPlugin method createParser.

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {

        @Override
        public Query parse() throws SyntaxError {
            String fname = localParams.get(QueryParsing.F);
            FieldType ft = req.getSchema().getFieldTypeNoEx(fname);
            int maxDocFreq = localParams.getInt("maxDocFreq", Integer.MAX_VALUE);
            //never null
            String qstr = localParams.get(QueryParsing.V);
            if (qstr.length() == 0) {
                return new MatchNoDocsQuery();
            }
            final String[] splitVals = qstr.split(",");
            Term[] terms = new Term[splitVals.length];
            BytesRefBuilder term = new BytesRefBuilder();
            for (int i = 0; i < splitVals.length; i++) {
                String stringVal = splitVals[i].trim();
                if (ft != null) {
                    ft.readableToIndexed(stringVal, term);
                } else {
                    term.copyChars(stringVal);
                }
                BytesRef ref = term.toBytesRef();
                terms[i] = new Term(fname, ref);
            }
            ArrayUtil.timSort(terms);
            return new ConstantScoreQuery(new GraphTermsQuery(fname, terms, maxDocFreq));
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef) FieldType(org.apache.solr.schema.FieldType)

Example 47 with MatchNoDocsQuery

use of org.apache.lucene.search.MatchNoDocsQuery 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

MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)47 Query (org.apache.lucene.search.Query)29 BooleanQuery (org.apache.lucene.search.BooleanQuery)16 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)15 TermQuery (org.apache.lucene.search.TermQuery)14 Term (org.apache.lucene.index.Term)11 ArrayList (java.util.ArrayList)8 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)8 Directory (org.apache.lucene.store.Directory)8 Document (org.apache.lucene.document.Document)7 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)7 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 DirectoryReader (org.apache.lucene.index.DirectoryReader)5 IndexReader (org.apache.lucene.index.IndexReader)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)5 BytesRef (org.apache.lucene.util.BytesRef)5 List (java.util.List)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)4 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)4