Search in sources :

Example 6 with TermRangeQuery

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

the class TestForTooMuchCloning method test.

// Make sure we don't clone IndexInputs too frequently
// during merging and searching:
public void test() throws Exception {
    final MockDirectoryWrapper dir = newMockDirectory();
    final TieredMergePolicy tmp = new TieredMergePolicy();
    tmp.setMaxMergeAtOnce(2);
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig(new MockAnalyzer(random())).setMaxBufferedDocs(2).setMergePolicy(tmp));
    final int numDocs = 20;
    for (int docs = 0; docs < numDocs; docs++) {
        StringBuilder sb = new StringBuilder();
        for (int terms = 0; terms < 100; terms++) {
            sb.append(TestUtil.randomRealisticUnicodeString(random()));
            sb.append(' ');
        }
        final Document doc = new Document();
        doc.add(new TextField("field", sb.toString(), Field.Store.NO));
        w.addDocument(doc);
    }
    final IndexReader r = w.getReader();
    w.close();
    //System.out.println("merge clone count=" + cloneCount);
    assertTrue("too many calls to IndexInput.clone during merging: " + dir.getInputCloneCount(), dir.getInputCloneCount() < 500);
    final IndexSearcher s = newSearcher(r);
    // important: set this after newSearcher, it might have run checkindex
    final int cloneCount = dir.getInputCloneCount();
    // dir.setVerboseClone(true);
    // MTQ that matches all terms so the AUTO_REWRITE should
    // cutover to filter rewrite and reuse a single DocsEnum
    // across all terms;
    final TopDocs hits = s.search(new TermRangeQuery("field", new BytesRef(), new BytesRef("￿"), true, true), 10);
    assertTrue(hits.totalHits > 0);
    final int queryCloneCount = dir.getInputCloneCount() - cloneCount;
    //System.out.println("query clone count=" + queryCloneCount);
    assertTrue("too many calls to IndexInput.clone during TermRangeQuery: " + queryCloneCount, queryCloneCount < 50);
    r.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) TextField(org.apache.lucene.document.TextField) BytesRef(org.apache.lucene.util.BytesRef)

Example 7 with TermRangeQuery

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

the class TermRangeQueryNodeBuilder method build.

@Override
public TermRangeQuery build(QueryNode queryNode) throws QueryNodeException {
    TermRangeQueryNode rangeNode = (TermRangeQueryNode) queryNode;
    FieldQueryNode upper = rangeNode.getUpperBound();
    FieldQueryNode lower = rangeNode.getLowerBound();
    String field = StringUtils.toString(rangeNode.getField());
    String lowerText = lower.getTextAsString();
    String upperText = upper.getTextAsString();
    if (lowerText.length() == 0) {
        lowerText = null;
    }
    if (upperText.length() == 0) {
        upperText = null;
    }
    TermRangeQuery rangeQuery = TermRangeQuery.newStringRange(field, lowerText, upperText, rangeNode.isLowerInclusive(), rangeNode.isUpperInclusive());
    MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod) queryNode.getTag(MultiTermRewriteMethodProcessor.TAG_ID);
    if (method != null) {
        rangeQuery.setRewriteMethod(method);
    }
    return rangeQuery;
}
Also used : FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)

Example 8 with TermRangeQuery

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

the class FieldQueryTest method testRangeQuery.

public void testRangeQuery() throws Exception {
    makeIndexStrMV();
    defgMultiTermQueryTest(new TermRangeQuery(F, new BytesRef("d"), new BytesRef("e"), true, true));
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 9 with TermRangeQuery

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

the class TestQPHelper method testConstantScoreAutoRewrite.

public void testConstantScoreAutoRewrite() throws Exception {
    StandardQueryParser qp = new StandardQueryParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
    Query q = qp.parse("foo*bar", "field");
    assertTrue(q instanceof WildcardQuery);
    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
    q = qp.parse("foo*", "field");
    assertTrue(q instanceof PrefixQuery);
    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
    q = qp.parse("[a TO z]", "field");
    assertTrue(q instanceof TermRangeQuery);
    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) 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) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery)

Example 10 with TermRangeQuery

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

the class ICUCollationField method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
    String f = field.getName();
    BytesRef low = part1 == null ? null : getCollationKey(f, part1);
    BytesRef high = part2 == null ? null : getCollationKey(f, part2);
    if (!field.indexed() && field.hasDocValues()) {
        return SortedSetDocValuesField.newRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
    } else {
        return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
    }
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BytesRef(org.apache.lucene.util.BytesRef)

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