Java Code Examples for org.apache.lucene.document.FloatPoint#newRangeQuery()

The following examples show how to use org.apache.lucene.document.FloatPoint#newRangeQuery() . 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: FloatPointField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query getPointRangeQuery(QParser parser, SchemaField field, String min, String max, boolean minInclusive,
    boolean maxInclusive) {
  float actualMin, actualMax;
  if (min == null) {
    actualMin = Float.NEGATIVE_INFINITY;
  } else {
    actualMin = parseFloatFromUser(field.getName(), min);
    if (!minInclusive) {
      if (actualMin == Float.POSITIVE_INFINITY) return new MatchNoDocsQuery();
      actualMin = FloatPoint.nextUp(actualMin);
    }
  }
  if (max == null) {
    actualMax = Float.POSITIVE_INFINITY;
  } else {
    actualMax = parseFloatFromUser(field.getName(), max);
    if (!maxInclusive) {
      if (actualMax == Float.NEGATIVE_INFINITY) return new MatchNoDocsQuery();
      actualMax = FloatPoint.nextDown(actualMax);
    }
  }
  return FloatPoint.newRangeQuery(field.getName(), actualMin, actualMax);
}
 
Example 2
Source File: FloatQuery.java    From HongsCORE with MIT License 6 votes vote down vote up
@Override
public Query whr(String k, Object n, Object x, boolean l, boolean g) {
    if (n == null && x == null) {
        throw new NullPointerException("Range for "+k+" must be number, but null");
    }
    float n2, x2;
    if (n == null || "".equals(n)) {
        n2 = Float.MIN_VALUE;
    } else {
        n2 = Synt.asFloat(n);
        if (!l) {
            n2 = FloatPoint.nextUp  (n2);
        }
    }
    if (x == null || "".equals(x)) {
        x2 = Float.MAX_VALUE;
    } else {
        x2 = Synt.asFloat(x);
        if (!g) {
            x2 = FloatPoint.nextDown(x2);
        }
    }
    Query   q2 = FloatPoint.newRangeQuery("@"+k, n2, x2);
    return  q2;
}
 
Example 3
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Query createFloatRangeQuery(final String name, final Object value,
        final ConditionType type, final boolean minInclusive, final boolean maxInclusive) {
    final Float floatValue = Float.valueOf(value.toString());
    Float min = getMin(type, floatValue);
    if (min == null) {
        min = Float.NEGATIVE_INFINITY;
    } else if (!minInclusive) {
        min = Math.nextUp(min);
    }

    Float max = getMax(type, floatValue);
    if (max == null) {
        max = Float.POSITIVE_INFINITY;
    } else if (!maxInclusive) {
        max = Math.nextDown(max);
    }

    return FloatPoint.newRangeQuery(name, min, max);
}
 
Example 4
Source File: LuceneQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private Query toRangeQuery(RangeFloat range) {
  return FloatPoint.newRangeQuery(
      range.getField(),
      range.hasMin() ?
        range.getMinInclusive() ? range.getMin()
        : Math.nextUp(range.getMin())
      : Float.NEGATIVE_INFINITY,
      range.hasMax() ?
        range.getMaxInclusive() ? range.getMax()
        : Math.nextAfter(range.getMax(), -Double.MAX_VALUE)
      : Float.POSITIVE_INFINITY );
}
 
Example 5
Source File: PointRangeQueryBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Query getQuery(Element e) throws ParserException {
  String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
  final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null);
  final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null);

  String type = DOMUtils.getAttribute(e, "type", "int");
  try {
    if (type.equalsIgnoreCase("int")) {
      return IntPoint.newRangeQuery(field,
          (lowerTerm == null ? Integer.MIN_VALUE : Integer.parseInt(lowerTerm)),
          (upperTerm == null ? Integer.MAX_VALUE : Integer.parseInt(upperTerm)));
    } else if (type.equalsIgnoreCase("long")) {
      return LongPoint.newRangeQuery(field,
          (lowerTerm == null ? Long.MIN_VALUE : Long.parseLong(lowerTerm)),
          (upperTerm == null ? Long.MAX_VALUE : Long.parseLong(upperTerm)));
    } else if (type.equalsIgnoreCase("double")) {
      return DoublePoint.newRangeQuery(field,
          (lowerTerm == null ? Double.NEGATIVE_INFINITY : Double.parseDouble(lowerTerm)),
          (upperTerm == null ? Double.POSITIVE_INFINITY : Double.parseDouble(upperTerm)));
    } else if (type.equalsIgnoreCase("float")) {
      return FloatPoint.newRangeQuery(field,
          (lowerTerm == null ? Float.NEGATIVE_INFINITY : Float.parseFloat(lowerTerm)),
          (upperTerm == null ? Float.POSITIVE_INFINITY : Float.parseFloat(upperTerm)));
    } else {
      throw new ParserException("type attribute must be one of: [long, int, double, float]");
    }
  } catch (NumberFormatException nfe) {
    throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
  }
}
 
