Java Code Examples for org.apache.lucene.search.TermRangeQuery

The following examples show how to use org.apache.lucene.search.TermRangeQuery. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetBestFragmentsFilteredQuery() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      SpanQuery clauses[] = { new SpanTermQuery(new Term("contents", "john")),
          new SpanTermQuery(new Term("contents", "kennedy")), };
      SpanNearQuery snq = new SpanNearQuery(clauses, 1, true);
      BooleanQuery.Builder bq = new BooleanQuery.Builder();
      bq.add(snq, Occur.MUST);
      bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);

      doSearching(bq.build());
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      // Currently highlights "John" and "Kennedy" separately
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 2);
    }
  };

  helper.start();
}
 
Example 2
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetBestFragmentsFilteredPhraseQuery() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      PhraseQuery pq = new PhraseQuery("contents", "john", "kennedy");
      BooleanQuery.Builder bq = new BooleanQuery.Builder();
      bq.add(pq, Occur.MUST);
      bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);

      doSearching(bq.build());
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      // Currently highlights "John" and "Kennedy" separately
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 2);
    }
  };

  helper.start();
}
 
Example 3
Source Project: james-project   Source File: LuceneMessageSearchIndex.java    License: Apache License 2.0 6 votes vote down vote up
private Query createQuery(String field, DateOperator dop) throws UnsupportedSearchException {
    Date date = dop.getDate();
    DateResolution res = dop.getDateResultion();
    DateTools.Resolution dRes = toResolution(res);
    String value = DateTools.dateToString(date, dRes);
    switch (dop.getType()) {
    case ON:
        return new TermQuery(new Term(field, value));
    case BEFORE: 
        return new TermRangeQuery(field, DateTools.dateToString(MIN_DATE, dRes), value, true, false);
    case AFTER: 
        return new TermRangeQuery(field, value, DateTools.dateToString(MAX_DATE, dRes), false, true);
    default:
        throw new UnsupportedSearchException();
    }
}
 
Example 4
Source Project: stratio-cassandra   Source File: RangeConditionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStringClose() {

    Map<String, ColumnMapper> map = new HashMap<>();
    map.put("name", new ColumnMapperBoolean());
    Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());

    RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", "beta", true, true);
    Query query = rangeCondition.query(mappers);

    Assert.assertNotNull(query);
    Assert.assertEquals(TermRangeQuery.class, query.getClass());
    Assert.assertEquals("name", ((TermRangeQuery) query).getField());
    Assert.assertEquals("alpha", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
    Assert.assertEquals("beta", ((TermRangeQuery) query).getUpperTerm().utf8ToString());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper());
    Assert.assertEquals(0.5f, query.getBoost(), 0);
}
 
Example 5
Source Project: stratio-cassandra   Source File: RangeConditionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStringOpen() {

    Map<String, ColumnMapper> map = new HashMap<>();
    map.put("name", new ColumnMapperBoolean());
    Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());

    RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", null, true, false);
    Query query = rangeCondition.query(mappers);

    Assert.assertNotNull(query);
    Assert.assertEquals(TermRangeQuery.class, query.getClass());
    Assert.assertEquals("name", ((TermRangeQuery) query).getField());
    Assert.assertEquals("alpha", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
    Assert.assertEquals(null, ((TermRangeQuery) query).getUpperTerm());
    Assert.assertNull(((TermRangeQuery) query).getUpperTerm());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
    Assert.assertEquals(false, ((TermRangeQuery) query).includesUpper());
    Assert.assertEquals(0.5f, query.getBoost(), 0);
}
 
Example 6
Source Project: stratio-cassandra   Source File: RangeConditionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInetV4() {

    Map<String, ColumnMapper> map = new HashMap<>();
    map.put("name", new ColumnMapperInet());
    Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());

    RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "192.168.0.01", "192.168.0.045", true, true);
    Query query = rangeCondition.query(mappers);

    Assert.assertNotNull(query);
    Assert.assertEquals(TermRangeQuery.class, query.getClass());
    Assert.assertEquals("name", ((TermRangeQuery) query).getField());
    Assert.assertEquals("192.168.0.1", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
    Assert.assertEquals("192.168.0.45", ((TermRangeQuery) query).getUpperTerm().utf8ToString());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper());
    Assert.assertEquals(0.5f, query.getBoost(), 0);
}
 
