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