org.apache.lucene.document.FloatPoint Java Examples

The following examples show how to use org.apache.lucene.document.FloatPoint. 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: TestPointQueries.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testPointInSetQueryToString() throws Exception {
  // int
  assertEquals("int:{-42 18}", IntPoint.newSetQuery("int", -42, 18).toString());

  // long
  assertEquals("long:{-42 18}", LongPoint.newSetQuery("long", -42L, 18L).toString());

  // float
  assertEquals("float:{-42.0 18.0}", FloatPoint.newSetQuery("float", -42.0f, 18.0f).toString());

  // double
  assertEquals("double:{-42.0 18.0}", DoublePoint.newSetQuery("double", -42.0, 18.0).toString());

  // binary
  assertEquals("bytes:{[12] [2a]}", BinaryPoint.newSetQuery("bytes", new byte[] {42}, new byte[] {18}).toString());
}
 
Example #3
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEmptyPointInSetQuery() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig();
  iwc.setCodec(getCodec());
  IndexWriter w = new IndexWriter(dir, iwc);

  Document doc = new Document();
  doc.add(new IntPoint("int", 17));
  doc.add(new LongPoint("long", 17L));
  doc.add(new FloatPoint("float", 17.0f));
  doc.add(new DoublePoint("double", 17.0));
  doc.add(new BinaryPoint("bytes", new byte[] {0, 17}));
  w.addDocument(doc);

  IndexReader r = DirectoryReader.open(w);
  IndexSearcher s = newSearcher(r, false);
  assertEquals(0, s.count(IntPoint.newSetQuery("int")));
  assertEquals(0, s.count(LongPoint.newSetQuery("long")));
  assertEquals(0, s.count(FloatPoint.newSetQuery("float")));
  assertEquals(0, s.count(DoublePoint.newSetQuery("double")));
  assertEquals(0, s.count(BinaryPoint.newSetQuery("bytes")));

  w.close();
  r.close();
  dir.close();
}
 
Example #4
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testToString() throws Exception {
  
  // ints
  assertEquals("field:[1 TO 2]", IntPoint.newRangeQuery("field", 1, 2).toString());
  assertEquals("field:[-2 TO 1]", IntPoint.newRangeQuery("field", -2, 1).toString());

  // longs
  assertEquals("field:[1099511627776 TO 2199023255552]", LongPoint.newRangeQuery("field", 1L<<40, 1L<<41).toString());
  assertEquals("field:[-5 TO 6]", LongPoint.newRangeQuery("field", -5L, 6L).toString());
  
  // floats
  assertEquals("field:[1.3 TO 2.5]", FloatPoint.newRangeQuery("field", 1.3F, 2.5F).toString());
  assertEquals("field:[-2.9 TO 1.0]", FloatPoint.newRangeQuery("field", -2.9F, 1.0F).toString());
  
  // doubles
  assertEquals("field:[1.3 TO 2.5]", DoublePoint.newRangeQuery("field", 1.3, 2.5).toString());
  assertEquals("field:[-2.9 TO 1.0]", DoublePoint.newRangeQuery("field", -2.9, 1.0).toString());
  
  // n-dimensional double
  assertEquals("field:[1.3 TO 2.5],[-2.9 TO 1.0]", DoublePoint.newRangeQuery("field", 
                                                                    new double[] { 1.3, -2.9 }, 
                                                                    new double[] { 2.5, 1.0 }).toString());

}
 
Example #5
Source File: PointMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(int docID, byte[] packedValue) throws IOException {
  // TODO: handle filter or deleted documents?
  float v = FloatPoint.decodeDimension(packedValue, 0);
  if (v < last) return;

  if (v == last && pos >= 0) {
    count[pos]++;
  } else {
    if (pos+1 < values.length) {
      last = v;
      ++pos;
      values[pos] = v;
      count[pos] = 1;
    } else {
      // a new value we don't have room for
      throw breakException;
    }
  }
}
 
