org.apache.lucene.search.TermRangeQuery Java Examples

The following examples show how to use org.apache.lucene.search.TermRangeQuery. 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: LuceneMessageSearchIndex.java    From james-project with 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 #2
Source File: HighlighterTest.java    From lucene-solr with 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 #3
Source File: HighlighterTest.java    From lucene-solr with 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 #4
Source File: RangeConditionTest.java    From stratio-cassandra with 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 File: RangeConditionTest.java    From stratio-cassandra with 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 File: RangeConditionTest.java    From stratio-cassandra with 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 File: LuceneIndexCorpus.java    From word2vec-lucene with 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 #8
Source File: RangeConditionTest.java    From stratio-cassandra with 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 #9
Source File: SuperParserTest.java    From incubator-retired-blur with 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 #10
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 #11
Source File: LuceneQueryTestCase.java    From jstarcraft-core with 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 #12
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 #13
Source File: TermRangeQueryExpression.java    From incubator-atlas with 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 #14
Source File: SuperParserTest.java    From incubator-retired-blur with 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 #15
Source File: IcuCollationKeyAnalyzerTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #16
Source File: CollationField.java    From lucene-solr with 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 #17
Source File: QueryHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
protected QueryBuilder convertQuery(final QueryContext context, final Query query, final float boost) {
    if (query instanceof TermQuery) {
        return convertTermQuery(context, (TermQuery) query, boost);
    } else if (query instanceof TermRangeQuery) {
        return convertTermRangeQuery(context, (TermRangeQuery) query, boost);
    } else if (query instanceof PhraseQuery) {
        return convertPhraseQuery(context, (PhraseQuery) query, boost);
    } else if (query instanceof FuzzyQuery) {
        return convertFuzzyQuery(context, (FuzzyQuery) query, boost);
    } else if (query instanceof PrefixQuery) {
        return convertPrefixQuery(context, (PrefixQuery) query, boost);
    } else if (query instanceof WildcardQuery) {
        return convertWildcardQuery(context, (WildcardQuery) query, boost);
    } else if (query instanceof BooleanQuery) {
        final BooleanQuery booleanQuery = (BooleanQuery) query;
        return convertBooleanQuery(context, booleanQuery, boost);
    } else if (query instanceof MatchAllDocsQuery) {
        return QueryBuilders.matchAllQuery();
    } else if (query instanceof BoostQuery) {
        final BoostQuery boostQuery = (BoostQuery) query;
        return convertQuery(context, boostQuery.getQuery(), boostQuery.getBoost());
    }
    throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY), "Unknown q: "
            + query.getClass() + " => " + query);
}
 
Example #18
Source File: QueryHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
protected QueryBuilder convertTermRangeQuery(final QueryContext context, final TermRangeQuery termRangeQuery, final float boost) {
    final String field = getSearchField(context, termRangeQuery.getField());
    if (isSearchField(field)) {
        context.addFieldLog(field, termRangeQuery.toString(field));
        final RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(field);
        final BytesRef min = termRangeQuery.getLowerTerm();
        if (min != null) {
            if (termRangeQuery.includesLower()) {
                rangeQuery.gte(min.utf8ToString());
            } else {
                rangeQuery.gt(min.utf8ToString());
            }
        }
        final BytesRef max = termRangeQuery.getUpperTerm();
        if (max != null) {
            if (termRangeQuery.includesUpper()) {
                rangeQuery.lte(max.utf8ToString());
            } else {
                rangeQuery.lt(max.utf8ToString());
            }
        }
        rangeQuery.boost(boost);
        return rangeQuery;
    } else {
        final String origQuery = termRangeQuery.toString();
        context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b));
    }
}
 
Example #19
Source File: StringFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
    failIfNotIndexed();
    return new TermRangeQuery(name(),
        lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
        upperTerm == null ? null : indexedValueForSearch(upperTerm),
        includeLower, includeUpper);
}
 
Example #20
Source File: IgnoredFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query existsQuery(QueryShardContext context) {
    // This query is not performance sensitive, it only helps assess
    // quality of the data, so we may use a slow query. It shouldn't
    // be too slow in practice since the number of unique terms in this
    // field is bounded by the number of fields in the mappings.
    return new TermRangeQuery(name(), null, null, true, true);
}
 
Example #21
Source File: BooleanFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
    failIfNotIndexed();
    return new TermRangeQuery(name(),
        lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
        upperTerm == null ? null : indexedValueForSearch(upperTerm),
        includeLower, includeUpper);
}
 
Example #22
Source File: CommonQueryBuilderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeoShapeMatchDisJoint() throws Exception {
    Query query = convert("match(shape, 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))') using disjoint");
    assertThat(query, instanceOf(ConstantScoreQuery.class));
    Query booleanQuery = ((ConstantScoreQuery) query).getQuery();
    assertThat(booleanQuery, instanceOf(BooleanQuery.class));

    BooleanClause existsClause = ((BooleanQuery) booleanQuery).clauses().get(0);
    BooleanClause intersectsClause = ((BooleanQuery) booleanQuery).clauses().get(1);

    assertThat(existsClause.getQuery(), instanceOf(TermRangeQuery.class));
    assertThat(intersectsClause.getQuery(), instanceOf(IntersectsPrefixTreeQuery.class));
}
 
Example #23
Source File: CommonQueryBuilderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testRangeQueryForUid() throws Exception {
    Query query = convert("_uid > 'foo'");
    assertThat(query, instanceOf(TermRangeQuery.class));
    TermRangeQuery rangeQuery = (TermRangeQuery) query;
    assertThat(rangeQuery.getField(), is("_id"));
    assertThat(rangeQuery.getLowerTerm().utf8ToString(), is("foo"));
}
 
Example #24
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 #25
Source File: AlfrescoFieldType.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 #26
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 #27
Source File: HighlighterTest.java    From lucene-solr with 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 #28
Source File: HighlighterTest.java    From lucene-solr with 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 #29
Source File: CollationTestBase.java    From lucene-solr with 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 #30
Source File: ICUCollationField.java    From lucene-solr with 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);
  }
}