Search in sources :

Example 21 with TermRangeQuery

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

the class TestReaderClosed method test.

public void test() throws Exception {
    assertTrue(reader.getRefCount() > 0);
    IndexSearcher searcher = newSearcher(reader);
    TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true);
    searcher.search(query, 5);
    reader.close();
    try {
        searcher.search(query, 5);
    } catch (AlreadyClosedException ace) {
    // expected
    } catch (RejectedExecutionException ree) {
    // expected if the searcher has been created with threads since LuceneTestCase
    // closes the thread-pool in a reader close listener
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 22 with TermRangeQuery

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

the class QueryParsing method toString.

/**
   * @see #toString(Query,IndexSchema)
   */
public static void toString(Query query, IndexSchema schema, Appendable out, int flags) throws IOException {
    // clear the boosted / is clause flags for recursion
    int subflag = flags & ~(FLAG_BOOSTED | FLAG_IS_CLAUSE);
    if (query instanceof TermQuery) {
        TermQuery q = (TermQuery) query;
        Term t = q.getTerm();
        FieldType ft = writeFieldName(t.field(), schema, out, flags);
        writeFieldVal(t.bytes(), ft, out, flags);
    } else if (query instanceof TermRangeQuery) {
        TermRangeQuery q = (TermRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesLower() ? '[' : '{');
        BytesRef lt = q.getLowerTerm();
        BytesRef ut = q.getUpperTerm();
        if (lt == null) {
            out.append('*');
        } else {
            writeFieldVal(lt, ft, out, flags);
        }
        out.append(" TO ");
        if (ut == null) {
            out.append('*');
        } else {
            writeFieldVal(ut, ft, out, flags);
        }
        out.append(q.includesUpper() ? ']' : '}');
    } else if (query instanceof LegacyNumericRangeQuery) {
        LegacyNumericRangeQuery q = (LegacyNumericRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesMin() ? '[' : '{');
        Number lt = q.getMin();
        Number ut = q.getMax();
        if (lt == null) {
            out.append('*');
        } else {
            out.append(lt.toString());
        }
        out.append(" TO ");
        if (ut == null) {
            out.append('*');
        } else {
            out.append(ut.toString());
        }
        out.append(q.includesMax() ? ']' : '}');
    } else if (query instanceof BooleanQuery) {
        BooleanQuery q = (BooleanQuery) query;
        boolean needParens = false;
        if (q.getMinimumNumberShouldMatch() != 0 || (flags & (FLAG_IS_CLAUSE | FLAG_BOOSTED)) != 0) {
            needParens = true;
        }
        if (needParens) {
            out.append('(');
        }
        boolean first = true;
        for (BooleanClause c : q.clauses()) {
            if (!first) {
                out.append(' ');
            } else {
                first = false;
            }
            if (c.isProhibited()) {
                out.append('-');
            } else if (c.isRequired()) {
                out.append('+');
            }
            Query subQuery = c.getQuery();
            toString(subQuery, schema, out, subflag | FLAG_IS_CLAUSE);
        }
        if (needParens) {
            out.append(')');
        }
        if (q.getMinimumNumberShouldMatch() > 0) {
            out.append('~');
            out.append(Integer.toString(q.getMinimumNumberShouldMatch()));
        }
    } else if (query instanceof PrefixQuery) {
        PrefixQuery q = (PrefixQuery) query;
        Term prefix = q.getPrefix();
        FieldType ft = writeFieldName(prefix.field(), schema, out, flags);
        out.append(prefix.text());
        out.append('*');
    } else if (query instanceof WildcardQuery) {
        out.append(query.toString());
    } else if (query instanceof FuzzyQuery) {
        out.append(query.toString());
    } else if (query instanceof ConstantScoreQuery) {
        out.append(query.toString());
    } else if (query instanceof WrappedQuery) {
        WrappedQuery q = (WrappedQuery) query;
        out.append(q.getOptions());
        toString(q.getWrappedQuery(), schema, out, subflag);
    } else if (query instanceof BoostQuery) {
        BoostQuery q = (BoostQuery) query;
        toString(q.getQuery(), schema, out, subflag | FLAG_BOOSTED);
        out.append("^");
        out.append(Float.toString(q.getBoost()));
    } else {
        out.append(query.getClass().getSimpleName() + '(' + query.toString() + ')');
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FieldType(org.apache.solr.schema.FieldType) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 23 with TermRangeQuery

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

the class CollationTestBase method testFarsiRangeQueryCollating.

public void testFarsiRangeQueryCollating(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd) throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer));
    Document doc = new Document();
    // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
    // orders the U+0698 character before the U+0633 character, so the single
    // index Term below should NOT be returned by a TermRangeQuery with a Farsi
    // Collator (or an Arabic one for the case when Farsi is not supported).
    doc.add(new TextField("content", "ساب", Field.Store.YES));
    writer.addDocument(doc);
    writer.close();
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new TermRangeQuery("content", firstBeg, firstEnd, true, true);
    ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, hits.length);
    query = new TermRangeQuery("content", secondBeg, secondEnd, true, true);
    hits = searcher.search(query, 1000).scoreDocs;
    assertEquals("The index Term should be included.", 1, hits.length);
    reader.close();
    dir.close();
}
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) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexWriter(org.apache.lucene.index.IndexWriter) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 24 with TermRangeQuery

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

