Search in sources :

Example 21 with MatchNoDocsQuery

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

the class BoostingQueryTest method testRewrite.

public void testRewrite() throws IOException {
    IndexReader reader = new MultiReader();
    BoostingQuery q = new BoostingQuery(new BooleanQuery.Builder().build(), new MatchAllDocsQuery(), 3);
    Query rewritten = new IndexSearcher(reader).rewrite(q);
    Query expectedRewritten = new BoostingQuery(new MatchNoDocsQuery(), new MatchAllDocsQuery(), 3);
    assertEquals(expectedRewritten, rewritten);
    assertSame(rewritten, rewritten.rewrite(reader));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiReader(org.apache.lucene.index.MultiReader) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 22 with MatchNoDocsQuery

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

the class TestJoinUtil method testRewrite.

public void testRewrite() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new SortedDocValuesField("join_field", new BytesRef("abc")));
    w.addDocument(doc);
    doc = new Document();
    doc.add(new SortedDocValuesField("join_field", new BytesRef("abd")));
    w.addDocument(doc);
    IndexReader reader = w.getReader();
    IndexSearcher searcher = newSearcher(reader);
    OrdinalMap ordMap = OrdinalMap.build(null, new SortedDocValues[0], 0f);
    Query joinQuery = JoinUtil.createJoinQuery("join_field", new MatchNoDocsQuery(), new MatchNoDocsQuery(), searcher, RandomPicks.randomFrom(random(), ScoreMode.values()), ordMap, 0, Integer.MAX_VALUE);
    // no exception due to missing rewrites
    searcher.search(joinQuery, 1);
    reader.close();
    w.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FieldValueQuery(org.apache.lucene.search.FieldValueQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) OrdinalMap(org.apache.lucene.index.MultiDocValues.OrdinalMap) Directory(org.apache.lucene.store.Directory)

Example 23 with MatchNoDocsQuery

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

the class TestQueryBitSetProducer method testSimple.

public void testSimple() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    w.addDocument(new Document());
    DirectoryReader reader = w.getReader();
    QueryBitSetProducer producer = new QueryBitSetProducer(new MatchNoDocsQuery());
    assertNull(producer.getBitSet(reader.leaves().get(0)));
    assertEquals(1, 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(1, 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 24 with MatchNoDocsQuery

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

the class TestQPHelper method testStopwords.

public void testStopwords() throws Exception {
    StandardQueryParser qp = new StandardQueryParser();
    CharacterRunAutomaton stopSet = new CharacterRunAutomaton(new RegExp("the|foo").toAutomaton());
    qp.setAnalyzer(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, stopSet));
    Query result = qp.parse("a:the OR a:foo", "a");
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a MatchNoDocsQuery", result instanceof MatchNoDocsQuery);
    result = qp.parse("a:woo OR a:the", "a");
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a TermQuery", result instanceof TermQuery);
    result = qp.parse("(fieldX:xxxxx OR fieldy:xxxxxxxx)^2 AND (fieldx:the OR fieldy:foo)", "a");
    Query expected = new BooleanQuery.Builder().add(new TermQuery(new Term("fieldX", "xxxxx")), Occur.SHOULD).add(new TermQuery(new Term("fieldy", "xxxxxxxx")), Occur.SHOULD).build();
    expected = new BoostQuery(expected, 2f);
    assertEquals(expected, result);
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) RegExp(org.apache.lucene.util.automaton.RegExp) CharacterRunAutomaton(org.apache.lucene.util.automaton.CharacterRunAutomaton) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 25 with MatchNoDocsQuery

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

the class TestMultiFieldQPHelper method testStaticMethod1.

public void testStaticMethod1() throws QueryNodeException {
    String[] fields = { "b", "t" };
    String[] queries = { "one", "two" };
    Query q = QueryParserUtil.parse(queries, fields, new MockAnalyzer(random()));
    assertEquals("b:one t:two", q.toString());
    String[] queries2 = { "+one", "+two" };
    q = QueryParserUtil.parse(queries2, fields, new MockAnalyzer(random()));
    assertEquals("b:one t:two", q.toString());
    String[] queries3 = { "one", "+two" };
    q = QueryParserUtil.parse(queries3, fields, new MockAnalyzer(random()));
    assertEquals("b:one t:two", q.toString());
    String[] queries4 = { "one +more", "+two" };
    q = QueryParserUtil.parse(queries4, fields, new MockAnalyzer(random()));
    assertEquals("(b:one +b:more) t:two", q.toString());
    String[] queries5 = { "blah" };
    // expected exception, array length differs
    expectThrows(IllegalArgumentException.class, () -> {
        QueryParserUtil.parse(queries5, fields, new MockAnalyzer(random()));
    });
    // check also with stop words for this static form (qtxts[], fields[]).
    TestQPHelper.QPTestAnalyzer stopA = new TestQPHelper.QPTestAnalyzer();
    String[] queries6 = { "((+stop))", "+((stop))" };
    q = QueryParserUtil.parse(queries6, fields, stopA);
    assertEquals("MatchNoDocsQuery(\"\") MatchNoDocsQuery(\"\")", q.toString());
    //assertEquals(" ", q.toString());
    String[] queries7 = { "one ((+stop)) +more", "+((stop)) +two" };
    q = QueryParserUtil.parse(queries7, fields, stopA);
    assertEquals("(b:one +b:more) (+t:two)", q.toString());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery)

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