Java Code Examples for org.apache.lucene.search.TermRangeQuery#newStringRange()

The following examples show how to use org.apache.lucene.search.TermRangeQuery#newStringRange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LuceneQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private Query toRangeQuery(RangeTerm range) {
  return TermRangeQuery.newStringRange(
      range.getField(),
      range.hasMin() ? range.getMin() : null,
      range.hasMax() ? range.getMax() : null,
      range.getMinInclusive(),
      range.getMaxInclusive());
}
 
Example 2
Source File: TermRangeQueryNodeBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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;
  
}
 
Example 3
Source File: RangeQueryBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Query getQuery(Element e) throws ParserException {
  String fieldName = DOMUtils.getAttributeWithInheritance(e, "fieldName");

  String lowerTerm = e.getAttribute("lowerTerm");
  String upperTerm = e.getAttribute("upperTerm");
  boolean includeLower = DOMUtils.getAttribute(e, "includeLower", true);
  boolean includeUpper = DOMUtils.getAttribute(e, "includeUpper", true);
  return TermRangeQuery.newStringRange(fieldName, lowerTerm, upperTerm, includeLower, includeUpper);
}
 
Example 4
Source File: TestReaderClosed.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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
  }
}
 
Example 5
Source File: StringQuery.java    From HongsCORE with MIT License 5 votes vote down vote up
@Override
public Query whr(String k, Object n, Object x, boolean l, boolean g) {
    String n2 = Synt.declare(n, "");
    String x2 = Synt.declare(x, "");
    Query  q2 = TermRangeQuery.newStringRange("@" + k, n2, x2, l, g);
    return q2 ;
}
 
Example 6
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Query createRangeQuery(Class<?> cls, String name, Object value, ConditionType type) {

        boolean minInclusive = type == ConditionType.GREATER_OR_EQUALS || type == ConditionType.EQUALS;
        boolean maxInclusive = type == ConditionType.LESS_OR_EQUALS || type == ConditionType.EQUALS;

        if (String.class.isAssignableFrom(cls) || Number.class.isAssignableFrom(cls)) {
            Query query = null;

            if (Double.class.isAssignableFrom(cls)) {
                query = createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Float.class.isAssignableFrom(cls)) {
                query = createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Long.class.isAssignableFrom(cls)) {
                query = createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else {
                query = createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
            }

            return query;
        } else if (Date.class.isAssignableFrom(cls)) {
            final Date date = getValue(Date.class, getFieldTypeConverter(), value.toString());
            final String luceneDateValue = getString(Date.class, getFieldTypeConverter(), date);

            if (type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS) {
                return TermRangeQuery.newStringRange(name, null, luceneDateValue, minInclusive, maxInclusive);
            }
            return TermRangeQuery.newStringRange(name, luceneDateValue,
                DateTools.dateToString(new Date(), Resolution.MILLISECOND), minInclusive, maxInclusive);
        } else {
            return null;
        }
    }
 
Example 7
Source File: TestUnifiedHighlighterMTQ.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testRanges() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("This is a test.");
  iw.addDocument(doc);
  body.setStringValue("Test a one sentence document.");
  iw.addDocument(doc);

  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  Query query = TermRangeQuery.newStringRange("body", "ta", "tf", true, true);
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  String snippets[] = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

  // null start
  query = TermRangeQuery.newStringRange("body", null, "tf", true, true);
  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This <b>is</b> <b>a</b> <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> <b>a</b> <b>one</b> <b>sentence</b> <b>document</b>.", snippets[1]);

  // null end
  query = TermRangeQuery.newStringRange("body", "ta", null, true, true);
  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("<b>This</b> is a <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

  // exact start inclusive
  query = TermRangeQuery.newStringRange("body", "test", "tf", true, true);
  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

  // exact end inclusive
  query = TermRangeQuery.newStringRange("body", "ta", "test", true, true);
  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

  // exact start exclusive
  BooleanQuery bq = new BooleanQuery.Builder()
      .add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD)
      .add(TermRangeQuery.newStringRange("body", "test", "tf", false, true), BooleanClause.Occur.SHOULD)
      .build();
  topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", bq, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a test.", snippets[0]);
  assertEquals("Test a one sentence document.", snippets[1]);

  // exact end exclusive
  bq = new BooleanQuery.Builder()
      .add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD)
      .add(TermRangeQuery.newStringRange("body", "ta", "test", true, false), BooleanClause.Occur.SHOULD)
      .build();
  topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", bq, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a test.", snippets[0]);
  assertEquals("Test a one sentence document.", snippets[1]);

  // wrong field
  highlighter.setFieldMatcher(null);//default
  bq = new BooleanQuery.Builder()
      .add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD)
      .add(TermRangeQuery.newStringRange("bogus", "ta", "tf", true, true), BooleanClause.Occur.SHOULD)
      .build();
  topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  snippets = highlighter.highlight("body", bq, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a test.", snippets[0]);
  assertEquals("Test a one sentence document.", snippets[1]);

  ir.close();
}
 
Example 8
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testCollatedRange() throws Exception {
  CommonQueryParserConfiguration qp = getParserConfig(new MockCollationAnalyzer());
  Query expected = TermRangeQuery.newStringRange(getDefaultField(), "collatedabc", "collateddef", true, true);
  Query actual = getQuery("[abc TO def]", qp);
  assertEquals(expected, actual);
}
 
Example 9
Source File: TestMultiValuedNumericRangeQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** 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);
    TopScoreDocCollector trCollector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
    TopScoreDocCollector nrCollector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
    searcher.search(cq, trCollector);
    searcher.search(tq, nrCollector);
    TopDocs trTopDocs = trCollector.topDocs();
    TopDocs nrTopDocs = nrCollector.topDocs();
    assertEquals("Returned count for LegacyNumericRangeQuery and TermRangeQuery must be equal", trTopDocs.totalHits.value, nrTopDocs.totalHits.value );
  }
  reader.close();
  directory.close();
}
 
Example 10
Source File: SuperParserTest.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
private Query rq_ie(String field, String part1, String part2) {
  return TermRangeQuery.newStringRange(field, part1, part2, true, false);
}