Example 7
Source Project: stratio-cassandra   Source File: RangeConditionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInetV6() {

    Map<String, ColumnMapper> map = new HashMap<>();
    map.put("name", new ColumnMapperInet());
    Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());

    RangeCondition rangeCondition = range("name").boost(0.5f)
                                                 .lower("2001:DB8:2de::e13")
                                                 .upper("2001:DB8:02de::e23")
                                                 .includeLower(true)
                                                 .includeUpper(true)
                                                 .build();
    Query query = rangeCondition.query(mappers);

    Assert.assertNotNull(query);
    Assert.assertEquals(TermRangeQuery.class, query.getClass());
    Assert.assertEquals("name", ((TermRangeQuery) query).getField());
    Assert.assertEquals("2001:db8:2de:0:0:0:0:e13", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
    Assert.assertEquals("2001:db8:2de:0:0:0:0:e23", ((TermRangeQuery) query).getUpperTerm().utf8ToString());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
    Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper());
    Assert.assertEquals(0.5f, query.getBoost(), 0);
}
 
Example 8
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test5() throws ParseException, IOException {
  parser = new SuperParser(LUCENE_VERSION, getFieldManager(new WhitespaceAnalyzer(LUCENE_VERSION)), true, null,
      ScoreType.SUPER, new Term("_primedoc_"));
  Query query = parser.parse("<a.a:a a.d:{e TO f} a.b:b a.test:hello\\<> -<g.c:c g.d:d>");

  BooleanQuery booleanQuery1 = new BooleanQuery();
  booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
  booleanQuery1.add(new TermRangeQuery("a.d", new BytesRef("e"), new BytesRef("f"), false, false), Occur.SHOULD);
  booleanQuery1.add(new TermQuery(new Term("a.b", "b")), Occur.SHOULD);
  // std analyzer took the "<" out
  booleanQuery1.add(new TermQuery(new Term("a.test", "hello<")), Occur.SHOULD);

  BooleanQuery booleanQuery2 = new BooleanQuery();
  booleanQuery2.add(new TermQuery(new Term("g.c", "c")), Occur.SHOULD);
  booleanQuery2.add(new TermQuery(new Term("g.d", "d")), Occur.SHOULD);

  SuperQuery superQuery1 = new SuperQuery(booleanQuery1, ScoreType.SUPER, new Term("_primedoc_"));
  SuperQuery superQuery2 = new SuperQuery(booleanQuery2, ScoreType.SUPER, new Term("_primedoc_"));

  BooleanQuery booleanQuery = new BooleanQuery();
  booleanQuery.add(superQuery1, Occur.SHOULD);
  booleanQuery.add(superQuery2, Occur.MUST_NOT);

  assertQuery(booleanQuery, query);
}
 
Example 9
Source Project: word2vec-lucene   Source File: LuceneIndexCorpus.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void learnVocab() throws IOException {
  super.learnVocab();

  final String field = ((LuceneIndexConfig)config).getField();
  final Terms terms = MultiFields.getTerms(reader, field);
  final BytesRef maxTerm = terms.getMax();
  final BytesRef minTerm = terms.getMin();
  Query q = new TermRangeQuery(field, minTerm, maxTerm, true, true);
  IndexSearcher searcher = new IndexSearcher(reader);
  topDocs = searcher.search(q, Integer.MAX_VALUE);

  TermsEnum termsEnum = null;
  termsEnum = terms.iterator(termsEnum);

  termsEnum.seekCeil(new BytesRef());
  BytesRef term = termsEnum.term();
  while(term != null){
    int p = addWordToVocab(term.utf8ToString());
    vocab[p].setCn((int)termsEnum.totalTermFreq());
    term = termsEnum.next();
  }
}
 
