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());
}
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));
}
};
}
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);
}
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);
}
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();
}
}
Aggregations