Search in sources :

Example 36 with MatchNoDocsQuery

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

the class TopHitsAggregatorTests method testNoResults.

public void testNoResults() throws Exception {
    TopHits result = (TopHits) testCase(new MatchNoDocsQuery(), topHits("_name").sort("string", SortOrder.DESC));
    SearchHits searchHits = ((TopHits) result).getHits();
    assertEquals(0L, searchHits.getTotalHits());
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SearchHits(org.elasticsearch.search.SearchHits)

Example 37 with MatchNoDocsQuery

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

the class TermsQParserPlugin 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);
            String separator = localParams.get(SEPARATOR, ",");
            //never null
            String qstr = localParams.get(QueryParsing.V);
            Method method = Method.valueOf(localParams.get(METHOD, Method.termsFilter.name()));
            //TODO pick the default method based on various heuristics from benchmarks
            //if space then split on all whitespace & trim, otherwise strictly interpret
            final boolean sepIsSpace = separator.equals(" ");
            if (sepIsSpace)
                qstr = qstr.trim();
            if (qstr.length() == 0)
                return new MatchNoDocsQuery();
            final String[] splitVals = sepIsSpace ? qstr.split("\\s+") : qstr.split(Pattern.quote(separator), -1);
            assert splitVals.length > 0;
            if (ft.isPointField()) {
                if (localParams.get(METHOD) != null) {
                    throw new IllegalArgumentException(String.format(Locale.ROOT, "Method '%s' not supported in TermsQParser when using PointFields", localParams.get(METHOD)));
                }
                return ((PointField) ft).getSetQuery(this, req.getSchema().getField(fname), Arrays.asList(splitVals));
            }
            BytesRef[] bytesRefs = new BytesRef[splitVals.length];
            BytesRefBuilder term = new BytesRefBuilder();
            for (int i = 0; i < splitVals.length; i++) {
                String stringVal = splitVals[i];
                //logic same as TermQParserPlugin
                if (ft != null) {
                    ft.readableToIndexed(stringVal, term);
                } else {
                    term.copyChars(stringVal);
                }
                bytesRefs[i] = term.toBytesRef();
            }
            return new SolrConstantScoreQuery(method.makeFilter(fname, bytesRefs));
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PointField(org.apache.solr.schema.PointField) FieldType(org.apache.solr.schema.FieldType) BytesRef(org.apache.lucene.util.BytesRef)

Example 38 with MatchNoDocsQuery

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

the class TestSolrCoreParser method testHandyQuery.

// test custom query (HandyQueryBuilder) wrapping a Query
public void testHandyQuery() throws IOException, ParserException {
    final String lhsXml = "<HelloQuery/>";
    final String rhsXml = "<GoodbyeQuery/>";
    final Query query = parseHandyQuery(lhsXml, rhsXml);
    assertTrue(query instanceof BooleanQuery);
    final BooleanQuery bq = (BooleanQuery) query;
    assertEquals(2, bq.clauses().size());
    assertTrue(bq.clauses().get(0).getQuery() instanceof MatchAllDocsQuery);
    assertTrue(bq.clauses().get(1).getQuery() instanceof MatchNoDocsQuery);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 39 with MatchNoDocsQuery

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

the class TestQueryBitSetProducer method testReaderNotSuitedForCaching.

public void testReaderNotSuitedForCaching() throws IOException {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    w.addDocument(new Document());
    DirectoryReader reader = new DummyDirectoryReader(w.getReader());
    QueryBitSetProducer producer = new QueryBitSetProducer(new MatchNoDocsQuery());
    assertNull(producer.getBitSet(reader.leaves().get(0)));
    assertEquals(0, producer.cache.size());
    producer = new QueryBitSetProducer(new MatchAllDocsQuery());
    BitSet bitSet = producer.getBitSet(reader.leaves().get(0));
    assertEquals(1, bitSet.length());
    assertEquals(true, bitSet.get(0));
    assertEquals(0, producer.cache.size());
    IOUtils.close(reader, w, dir);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BitSet(org.apache.lucene.util.BitSet) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 40 with MatchNoDocsQuery

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

the class TestCheckJoinIndex method testNoParent.

public void testNoParent() throws IOException {
    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final int numDocs = TestUtil.nextInt(random(), 1, 3);
    for (int i = 0; i < numDocs; ++i) {
        w.addDocument(new Document());
    }
    final IndexReader reader = w.getReader();
    w.close();
    BitSetProducer parentsFilter = new QueryBitSetProducer(new MatchNoDocsQuery());
    try {
        CheckJoinIndex.check(reader, parentsFilter);
        fail("Invalid index");
    } catch (IllegalStateException e) {
    // expected
    } finally {
        reader.close();
        dir.close();
    }
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)42 Query (org.apache.lucene.search.Query)25 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 TermQuery (org.apache.lucene.search.TermQuery)11 Term (org.apache.lucene.index.Term)9 ArrayList (java.util.ArrayList)7 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)7 Directory (org.apache.lucene.store.Directory)7 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)7 Document (org.apache.lucene.document.Document)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 IndexReader (org.apache.lucene.index.IndexReader)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)5 List (java.util.List)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 BytesRef (org.apache.lucene.util.BytesRef)4 LongPoint (org.apache.lucene.document.LongPoint)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3