Example #6
Source File: NumericFieldType.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected Query getRangeQueryForMultiValuedFloatDocValues(SchemaField sf, String min, String max, boolean minInclusive, boolean maxInclusive) {
  float minVal,maxVal;
  if (min == null) {
    minVal = Float.NEGATIVE_INFINITY;
  } else {
    minVal = parseFloatFromUser(sf.getName(), min);
    if (!minInclusive) {
      if (minVal == Float.POSITIVE_INFINITY) return new MatchNoDocsQuery();
      minVal = FloatPoint.nextUp(minVal);
    }
  }
  if (max == null) {
    maxVal = Float.POSITIVE_INFINITY;
  } else {
    maxVal = parseFloatFromUser(sf.getName(), max);
    if (!maxInclusive) {
      if (maxVal == Float.NEGATIVE_INFINITY) return new MatchNoDocsQuery();
      maxVal = FloatPoint.nextDown(maxVal);
    }
  }
  Long minBits = (long)NumericUtils.floatToSortableInt(minVal);
  Long maxBits = (long)NumericUtils.floatToSortableInt(maxVal);
  return numericDocValuesRangeQuery(sf.getName(), minBits, maxBits, true, true, true);
}
 
Example #7
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 #8
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 #9
Source File: TestMemoryIndex.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testMultiValuedPointsSortedCorrectly() throws Exception {
  Document doc = new Document();
  doc.add(new IntPoint("ints", 3));
  doc.add(new IntPoint("ints", 2));
  doc.add(new IntPoint("ints", 1));
  doc.add(new LongPoint("longs", 3L));
  doc.add(new LongPoint("longs", 2L));
  doc.add(new LongPoint("longs", 1L));
  doc.add(new FloatPoint("floats", 3F));
  doc.add(new FloatPoint("floats", 2F));
  doc.add(new FloatPoint("floats", 1F));
  doc.add(new DoublePoint("doubles", 3D));
  doc.add(new DoublePoint("doubles", 2D));
  doc.add(new DoublePoint("doubles", 1D));

  MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
  IndexSearcher s = mi.createSearcher();

  assertEquals(1, s.count(IntPoint.newSetQuery("ints", 2)));
  assertEquals(1, s.count(LongPoint.newSetQuery("longs", 2)));
  assertEquals(1, s.count(FloatPoint.newSetQuery("floats", 2)));
  assertEquals(1, s.count(DoublePoint.newSetQuery("doubles", 2)));
}
 
Example #10
Source File: TestMemoryIndex.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void test2DPoints() throws Exception {
  Document doc = new Document();
  doc.add(new IntPoint("ints", 0, -100));
  doc.add(new IntPoint("ints", 20, 20));
  doc.add(new IntPoint("ints", 100, -100));
  doc.add(new LongPoint("longs", 0L, -100L));
  doc.add(new LongPoint("longs", 20L, 20L));
  doc.add(new LongPoint("longs", 100L, -100L));
  doc.add(new FloatPoint("floats", 0F, -100F));
  doc.add(new FloatPoint("floats", 20F, 20F));
  doc.add(new FloatPoint("floats", 100F, -100F));
  doc.add(new DoublePoint("doubles", 0D, -100D));
  doc.add(new DoublePoint("doubles", 20D, 20D));
  doc.add(new DoublePoint("doubles", 100D, -100D));

  MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
  IndexSearcher s = mi.createSearcher();

  assertEquals(1, s.count(IntPoint.newRangeQuery("ints", new int[] {10, 10}, new int[] {30, 30})));
  assertEquals(1, s.count(LongPoint.newRangeQuery("longs", new long[] {10L, 10L}, new long[] {30L, 30L})));
  assertEquals(1, s.count(FloatPoint.newRangeQuery("floats", new float[] {10F, 10F}, new float[] {30F, 30F})));
  assertEquals(1, s.count(DoublePoint.newRangeQuery("doubles", new double[] {10D, 10D}, new double[] {30D, 30D})));
}
 
Example #11
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<Field> createFields(String name, Number value,
                                boolean indexed, boolean docValued, boolean stored) {
    List<Field> fields = new ArrayList<>();
    if (indexed) {
        fields.add(new FloatPoint(name, value.floatValue()));
    }
    if (docValued) {
        fields.add(new SortedNumericDocValuesField(name,
            NumericUtils.floatToSortableInt(value.floatValue())));
    }
    if (stored) {
        fields.add(new StoredField(name, value.floatValue()));
    }
    return fields;
}
 
Example #12
Source File: FloatQuery.java    From HongsCORE with MIT License 5 votes vote down vote up
@Override
public Query whr(String k, Object v) {
    if (v == null) {
        throw new NullPointerException("Query for "+k+" must be number, but null");
    }
    float   n2 = Synt.asFloat(v);
    Query   q2 = FloatPoint.newExactQuery("@"+k, n2);
    return  q2;
}
 