the class CollationTestBase method testFarsiRangeFilterCollating.

public void testFarsiRangeFilterCollating(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd) throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer));
    Document doc = new Document();
    doc.add(new TextField("content", "ساب", Field.Store.YES));
    doc.add(new StringField("body", "body", Field.Store.YES));
    writer.addDocument(doc);
    writer.close();
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new TermQuery(new Term("body", "body"));
    // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
    // orders the U+0698 character before the U+0633 character, so the single
    // index Term below should NOT be returned by a TermRangeFilter with a Farsi
    // Collator (or an Arabic one for the case when Farsi searcher not
    // supported).
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(query, Occur.MUST);
    bq.add(new TermRangeQuery("content", firstBeg, firstEnd, true, true), Occur.FILTER);
    ScoreDoc[] result = searcher.search(bq.build(), 1).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);
    bq = new BooleanQuery.Builder();
    bq.add(query, Occur.MUST);
    bq.add(new TermRangeQuery("content", secondBeg, secondEnd, true, true), Occur.FILTER);
    result = searcher.search(bq.build(), 1).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 25 with TermRangeQuery

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

the class TestCustomScoreQuery method testRewrite.

@Test
public void testRewrite() throws Exception {
    IndexReader r = DirectoryReader.open(dir);
    final IndexSearcher s = newSearcher(r);
    Query q = new TermQuery(new Term(TEXT_FIELD, "first"));
    CustomScoreQuery original = new CustomScoreQuery(q);
    CustomScoreQuery rewritten = (CustomScoreQuery) original.rewrite(s.getIndexReader());
    assertTrue("rewritten query should be identical, as TermQuery does not rewrite", original == rewritten);
    assertTrue("no hits for query", s.search(rewritten, 1).totalHits > 0);
    assertEquals(s.search(q, 1).totalHits, s.search(rewritten, 1).totalHits);
    // everything
    q = new TermRangeQuery(TEXT_FIELD, null, null, true, true);
    original = new CustomScoreQuery(q);
    rewritten = (CustomScoreQuery) original.rewrite(s.getIndexReader());
    assertTrue("rewritten query should not be identical, as TermRangeQuery rewrites", original != rewritten);
    assertTrue("no hits for query", s.search(rewritten, 1).totalHits > 0);
    assertEquals(s.search(q, 1).totalHits, s.search(original, 1).totalHits);
    assertEquals(s.search(q, 1).totalHits, s.search(rewritten, 1).totalHits);
    r.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Aggregations

TermRangeQuery (org.apache.lucene.search.TermRangeQuery)29 BooleanQuery (org.apache.lucene.search.BooleanQuery)13 TermQuery (org.apache.lucene.search.TermQuery)11 IndexSearcher (org.apache.lucene.search.IndexSearcher)10 Query (org.apache.lucene.search.Query)10 BytesRef (org.apache.lucene.util.BytesRef)10 Term (org.apache.lucene.index.Term)8 Document (org.apache.lucene.document.Document)7 IndexReader (org.apache.lucene.index.IndexReader)5 BoostQuery (org.apache.lucene.search.BoostQuery)5 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)5 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)5 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)5 PrefixQuery (org.apache.lucene.search.PrefixQuery)5 ScoreDoc (org.apache.lucene.search.ScoreDoc)5 TextField (org.apache.lucene.document.TextField)4 IndexWriter (org.apache.lucene.index.IndexWriter)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3 PhraseQuery (org.apache.lucene.search.PhraseQuery)3 WildcardQuery (org.apache.lucene.search.WildcardQuery)3