Java Code Examples for org.apache.lucene.document.SortedNumericDocValuesField#newSlowRangeQuery()

The following examples show how to use org.apache.lucene.document.SortedNumericDocValuesField#newSlowRangeQuery() . 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: TestDocValuesQueries.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testToString() {
  Query q1 = SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5);
  assertEquals("foo:[3 TO 5]", q1.toString());
  assertEquals("[3 TO 5]", q1.toString("foo"));
  assertEquals("foo:[3 TO 5]", q1.toString("bar"));

  Query q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true);
  assertEquals("foo:[[62 61 72] TO [62 61 7a]]", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), false, true);
  assertEquals("foo:{[62 61 72] TO [62 61 7a]]", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), false, false);
  assertEquals("foo:{[62 61 72] TO [62 61 7a]}", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), null, true, true);
  assertEquals("foo:[[62 61 72] TO *}", q2.toString());
  q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", null, new BytesRef("baz"), true, true);
  assertEquals("foo:{* TO [62 61 7a]]", q2.toString());
  assertEquals("{* TO [62 61 7a]]", q2.toString("foo"));
  assertEquals("foo:{* TO [62 61 7a]]", q2.toString("bar"));
}
 
Example 2
Source File: TestDocValuesQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testEquals() {
  Query q1 = SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5);
  QueryUtils.checkEqual(q1, SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 5));
  QueryUtils.checkUnequal(q1, SortedNumericDocValuesField.newSlowRangeQuery("foo", 3, 6));
  QueryUtils.checkUnequal(q1, SortedNumericDocValuesField.newSlowRangeQuery("foo", 4, 5));
  QueryUtils.checkUnequal(q1, SortedNumericDocValuesField.newSlowRangeQuery("bar", 3, 5));

  Query q2 = SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true);
  QueryUtils.checkEqual(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("baz"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("baz"), new BytesRef("baz"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("foo", new BytesRef("bar"), new BytesRef("bar"), true, true));
  QueryUtils.checkUnequal(q2, SortedSetDocValuesField.newSlowRangeQuery("quux", new BytesRef("bar"), new BytesRef("baz"), true, true));
}
 
Example 3
Source File: TestDocValuesQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSortedNumericNPE() throws IOException {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  double[] nums = {-1.7147449030215377E-208, -1.6887024655302576E-11, 1.534911516604164E113, 0.0,
      2.6947996404505155E-166, -2.649722021970773E306, 6.138239235731689E-198, 2.3967090122610808E111};
  for (int i = 0; i < nums.length; ++i) {
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("dv", NumericUtils.doubleToSortableLong(nums[i])));
    iw.addDocument(doc);
  }
  iw.commit();
  final IndexReader reader = iw.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  iw.close();

  final long lo = NumericUtils.doubleToSortableLong(8.701032080293731E-226);
  final long hi = NumericUtils.doubleToSortableLong(2.0801416404385346E-41);
  
  Query query = SortedNumericDocValuesField.newSlowRangeQuery("dv", lo, hi);
  // TODO: assert expected matches
  searcher.search(query, searcher.reader.maxDoc(), Sort.INDEXORDER);

  // swap order, should still work
  query = SortedNumericDocValuesField.newSlowRangeQuery("dv", hi, lo);
  // TODO: assert expected matches
  searcher.search(query, searcher.reader.maxDoc(), Sort.INDEXORDER);
  
  reader.close();
  dir.close();
}
 
Example 4
Source File: NumericFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Query numericDocValuesRangeQuery(
    String field,
    Number lowerValue, Number upperValue,
    boolean lowerInclusive, boolean upperInclusive,
    boolean multiValued) {

  long actualLowerValue = Long.MIN_VALUE;
  if (lowerValue != null) {
    actualLowerValue = lowerValue.longValue();
    if (lowerInclusive == false) {
      if (actualLowerValue == Long.MAX_VALUE) {
        return new MatchNoDocsQuery();
      }
      ++actualLowerValue;
    }
  }

  long actualUpperValue = Long.MAX_VALUE;
  if (upperValue != null) {
    actualUpperValue = upperValue.longValue();
    if (upperInclusive == false) {
      if (actualUpperValue == Long.MIN_VALUE) {
        return new MatchNoDocsQuery();
      }
      --actualUpperValue;
    }
  }
  if (multiValued) {
    // In multiValued case use SortedNumericDocValuesField, this won't work for Trie*Fields wince they use BinaryDV in the multiValue case
    return SortedNumericDocValuesField.newSlowRangeQuery(field, actualLowerValue, actualUpperValue);
  } else {
    return NumericDocValuesField.newSlowRangeQuery(field, actualLowerValue, actualUpperValue);
  }
}
 