Example #13
Source File: TestPointFields.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWhiteboxCreateFields() throws Exception {
  String[] typeNames = new String[]{"i", "l", "f", "d", "dt"};
  @SuppressWarnings({"rawtypes"})
  Class<?>[] expectedClasses = new Class[]{IntPoint.class, LongPoint.class, FloatPoint.class, DoublePoint.class, LongPoint.class};
  
  Date dateToTest = new Date();
  Object[][] values = new Object[][] {
    {42, "42"},
    {42, "42"},
    {42.123, "42.123"},
    {12345.6789, "12345.6789"},
    {dateToTest, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT).format(dateToTest), "NOW"} // "NOW" won't be equal to the other dates
  };
  
  Set<String> typesTested = new HashSet<>();
  for (int i = 0; i < typeNames.length; i++) {
    for (String suffix:FIELD_SUFFIXES) {
      doWhiteboxCreateFields("whitebox_p_" + typeNames[i] + suffix, expectedClasses[i], values[i]);
      typesTested.add("*_p_" + typeNames[i] + suffix);
    }
  }
  Set<String> typesToTest = new HashSet<>();
  for (DynamicField dynField:h.getCore().getLatestSchema().getDynamicFields()) {
    if (dynField.getPrototype().getType() instanceof PointField) {
      typesToTest.add(dynField.getRegex());
    }
  }
  assertEquals("Missing types in the test", typesTested, typesToTest);
}
 
Example #14
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPointInSetEquals() {
  Query q1, q2;
  q1 = IntPoint.newSetQuery("a", 0, 1000, 17);
  q2 = IntPoint.newSetQuery("a", 17, 0, 1000);
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  assertFalse(q1.equals(IntPoint.newSetQuery("a", 1, 17, 1000)));
  assertFalse(q1.equals(IntPoint.newSetQuery("b", 0, 1000, 17)));

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

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

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

  byte[] zeros = new byte[5];
  byte[] ones = new byte[5];
  Arrays.fill(ones, (byte) 0xff);
  q1 = BinaryPoint.newSetQuery("a", new byte[][] {zeros, ones});
  q2 = BinaryPoint.newSetQuery("a", new byte[][] {zeros, ones});
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  byte[] other = ones.clone();
  other[2] = (byte) 5;
  assertFalse(q1.equals(BinaryPoint.newSetQuery("a", new byte[][] {zeros, other})));
}
 
Example #15
Source File: PointMerger.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public PointValues.Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
  float v = FloatPoint.decodeDimension(maxPackedValue, 0);
  if (v >= last) {
    return PointValues.Relation.CELL_CROSSES_QUERY;
  } else {
    return PointValues.Relation.CELL_OUTSIDE_QUERY;
  }
}
 
Example #16
Source File: TestPointValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testInvalidFloatPointUsage() throws Exception {
  FloatPoint field = new FloatPoint("field", 17, 42);

  expectThrows(IllegalArgumentException.class, () -> {
    field.setFloatValue(14);
  });

  expectThrows(IllegalStateException.class, () -> {
    field.numericValue();
  });
}
 
Example #17
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testNextDown() {
  assertTrue(Double.compare(-0d, DoublePoint.nextDown(0d)) == 0);
  assertTrue(Double.compare(-Double.MIN_VALUE, DoublePoint.nextDown(-0d)) == 0);
  assertTrue(Double.compare(Double.NEGATIVE_INFINITY, DoublePoint.nextDown(-Double.MAX_VALUE)) == 0);
  assertTrue(Double.compare(Double.NEGATIVE_INFINITY, DoublePoint.nextDown(Double.NEGATIVE_INFINITY)) == 0);
  assertTrue(Double.compare(Double.MAX_VALUE, DoublePoint.nextDown(Double.POSITIVE_INFINITY)) == 0);

  assertTrue(Float.compare(-0f, FloatPoint.nextDown(0f)) == 0);
  assertTrue(Float.compare(-Float.MIN_VALUE, FloatPoint.nextDown(-0f)) == 0);
  assertTrue(Float.compare(Float.NEGATIVE_INFINITY, FloatPoint.nextDown(-Float.MAX_VALUE)) == 0);
  assertTrue(Float.compare(Float.NEGATIVE_INFINITY, FloatPoint.nextDown(Float.NEGATIVE_INFINITY)) == 0);
  assertTrue(Float.compare(Float.MAX_VALUE, FloatPoint.nextDown(Float.POSITIVE_INFINITY)) == 0);
}
 