Example 10
Source Project: jstarcraft-core   Source File: LuceneQueryTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTermRangeQuery() throws Exception {
    // 范围查询
    Query query = new TermRangeQuery("title", new BytesRef("Toa"), new BytesRef("Toz"), true, true);
    TopDocs search = searcher.search(query, 1000);
    Assert.assertEquals(22, search.totalHits.value);
}
 
Example 11
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive)
{
    Analyzer multiAnalyzer = constructMultiTermAnalyzer(getQueryAnalyzer());
    BytesRef lower = analyzeMultiTerm(field.getName(), part1, multiAnalyzer);
    BytesRef upper = analyzeMultiTerm(field.getName(), part2, multiAnalyzer);
    return new TermRangeQuery(field.getName(), lower, upper, minInclusive, maxInclusive);
}
 
Example 12
Source Project: dremio-oss   Source File: LuceneQueryConverter.java    License: 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 13
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testGetRangeFragments() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;

      // Need to explicitly set the QueryParser property to use TermRangeQuery
      // rather
      // than RangeFilters

      TermRangeQuery rangeQuery = new TermRangeQuery(
          FIELD_NAME,
          new BytesRef("kannedy"),
          new BytesRef("kznnedy"),
          true, true);
      rangeQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);

      query = rangeQuery;
      doSearching(query);

      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 5);
    }
  };

  helper.start();
}
 
Example 14
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNotRewriteMultiTermQuery() throws IOException {
  // field "bar": (not the field we ultimately want to extract)
  MultiTermQuery mtq = new TermRangeQuery("bar", new BytesRef("aa"), new BytesRef("zz"), true, true) ;
  WeightedSpanTermExtractor extractor = new WeightedSpanTermExtractor() {
    @Override
    protected void extract(Query query, float boost, Map<String, WeightedSpanTerm> terms) throws IOException {
      assertEquals(mtq, query);
      super.extract(query, boost, terms);
    }
  };
  extractor.setExpandMultiTermQuery(true);
  extractor.setMaxDocCharsToAnalyze(51200);
  extractor.getWeightedSpanTerms(
      mtq, 3, new CannedTokenStream(new Token("aa",0,2), new Token("bb", 2,4)), "foo"); // field "foo"
}
 
Example 15
Source Project: lucene-solr   Source File: CollationTestBase.java    License: Apache License 2.0 5 votes vote down vote up
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", "\u0633\u0627\u0628", 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();
}
 
Example 16
Source Project: lucene-solr   Source File: TermRangeQueryNodeBuilder.java    License: 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 17
Source Project: lucene-solr   Source File: RangeQueryBuilder.java    License: 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 18
Source Project: lucene-solr   Source File: TestPrecedenceQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public void testRange() throws Exception {
  assertQueryEquals("[ a TO z]", null, "[a TO z]");
  assertTrue(getQuery("[ a TO z]", null) instanceof TermRangeQuery);
  assertQueryEquals("[ a TO z ]", null, "[a TO z]");
  assertQueryEquals("{ a TO z}", null, "{a TO z}");
  assertQueryEquals("{ a TO z }", null, "{a TO z}");
  assertQueryEquals("{ a TO z }^2.0", null, "({a TO z})^2.0");
  assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
  assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
  assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
  assertQueryEquals("gack ( bar blar { a TO z}) ", null,
      "gack (bar blar {a TO z})");
}
 