Example 6
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPointRangeEquals() {
  Query q1, q2;

  q1 = IntPoint.newRangeQuery("a", 0, 1000);
  q2 = IntPoint.newRangeQuery("a", 0, 1000);
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  assertFalse(q1.equals(IntPoint.newRangeQuery("a", 1, 1000)));
  assertFalse(q1.equals(IntPoint.newRangeQuery("b", 0, 1000)));

  q1 = LongPoint.newRangeQuery("a", 0, 1000);
  q2 = LongPoint.newRangeQuery("a", 0, 1000);
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  assertFalse(q1.equals(LongPoint.newRangeQuery("a", 1, 1000)));

  q1 = FloatPoint.newRangeQuery("a", 0, 1000);
  q2 = FloatPoint.newRangeQuery("a", 0, 1000);
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  assertFalse(q1.equals(FloatPoint.newRangeQuery("a", 1, 1000)));

  q1 = DoublePoint.newRangeQuery("a", 0, 1000);
  q2 = DoublePoint.newRangeQuery("a", 0, 1000);
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  assertFalse(q1.equals(DoublePoint.newRangeQuery("a", 1, 1000)));

  byte[] zeros = new byte[5];
  byte[] ones = new byte[5];
  Arrays.fill(ones, (byte) 0xff);
  q1 = BinaryPoint.newRangeQuery("a", new byte[][] {zeros}, new byte[][] {ones});
  q2 = BinaryPoint.newRangeQuery("a", new byte[][] {zeros}, new byte[][] {ones});
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  byte[] other = ones.clone();
  other[2] = (byte) 5;
  assertFalse(q1.equals(BinaryPoint.newRangeQuery("a", new byte[][] {zeros}, new byte[][] {other})));
}
 
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) {
    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 8
Source File: LuceneQueryConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private Query toTermFloatQuery(SearchQuery.TermFloat term) {
  return FloatPoint.newRangeQuery(
    term.getField(),
    term.getValue(),
    term.getValue());
}
 
Example 9
Source File: TestMemoryIndexAgainstDirectory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testPointValuesMemoryIndexVsNormalIndex() throws Exception {
  int size = atLeast(12);

  List<Integer> randomValues = new ArrayList<>();

  Document doc = new Document();
  for (Integer randomInteger : random().ints(size).toArray()) {
    doc.add(new IntPoint("int", randomInteger));
    randomValues.add(randomInteger);
    doc.add(new LongPoint("long", randomInteger));
    doc.add(new FloatPoint("float", randomInteger));
    doc.add(new DoublePoint("double", randomInteger));
  }

  MockAnalyzer mockAnalyzer = new MockAnalyzer(random());
  MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc, mockAnalyzer);
  IndexSearcher memoryIndexSearcher = memoryIndex.createSearcher();

  Directory dir = newDirectory();
  IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(random(), mockAnalyzer));
  writer.addDocument(doc);
  writer.close();
  IndexReader controlIndexReader = DirectoryReader.open(dir);
  IndexSearcher controlIndexSearcher = new IndexSearcher(controlIndexReader);

  Supplier<Integer> valueSupplier = () -> randomValues.get(random().nextInt(randomValues.size()));
  Query[] queries = new Query[] {
      IntPoint.newExactQuery("int", valueSupplier.get()),
      LongPoint.newExactQuery("long", valueSupplier.get()),
      FloatPoint.newExactQuery("float", valueSupplier.get()),
      DoublePoint.newExactQuery("double", valueSupplier.get()),
      IntPoint.newSetQuery("int", valueSupplier.get(), valueSupplier.get()),
      LongPoint.newSetQuery("long", valueSupplier.get(), valueSupplier.get()),
      FloatPoint.newSetQuery("float", valueSupplier.get(), valueSupplier.get()),
      DoublePoint.newSetQuery("double", valueSupplier.get(), valueSupplier.get()),
      IntPoint.newRangeQuery("int", valueSupplier.get(), valueSupplier.get()),
      LongPoint.newRangeQuery("long", valueSupplier.get(), valueSupplier.get()),
      FloatPoint.newRangeQuery("float", valueSupplier.get(), valueSupplier.get()),
      DoublePoint.newRangeQuery("double", valueSupplier.get(), valueSupplier.get())
  };
  for (Query query : queries) {
    assertEquals(controlIndexSearcher.count(query), controlIndexSearcher.count(query));
  }

  memoryIndexSearcher.getIndexReader().close();
  controlIndexReader.close();
  dir.close();
}