Example #18
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query termsQuery(String field, List<Object> values) {
    float[] v = new float[values.size()];
    for (int i = 0; i < values.size(); ++i) {
        v[i] = parse(values.get(i), false);
    }
    return FloatPoint.newSetQuery(field, v);
}
 
Example #19
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testNextUp() {
  assertTrue(Double.compare(0d, DoublePoint.nextUp(-0d)) == 0);
  assertTrue(Double.compare(Double.MIN_VALUE, DoublePoint.nextUp(0d)) == 0);
  assertTrue(Double.compare(Double.POSITIVE_INFINITY, DoublePoint.nextUp(Double.MAX_VALUE)) == 0);
  assertTrue(Double.compare(Double.POSITIVE_INFINITY, DoublePoint.nextUp(Double.POSITIVE_INFINITY)) == 0);
  assertTrue(Double.compare(-Double.MAX_VALUE, DoublePoint.nextUp(Double.NEGATIVE_INFINITY)) == 0);

  assertTrue(Float.compare(0f, FloatPoint.nextUp(-0f)) == 0);
  assertTrue(Float.compare(Float.MIN_VALUE, FloatPoint.nextUp(0f)) == 0);
  assertTrue(Float.compare(Float.POSITIVE_INFINITY, FloatPoint.nextUp(Float.MAX_VALUE)) == 0);
  assertTrue(Float.compare(Float.POSITIVE_INFINITY, FloatPoint.nextUp(Float.POSITIVE_INFINITY)) == 0);
  assertTrue(Float.compare(-Float.MAX_VALUE, FloatPoint.nextUp(Float.NEGATIVE_INFINITY)) == 0);
}
 
Example #20
Source File: NumberIndexConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneIndex annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (Byte.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, (byte) data));
        return indexables;
    }
    if (Short.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, (short) data));
        return indexables;
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        indexables.add(new IntPoint(path, (int) data));
        return indexables;
    }
    if (Long.class.isAssignableFrom(clazz)) {
        indexables.add(new LongPoint(path, (long) data));
        return indexables;
    }
    if (Float.class.isAssignableFrom(clazz)) {
        indexables.add(new FloatPoint(path, (float) data));
        return indexables;
    }
    if (Double.class.isAssignableFrom(clazz)) {
        indexables.add(new DoublePoint(path, (double) data));
        return indexables;
    }
    throw new StorageException();
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
Source File: TestMultiRangeQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFloatRandomMultiRangeQuery() throws IOException {
  final int numDims = TestUtil.nextInt(random(), 1, 3);
  final int numVals = TestUtil.nextInt(random(), 3, 8);
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  float[] value = new float[numDims];
  for (int i = 0; i < numDims; ++i) {
    value[i] = TestUtil.nextInt(random(), 1, 10);
  }
  doc.add(new FloatPoint("point", value));
  w.addDocument(doc);
  IndexReader reader = w.getReader();
  IndexSearcher searcher = new IndexSearcher(reader);
  searcher.setQueryCache(null);
  FloatPointMultiRangeBuilder builder = new FloatPointMultiRangeBuilder("point", numDims);
  for (int j = 0;j < numVals; j++) {
    float[] lowerBound = new float[numDims];
    float[] upperBound = new float[numDims];
    for (int i = 0; i < numDims; ++i) {
      lowerBound[i] = value[i] - random().nextInt(1);
      upperBound[i] = value[i] + random().nextInt(1);
    }
    builder.add(lowerBound, upperBound);
  }

  Query query = builder.build();
  searcher.search(query, Integer.MAX_VALUE);

  reader.close();
  w.close();
  dir.close();
}
 
