Search in sources :

Example 16 with TermRangeQuery

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));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) UnsupportedQueryException(org.elasticsearch.percolator.QueryAnalyzer.UnsupportedQueryException) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm)

Example 17 with 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());
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Test(org.junit.Test)

Example 18 with TermRangeQuery

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;
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery)

Example 19 with TermRangeQuery

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) LegacyIntField(org.apache.solr.legacy.LegacyIntField)

Example 20 with TermRangeQuery

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();
    }
}
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) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException)

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