use of org.apache.lucene.search.TermRangeQuery in project elasticsearch by elastic.
the class QueryAnalyzerTests method testExtractQueryMetadata_unsupportedQuery.
public void testExtractQueryMetadata_unsupportedQuery() {
TermRangeQuery termRangeQuery = new TermRangeQuery("_field", null, null, true, false);
UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(termRangeQuery));
assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery));
TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(termQuery1, BooleanClause.Occur.SHOULD);
builder.add(termRangeQuery, BooleanClause.Occur.SHOULD);
BooleanQuery bq = builder.build();
e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq));
assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery));
}
use of org.apache.lucene.search.TermRangeQuery in project neo4j by neo4j.
the class LuceneDocumentStructureTest method shouldBuildRangeSeekByStringQueryForStrings.
@Test
public void shouldBuildRangeSeekByStringQueryForStrings() throws Exception {
// given
TermRangeQuery query = (TermRangeQuery) LuceneDocumentStructure.newRangeSeekByStringQuery("foo", false, null, true);
// then
assertEquals("string", query.getField());
assertEquals("foo", query.getLowerTerm().utf8ToString());
assertEquals(false, query.includesLower());
assertEquals(null, query.getUpperTerm());
assertEquals(true, query.includesUpper());
}
use of org.apache.lucene.search.TermRangeQuery in project jackrabbit by apache.
the class RangeQuery method rewrite.
/**
* Tries to rewrite this query into a standard lucene RangeQuery.
* This rewrite might fail with a TooManyClauses exception. If that
* happens, we use our own implementation.
*
* @param reader the index reader.
* @return the rewritten query or this query if rewriting is not possible.
* @throws IOException if an error occurs.
*/
public Query rewrite(IndexReader reader) throws IOException {
if (transform == TRANSFORM_NONE) {
TermRangeQuery stdRangeQueryImpl = new TermRangeQuery(lowerTerm.field(), lowerTerm.text(), upperTerm.text(), inclusive, inclusive);
stdRangeQueryImpl.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
try {
stdRangeQuery = stdRangeQueryImpl.rewrite(reader);
return stdRangeQuery;
} catch (BooleanQuery.TooManyClauses e) {
log.debug("Too many terms to enumerate, using custom RangeQuery");
// failed, use own implementation
return this;
}
} else {
// term enum
return this;
}
}
use of org.apache.lucene.search.TermRangeQuery in project lucene-solr by apache.
the class TestMultiValuedNumericRangeQuery method testMultiValuedNRQ.
/** Tests LegacyNumericRangeQuery on a multi-valued field (multiple numeric values per document).
* This test ensures, that a classical TermRangeQuery returns exactly the same document numbers as
* LegacyNumericRangeQuery (see SOLR-1322 for discussion) and the multiple precision terms per numeric value
* do not interfere with multiple numeric values.
*/
public void testMultiValuedNRQ() throws Exception {
Directory directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new MockAnalyzer(random())).setMaxBufferedDocs(TestUtil.nextInt(random(), 50, 1000)));
DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.ROOT));
int num = atLeast(500);
for (int l = 0; l < num; l++) {
Document doc = new Document();
for (int m = 0, c = random().nextInt(10); m <= c; m++) {
int value = random().nextInt(Integer.MAX_VALUE);
doc.add(newStringField("asc", format.format(value), Field.Store.NO));
doc.add(new LegacyIntField("trie", value, Field.Store.NO));
}
writer.addDocument(doc);
}
IndexReader reader = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(reader);
num = atLeast(50);
for (int i = 0; i < num; i++) {
int lower = random().nextInt(Integer.MAX_VALUE);
int upper = random().nextInt(Integer.MAX_VALUE);
if (lower > upper) {
int a = lower;
lower = upper;
upper = a;
}
TermRangeQuery cq = TermRangeQuery.newStringRange("asc", format.format(lower), format.format(upper), true, true);
LegacyNumericRangeQuery<Integer> tq = LegacyNumericRangeQuery.newIntRange("trie", lower, upper, true, true);
TopDocs trTopDocs = searcher.search(cq, 1);
TopDocs nrTopDocs = searcher.search(tq, 1);
assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", trTopDocs.totalHits, nrTopDocs.totalHits);
}
reader.close();
directory.close();
}
use of org.apache.lucene.search.TermRangeQuery in project lucene-solr by apache.
the class TestReaderClosed method testReaderChaining.
// LUCENE-3800
public void testReaderChaining() throws Exception {
assertTrue(reader.getRefCount() > 0);
LeafReader wrappedReader = new ParallelLeafReader(getOnlyLeafReader(reader));
// We wrap with a OwnCacheKeyMultiReader so that closing the underlying reader
// does not terminate the threadpool (if that index searcher uses one)
IndexSearcher searcher = newSearcher(new OwnCacheKeyMultiReader(wrappedReader));
TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true);
searcher.search(query, 5);
// close original child reader
reader.close();
try {
searcher.search(query, 5);
} catch (Exception e) {
AlreadyClosedException ace = null;
for (Throwable t = e; t != null; t = t.getCause()) {
if (t instanceof AlreadyClosedException) {
ace = (AlreadyClosedException) t;
}
}
if (ace == null) {
throw new AssertionError("Query failed, but not due to an AlreadyClosedException", e);
}
assertEquals("this IndexReader cannot be used anymore as one of its child readers was closed", ace.getMessage());
} finally {
// close executor: in case of wrap-wrap-wrapping
searcher.getIndexReader().close();
}
}
Aggregations