Example #26
Source File: TestJoinUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void addLinkFields(final Random random, Document document, final String fieldName, String linkValue,
    boolean multipleValuesPerDocument, boolean globalOrdinalJoin) {
  document.add(newTextField(random, fieldName, linkValue, Field.Store.NO));

  final int linkInt = Integer.parseUnsignedInt(linkValue,16);
  document.add(new IntPoint(fieldName + "INT", linkInt));
  document.add(new FloatPoint(fieldName + "FLOAT", linkInt));

  final long linkLong = linkInt<<32 | linkInt;
  document.add(new LongPoint(fieldName + "LONG", linkLong));
  document.add(new DoublePoint(fieldName + "DOUBLE", linkLong));

  if (multipleValuesPerDocument) {
    document.add(new SortedSetDocValuesField(fieldName, new BytesRef(linkValue)));
    document.add(new SortedNumericDocValuesField(fieldName+ "INT", linkInt));
    document.add(new SortedNumericDocValuesField(fieldName+ "FLOAT", Float.floatToRawIntBits(linkInt)));
    document.add(new SortedNumericDocValuesField(fieldName+ "LONG", linkLong));
    document.add(new SortedNumericDocValuesField(fieldName+ "DOUBLE", Double.doubleToRawLongBits(linkLong)));
  } else {
    document.add(new SortedDocValuesField(fieldName, new BytesRef(linkValue)));
    document.add(new NumericDocValuesField(fieldName+ "INT", linkInt));
    document.add(new FloatDocValuesField(fieldName+ "FLOAT", linkInt));
    document.add(new NumericDocValuesField(fieldName+ "LONG", linkLong));
    document.add(new DoubleDocValuesField(fieldName+ "DOUBLE", linkLong));
  }
  if (globalOrdinalJoin) {
    document.add(new SortedDocValuesField("join_field", new BytesRef(linkValue)));
  }
}
 
Example #27
Source File: TestPointQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFloats() throws Exception {
  StandardQueryParser parser = new StandardQueryParser();
  Map<String,PointsConfig> pointsConfig = new HashMap<>();
  pointsConfig.put("floatField", new PointsConfig(NumberFormat.getNumberInstance(Locale.ROOT), Float.class));
  parser.setPointsConfigMap(pointsConfig);
  
  assertEquals(FloatPoint.newRangeQuery("floatField", 1.5F, 3.6F),
               parser.parse("floatField:[1.5 TO 3.6]", "body"));
  assertEquals(FloatPoint.newRangeQuery("floatField", 1.5F, 1.5F),
               parser.parse("floatField:1.5", "body"));
}
 
Example #28
Source File: TestFieldSortOptimizationSkipping.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFloatSortOptimization() throws IOException {
  final Directory dir = newDirectory();
  final IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig());
  final int numDocs = atLeast(10000);
  for (int i = 0; i < numDocs; ++i) {
    final Document doc = new Document();
    float f = 1f * i;
    doc.add(new FloatDocValuesField("my_field", f));
    doc.add(new FloatPoint("my_field", i));
    writer.addDocument(doc);
  }
  final IndexReader reader = DirectoryReader.open(writer);
  IndexSearcher searcher = new IndexSearcher(reader);
  final SortField sortField = new SortField("my_field", SortField.Type.FLOAT);
  final Sort sort = new Sort(sortField);
  final int numHits = 3;
  final int totalHitsThreshold = 3;

  { // simple sort
    final TopFieldCollector collector = TopFieldCollector.create(sort, numHits, null, totalHitsThreshold);
    searcher.search(new MatchAllDocsQuery(), collector);
    TopDocs topDocs = collector.topDocs();
    assertEquals(topDocs.scoreDocs.length, numHits);
    for (int i = 0; i < numHits; i++) {
      FieldDoc fieldDoc = (FieldDoc) topDocs.scoreDocs[i];
      assertEquals(1f * i, fieldDoc.fields[0]);
    }
    assertTrue(collector.isEarlyTerminated());
    assertTrue(topDocs.totalHits.value < numDocs);
  }

  writer.close();
  reader.close();
  dir.close();
}
 
Example #29
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Boxed methods for primitive types should behave the same as unboxed: just sugar */
public void testPointIntSetBoxed() throws Exception {
  assertEquals(IntPoint.newSetQuery("foo", 1, 2, 3), IntPoint.newSetQuery("foo", Arrays.asList(1, 2, 3)));
  assertEquals(FloatPoint.newSetQuery("foo", 1F, 2F, 3F), FloatPoint.newSetQuery("foo", Arrays.asList(1F, 2F, 3F)));
  assertEquals(LongPoint.newSetQuery("foo", 1L, 2L, 3L), LongPoint.newSetQuery("foo", Arrays.asList(1L, 2L, 3L)));
  assertEquals(DoublePoint.newSetQuery("foo", 1D, 2D, 3D), DoublePoint.newSetQuery("foo", Arrays.asList(1D, 2D, 3D)));
}
 
Example #30
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public Number parsePoint(byte[] value) {
    return FloatPoint.decodeDimension(value, 0);
}