Example 5
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    float l = Float.NEGATIVE_INFINITY;
    float u = Float.POSITIVE_INFINITY;
    if (lowerTerm != null) {
        l = parse(lowerTerm, false);
        if (includeLower == false) {
            l = FloatPoint.nextUp(l);
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, false);
        if (includeUpper == false) {
            u = FloatPoint.nextDown(u);
        }
    }
    Query query = FloatPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field,
                NumericUtils.floatToSortableInt(l),
                NumericUtils.floatToSortableInt(u));
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 6
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    double l = Double.NEGATIVE_INFINITY;
    double u = Double.POSITIVE_INFINITY;
    if (lowerTerm != null) {
        l = parse(lowerTerm, false);
        if (includeLower == false) {
            l = DoublePoint.nextUp(l);
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, false);
        if (includeUpper == false) {
            u = DoublePoint.nextDown(u);
        }
    }
    Query query = DoublePoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field,
                NumericUtils.doubleToSortableLong(l),
                NumericUtils.doubleToSortableLong(u));
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 7
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    int l = Integer.MIN_VALUE;
    int u = Integer.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm, true);
        // if the lower bound is decimal:
        // - if the bound is positive then we increment it:
        //      if lowerTerm=1.5 then the (inclusive) bound becomes 2
        // - if the bound is negative then we leave it as is:
        //      if lowerTerm=-1.5 then the (inclusive) bound becomes -1 due to the call to longValue
        boolean lowerTermHasDecimalPart = hasDecimalPart(lowerTerm);
        if ((lowerTermHasDecimalPart == false && includeLower == false) ||
                (lowerTermHasDecimalPart && signum(lowerTerm) > 0)) {
            if (l == Integer.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, true);
        boolean upperTermHasDecimalPart = hasDecimalPart(upperTerm);
        if ((upperTermHasDecimalPart == false && includeUpper == false) ||
                (upperTermHasDecimalPart && signum(upperTerm) < 0)) {
            if (u == Integer.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    Query query = IntPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 8
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(String field, Object lowerTerm, Object upperTerm,
                 boolean includeLower, boolean includeUpper,
                 boolean hasDocValues) {
    long l = Long.MIN_VALUE;
    long u = Long.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm, true);
        // if the lower bound is decimal:
        // - if the bound is positive then we increment it:
        //      if lowerTerm=1.5 then the (inclusive) bound becomes 2
        // - if the bound is negative then we leave it as is:
        //      if lowerTerm=-1.5 then the (inclusive) bound becomes -1 due to the call to longValue
        boolean lowerTermHasDecimalPart = hasDecimalPart(lowerTerm);
        if ((lowerTermHasDecimalPart == false && includeLower == false) ||
                (lowerTermHasDecimalPart && signum(lowerTerm) > 0)) {
            if (l == Long.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm, true);
        boolean upperTermHasDecimalPart = hasDecimalPart(upperTerm);
        if ((upperTermHasDecimalPart == false && includeUpper == false) ||
                (upperTermHasDecimalPart && signum(upperTerm) < 0)) {
            if (u == Long.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(field, l, u);
    if (hasDocValues) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 9
Source File: DateFieldMapper.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,
                        ShapeRelation relation,
                        @Nullable DateTimeZone timeZone,
                        QueryShardContext context) {
    failIfNotIndexed();
    if (relation == ShapeRelation.DISJOINT) {
        throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() +
                "] does not support DISJOINT ranges");
    }
    long l, u;
    if (lowerTerm == null) {
        l = Long.MIN_VALUE;
    } else {
        l = (Long) lowerTerm;
        if (includeLower == false) {
            ++l;
        }
    }
    if (upperTerm == null) {
        u = Long.MAX_VALUE;
    } else {
        u = (Long) upperTerm;
        if (includeUpper == false) {
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(name(), l, u);
    if (hasDocValues()) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example 10
Source File: TestIndexSortSortedNumericDocValuesRangeQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private Query createQuery(String field, long lowerValue, long upperValue) {
  Query fallbackQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, lowerValue, upperValue);
  return new IndexSortSortedNumericDocValuesRangeQuery(field, lowerValue, upperValue, fallbackQuery);
}
 
Example 11
Source File: TestDocValuesQueries.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void doTestDuelPointRangeNumericRangeQuery(boolean sortedNumeric, int maxValuesPerDoc) throws IOException {
  final int iters = atLeast(10);
  for (int iter = 0; iter < iters; ++iter) {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(100);
    for (int i = 0; i < numDocs; ++i) {
      Document doc = new Document();
      final int numValues = TestUtil.nextInt(random(), 0, maxValuesPerDoc);
      for (int j = 0; j < numValues; ++j) {
        final long value = TestUtil.nextLong(random(), -100, 10000);
        if (sortedNumeric) {
          doc.add(new SortedNumericDocValuesField("dv", value));
        } else {
          doc.add(new NumericDocValuesField("dv", value));
        }
        doc.add(new LongPoint("idx", value));
      }
      iw.addDocument(doc);
    }
    if (random().nextBoolean()) {
      iw.deleteDocuments(LongPoint.newRangeQuery("idx", 0L, 10L));
    }
    final IndexReader reader = iw.getReader();
    final IndexSearcher searcher = newSearcher(reader, false);
    iw.close();

    for (int i = 0; i < 100; ++i) {
      final long min = random().nextBoolean() ? Long.MIN_VALUE : TestUtil.nextLong(random(), -100, 10000);
      final long max = random().nextBoolean() ? Long.MAX_VALUE : TestUtil.nextLong(random(), -100, 10000);
      final Query q1 = LongPoint.newRangeQuery("idx", min, max);
      final Query q2;
      if (sortedNumeric) {
        q2 = SortedNumericDocValuesField.newSlowRangeQuery("dv", min, max);
      } else {
        q2 = NumericDocValuesField.newSlowRangeQuery("dv", min, max);
      }
      assertSameMatches(searcher, q1, q2, false);
    }

    reader.close();
    dir.close();
  }
}
 
Example 12
Source File: TestLRUQueryCache.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testSkipCachingForRangeQuery() throws IOException {
  Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document doc1 = new Document();
  doc1.add(new StringField("name", "tom", Store.YES));
  doc1.add(new LongPoint("age", 15));
  doc1.add(new SortedNumericDocValuesField("age", 15));
  Document doc2 = new Document();
  doc2.add(new StringField("name", "alice", Store.YES));
  doc2.add(new LongPoint("age", 20));
  doc2.add(new SortedNumericDocValuesField("age", 20));
  w.addDocuments(Arrays.asList(doc1, doc2));
  final IndexReader reader = w.getReader();
  final IndexSearcher searcher = newSearcher(reader);
  searcher.setQueryCachingPolicy(ALWAYS_CACHE);
  w.close();

  // lead cost is 1, cost of subQuery1 is 1, cost of subQuery2 is 2
  BooleanQuery.Builder bq = new BooleanQuery.Builder();
  TermQuery subQuery1 = new TermQuery(new Term("name", "tom"));
  IndexOrDocValuesQuery subQuery2 = new IndexOrDocValuesQuery(
      LongPoint.newRangeQuery("age", 10, 30),
      SortedNumericDocValuesField.newSlowRangeQuery("age", 10, 30));
  BooleanQuery query = bq.add(subQuery1, Occur.FILTER).add(subQuery2, Occur.FILTER).build();
  Set<Query> cacheSet = new HashSet<>();

  // only term query is cached
  final LRUQueryCache partCache = new LRUQueryCache(1000000, 10000000, context -> true, 1);
  searcher.setQueryCache(partCache);
  searcher.search(query, 1);
  cacheSet.add(subQuery1);
  assertEquals(cacheSet, new HashSet<>(partCache.cachedQueries()));

  // both queries are cached
  final LRUQueryCache allCache = new LRUQueryCache(1000000, 10000000, context -> true, Float.POSITIVE_INFINITY);
  searcher.setQueryCache(allCache);
  searcher.search(query, 1);
  cacheSet.add(subQuery2);
  assertEquals(cacheSet, new HashSet<>(allCache.cachedQueries()));

  reader.close();
  dir.close();
}