Example 19
Source Project: lucene-solr   Source File: TestQPHelper.java    License: Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 20
Source Project: lucene-solr   Source File: TestQPHelper.java    License: Apache License 2.0 5 votes vote down vote up
public void testRange() throws Exception {
  assertQueryEquals("[ a TO z]", null, "[a TO z]");
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((TermRangeQuery)getQuery("[ a TO z]", null)).getRewriteMethod());

  StandardQueryParser qp = new StandardQueryParser();
  
  qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
  assertEquals(MultiTermQuery.SCORING_BOOLEAN_REWRITE,((TermRangeQuery)qp.parse("[ a TO z]", "field")).getRewriteMethod());

  // test open ranges
  assertQueryEquals("[ a TO * ]", null, "[a TO *]");
  assertQueryEquals("[ * TO z ]", null, "[* TO z]");
  assertQueryEquals("[ * TO * ]", null, "[* TO *]");
  
  assertQueryEquals("field>=a", null, "[a TO *]");
  assertQueryEquals("field>a", null, "{a TO *]");
  assertQueryEquals("field<=a", null, "[* TO a]");
  assertQueryEquals("field<a", null, "[* TO a}");
  
  // mixing exclude and include bounds
  assertQueryEquals("{ a TO z ]", null, "{a TO z]");
  assertQueryEquals("[ a TO z }", null, "[a TO z}");
  assertQueryEquals("{ a TO * ]", null, "{a TO *]");
  assertQueryEquals("[ * TO z }", null, "[* TO z}");
  
  assertQueryEquals("[ a TO z ]", null, "[a TO z]");
  assertQueryEquals("{ a TO z}", null, "{a TO z}");
  assertQueryEquals("{ a TO z }", null, "{a TO z}");
  assertQueryEquals("{ a TO z }^2.0", null, "({a TO z})^2.0");
  assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
  assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
  assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
  assertQueryEquals("gack ( bar blar { a TO z}) ", null,
      "gack (bar blar {a TO z})");
}
 
Example 21
Source Project: lucene-solr   Source File: QueryParserTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public void testRange() throws Exception {
   assertQueryEquals("[ a TO z]", null, "[a TO z]");
   assertQueryEquals("[ a TO z}", null, "[a TO z}");
   assertQueryEquals("{ a TO z]", null, "{a TO z]"); 

    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((TermRangeQuery)getQuery("[ a TO z]")).getRewriteMethod());

   CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
   
   qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
   assertEquals(MultiTermQuery.SCORING_BOOLEAN_REWRITE,((TermRangeQuery)getQuery("[ a TO z]", qp)).getRewriteMethod());
   
   // test open ranges
   assertQueryEquals("[ a TO * ]", null, "[a TO *]");
   assertQueryEquals("[ * TO z ]", null, "[* TO z]");
   assertQueryEquals("[ * TO * ]", null, "[* TO *]");
   
   // mixing exclude and include bounds
   assertQueryEquals("{ a TO z ]", null, "{a TO z]");
   assertQueryEquals("[ a TO z }", null, "[a TO z}");
   assertQueryEquals("{ a TO * ]", null, "{a TO *]");
   assertQueryEquals("[ * TO z }", null, "[* TO z}");
   
   assertQueryEquals("[ a TO z ]", null, "[a TO z]");
   assertQueryEquals("{ a TO z}", null, "{a TO z}");
   assertQueryEquals("{ a TO z }", null, "{a TO z}");
   assertQueryEquals("{ a TO z }^2.0", null, "({a TO z})^2.0");
   assertQueryEquals("[ a TO z] OR bar", null, "[a TO z] bar");
   assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
   assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
   assertQueryEquals("gack ( bar blar { a TO z}) ", null, "gack (bar blar {a TO z})");

   assertQueryEquals("[* TO Z]",null,"[* TO z]");
   assertQueryEquals("[A TO *]",null,"[a TO *]");
   assertQueryEquals("[* TO *]",null,"[* TO *]");
}
 
Example 22
Source Project: lucene-solr   Source File: QueryParserTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public void testNestedAndClausesFoo() throws Exception {
  String query = "(field1:[1 TO *] AND field1:[* TO 2]) AND field2:(z)";
  BooleanQuery.Builder q = new BooleanQuery.Builder();
  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  bq.add(TermRangeQuery.newStringRange("field1", "1", null, true, true), BooleanClause.Occur.MUST);
  bq.add(TermRangeQuery.newStringRange("field1", null, "2", true, true), BooleanClause.Occur.MUST);
  q.add(bq.build(), BooleanClause.Occur.MUST);
  q.add(new TermQuery(new Term("field2", "z")), BooleanClause.Occur.MUST);
  assertEquals(q.build(), getQuery(query, new MockAnalyzer(random())));
}
 
Example 23
Source Project: lucene-solr   Source File: TestReaderClosed.java    License: 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 24
Source Project: lucene-solr   Source File: ICUCollationField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Query getSpecializedRangeQuery(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.newSlowRangeQuery(
        field.getName(), low, high, minInclusive, maxInclusive);
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
 
Example 25
Source Project: lucene-solr   Source File: CollationField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Query getSpecializedRangeQuery(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.newSlowRangeQuery(
        field.getName(), low, high, minInclusive, maxInclusive);
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
 
Example 26
Source Project: HongsCORE   Source File: StringQuery.java    License: 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 27
Source Project: incubator-atlas   Source File: TermRangeQueryExpression.java    License: Apache License 2.0 5 votes vote down vote up
public TermRangeQueryExpression(TermRangeQuery query, ResourceDefinition resourceDefinition) {
    super(query.getField(), null, resourceDefinition);
    m_lowerTerm = query.getLowerTerm();
    m_upperTerm = query.getUpperTerm();
    m_lowerInclusive = query.includesLower();
    m_upperInclusive = query.includesUpper();
}
 
Example 28
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
public static void assertEqualsQuery(Query expected, Query actual) {
  assertEquals(expected.getClass(), actual.getClass());
  if (expected instanceof BooleanQuery) {
    assertEqualsBooleanQuery((BooleanQuery) expected, (BooleanQuery) actual);
  } else if (expected instanceof SuperQuery) {
    assertEqualsSuperQuery((SuperQuery) expected, (SuperQuery) actual);
  } else if (expected instanceof TermQuery) {
    assertEqualsTermQuery((TermQuery) expected, (TermQuery) actual);
  } else if (expected instanceof PrefixQuery) {
    assertEqualsPrefixQuery((PrefixQuery) expected, (PrefixQuery) actual);
  } else if (expected instanceof WildcardQuery) {
    assertEqualsWildcardQuery((WildcardQuery) expected, (WildcardQuery) actual);
  } else if (expected instanceof FuzzyQuery) {
    assertEqualsFuzzyQuery((FuzzyQuery) expected, (FuzzyQuery) actual);
  } else if (expected instanceof RegexpQuery) {
    assertEqualsRegexpQuery((RegexpQuery) expected, (RegexpQuery) actual);
  } else if (expected instanceof TermRangeQuery) {
    assertEqualsTermRangeQuery((TermRangeQuery) expected, (TermRangeQuery) actual);
  } else if (expected instanceof MatchAllDocsQuery) {
    assertEqualsMatchAllDocsQuery((MatchAllDocsQuery) expected, (MatchAllDocsQuery) actual);
  } else if (expected instanceof MultiPhraseQuery) {
    assertEqualsMultiPhraseQuery((MultiPhraseQuery) expected, (MultiPhraseQuery) actual);
  } else if (expected instanceof PhraseQuery) {
    assertEqualsPhraseQuery((PhraseQuery) expected, (PhraseQuery) actual);
  } else if (expected instanceof NumericRangeQuery<?>) {
    assertEqualsNumericRangeQuery((NumericRangeQuery<?>) expected, (NumericRangeQuery<?>) actual);
  } else {
    fail("Type [" + expected.getClass() + "] not supported");
  }
}
 
Example 29
private void testFarsiRangeFilterCollating(Analyzer analyzer, BytesRef firstBeg,
                                   BytesRef firstEnd, BytesRef secondBeg,
                                   BytesRef secondEnd) throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer));
    Document doc = new Document();
    doc.add(new TextField("content", "\u0633\u0627\u0628", 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, BooleanClause.Occur.MUST);
    bq.add(new TermRangeQuery("content", firstBeg, firstEnd, true, true), BooleanClause.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, BooleanClause.Occur.MUST);
    bq.add(new TermRangeQuery("content", secondBeg, secondEnd, true, true), BooleanClause.Occur.FILTER);
    result = searcher.search(bq.build(), 1).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);

    reader.close();
    dir.close();
}
 
Example 30
Source Project: cxf   Source File: LuceneQueryVisitor.java    License: 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;
        }
    }