Java Code Examples for org.apache.lucene.document.LongPoint

The following examples show how to use org.apache.lucene.document.LongPoint. 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: Java-Data-Science-Cookbook   Source File: IndexFiles.java    License: MIT License 6 votes vote down vote up
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
	try (InputStream stream = Files.newInputStream(file)) {
		Document doc = new Document();
		Field pathField = new StringField("path", file.toString(), Field.Store.YES);
		doc.add(pathField);
		doc.add(new LongPoint("modified", lastModified));
		doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));

		if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
			System.out.println("adding " + file);
			writer.addDocument(doc);
		} else {
			System.out.println("updating " + file);
			writer.updateDocument(new Term("path", file.toString()), doc);
		}
	}
}
 
Example 2
/**
 * Creates and indexes a transaction with a certain number of nodes.
 *
 * @param howManyTestNodes how many nodes we want to index.
 * @param acl the related ACL.
 * @param sampleTextContent a sample text content that will be used to assert nodes have been actually indexed.
 */
private static void createAndIndexTransactionWithSomeNodes(int howManyTestNodes, Acl acl, String sampleTextContent)
{
    try
    {
        Transaction txn = getTransaction(0, howManyTestNodes);
        Map.Entry<List<Node>, List<NodeMetaData>> data = TestDataProvider.nSampleNodesWithSampleContent(acl, txn, howManyTestNodes);

        indexTransaction(txn, data.getKey(), data.getValue(), range(0, howManyTestNodes).mapToObj(index -> sampleTextContent).collect(Collectors.toList()));

        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!TX")), BooleanClause.Occur.MUST));
        builder.add(new BooleanClause(LongPoint.newExactQuery(QueryConstants.FIELD_S_TXID, txn.getId()), BooleanClause.Occur.MUST));
        BooleanQuery waitForQuery = builder.build();

        waitForDocCountAllCores(waitForQuery, 1, MAX_WAIT_TIME);
    }
    catch (Exception exception)
    {
        throw new RuntimeException(exception);
    }
}
 
Example 3
Source Project: lucene-solr   Source File: DocMaker.java    License: Apache License 2.0 6 votes vote down vote up
public DocState(boolean reuseFields, FieldType ft, FieldType bodyFt) {

      this.reuseFields = reuseFields;
      
      if (reuseFields) {
        fields =  new HashMap<>();
        numericFields = new HashMap<>();
        
        // Initialize the map with the default fields.
        fields.put(BODY_FIELD, new Field(BODY_FIELD, "", bodyFt));
        fields.put(TITLE_FIELD, new Field(TITLE_FIELD, "", ft));
        fields.put(DATE_FIELD, new Field(DATE_FIELD, "", ft));
        fields.put(ID_FIELD, new StringField(ID_FIELD, "", Field.Store.YES));
        fields.put(NAME_FIELD, new Field(NAME_FIELD, "", ft));

        numericFields.put(DATE_MSEC_FIELD, new LongPoint(DATE_MSEC_FIELD, 0L));
        numericFields.put(TIME_SEC_FIELD, new IntPoint(TIME_SEC_FIELD, 0));
        
        doc = new Document();
      } else {
        numericFields = null;
        fields = null;
        doc = null;
      }
    }
 
Example 4
Source Project: lucene-solr   Source File: TestMemoryIndex.java    License: 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 5
Source Project: lucene-solr   Source File: TestMemoryIndex.java    License: 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 6
Source Project: lucene-solr   Source File: RangeFacetsExample.java    License: Apache License 2.0 6 votes vote down vote up
/** Build the example index. */
public void index() throws IOException {
  IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(
      new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));

  // Add documents with a fake timestamp, 1000 sec before
  // "now", 2000 sec before "now", ...:
  for(int i=0;i<100;i++) {
    Document doc = new Document();
    long then = nowSec - i * 1000;
    // Add as doc values field, so we can compute range facets:
    doc.add(new NumericDocValuesField("timestamp", then));
    // Add as numeric field so we can drill-down:
    doc.add(new LongPoint("timestamp", then));
    indexWriter.addDocument(doc);
  }

  // Open near-real-time searcher
  searcher = new IndexSearcher(DirectoryReader.open(indexWriter));
  indexWriter.close();
}
 
Example 7
Source Project: lucene-solr   Source File: TestPointQueries.java    License: Apache License 2.0 6 votes vote down vote up
public void testWrongNumBytes() throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig();
  iwc.setCodec(getCodec());
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
  Document doc = new Document();
  doc.add(new LongPoint("value", Long.MIN_VALUE));
  w.addDocument(doc);

  IndexReader r = w.getReader();

  // no wrapping, else the exc might happen in executor thread:
  IndexSearcher s = new IndexSearcher(r);
  byte[][] point = new byte[1][];
  point[0] = new byte[10];
  IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
    s.count(BinaryPoint.newRangeQuery("value", point, point));
  });
  assertEquals("field=\"value\" was indexed with bytesPerDim=8 but this query has bytesPerDim=10", expected.getMessage());

  IOUtils.close(r, w, dir);
}
 
Example 8
Source Project: lucene-solr   Source File: TestPointQueries.java    License: 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 9
Source Project: lucene-solr   Source File: TestPointQueries.java    License: 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 10
Source Project: lucene-solr   Source File: TestPointQueries.java    License: 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 11
Source Project: lucene-solr   Source File: PointMerger.java    License: 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?
  long v = LongPoint.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 12
Source Project: lucene-solr   Source File: DatePointField.java    License: 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) {
  long actualMin, actualMax;
  if (min == null) {
    actualMin = Long.MIN_VALUE;
  } else {
    actualMin = DateMathParser.parseMath(null, min).getTime();
    if (!minInclusive) {
      if (actualMin == Long.MAX_VALUE) return new MatchNoDocsQuery();
      actualMin++;
    }
  }
  if (max == null) {
    actualMax = Long.MAX_VALUE;
  } else {
    actualMax = DateMathParser.parseMath(null, max).getTime();
    if (!maxInclusive) {
      if (actualMax == Long.MIN_VALUE) return new MatchNoDocsQuery();
      actualMax--;
    }
  }
  return LongPoint.newRangeQuery(field.getName(), actualMin, actualMax);
}
 
Example 13
Source Project: lucene-solr   Source File: LongPointField.java    License: 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) {
  long actualMin, actualMax;
  if (min == null) {
    actualMin = Long.MIN_VALUE;
  } else {
    actualMin = parseLongFromUser(field.getName(), min);
    if (!minInclusive) {
      if (actualMin == Long.MAX_VALUE) return new MatchNoDocsQuery();
      actualMin++;
    }
  }
  if (max == null) {
    actualMax = Long.MAX_VALUE;
  } else {
    actualMax = parseLongFromUser(field.getName(), max);
    if (!maxInclusive) {
      if (actualMax == Long.MIN_VALUE) return new MatchNoDocsQuery();
      actualMax--;
    }
  }
  return LongPoint.newRangeQuery(field.getName(), actualMin, actualMax);
}
 
Example 14
Source Project: HongsCORE   Source File: LongQuery.java    License: 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");
    }
    long n2, x2;
    if (n == null || "".equals(n)) {
        n2 = Long.MIN_VALUE;
    } else {
        n2 = Synt.asLong(n);
        if (!l) {
            n2 = n2 + 1;
        }
    }
    if (x == null || "".equals(x)) {
        x2 = Long.MAX_VALUE;
    } else {
        x2 = Synt.asLong(x);
        if (!g) {
            x2 = x2 - 1;
        }
    }
    Query   q2 = LongPoint.newRangeQuery("@"+k, n2, x2);
    return  q2;
}
 
Example 15
Source Project: cxf   Source File: LuceneQueryVisitor.java    License: Apache License 2.0 6 votes vote down vote up
private Query createLongRangeQuery(final String name, final Object value,
        final ConditionType type, final boolean minInclusive, final boolean maxInclusive) {
    final Long longValue = Long.valueOf(value.toString());
    Long min = getMin(type, longValue);
    if (min == null) {
        min = Long.MIN_VALUE;
    } else if (!minInclusive) {
        min = Math.addExact(min, 1L);
    }

    Long max = getMax(type, longValue);
    if (max == null) {
        max = Long.MAX_VALUE;
    } else if (!maxInclusive) {
        max = Math.addExact(max, -1L);
    }
    return LongPoint.newRangeQuery(name, min, max);
}
 
Example 16
Source Project: jstarcraft-core   Source File: NumberIndexConverter.java    License: 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 17
Source Project: dremio-oss   Source File: LuceneQueryConverter.java    License: Apache License 2.0 5 votes vote down vote up
private Query toRangeQuery(RangeLong range) {
  return LongPoint.newRangeQuery(
      range.getField(),
      range.hasMin() ?
        range.getMinInclusive() ? range.getMin()
        : (range.getMin() + 1L)
      : -Long.MAX_VALUE,
      range.hasMax() ?
        range.getMaxInclusive() ? range.getMax()
        : (range.getMax() - 1L)
      : Long.MAX_VALUE );
}
 
Example 18
Source Project: crate   Source File: NumberFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(String field, Object value) {
    if (hasDecimalPart(value)) {
        return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
    }
    long v = parse(value, true);
    return LongPoint.newExactQuery(field, v);
}
 
Example 19
Source Project: lucene-solr   Source File: RangeFacetsExample.java    License: Apache License 2.0 5 votes vote down vote up
/** User drills down on the specified range. */
public TopDocs drillDown(LongRange range) throws IOException {

  // Passing no baseQuery means we drill down on all
  // documents ("browse only"):
  DrillDownQuery q = new DrillDownQuery(getConfig());

  q.add("timestamp", LongPoint.newRangeQuery("timestamp", range.min, range.max));
  return searcher.search(q, 10);
}
 
Example 20
Source Project: lucene-solr   Source File: LongRangeGroupSelectorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void addGroupField(Document document, int id) {
  if (rarely()) {
    return; // missing value
  }
  // numbers between 0 and 1000, groups are 100 wide from 100 to 900
  long value = random().nextInt(1000);
  document.add(new LongPoint("long", value));
  document.add(new NumericDocValuesField("long", value));
}
 
Example 21
Source Project: lucene-solr   Source File: LongRangeGroupSelectorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Query filterQuery(LongRange groupValue) {
  if (groupValue == null) {
    return new BooleanQuery.Builder()
        .add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER)
        .add(new DocValuesFieldExistsQuery("long"), BooleanClause.Occur.MUST_NOT)
        .build();
  }
  return LongPoint.newRangeQuery("long", groupValue.min, groupValue.max - 1);
}
 
Example 22
Source Project: lucene-solr   Source File: TestJoinUtil.java    License: 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 23
Source Project: lucene-solr   Source File: TestMultiRangeQueries.java    License: Apache License 2.0 5 votes vote down vote up
public void testLongRandomMultiRangeQuery() 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();
  long[] value = new long[numDims];
  for (int i = 0; i < numDims; ++i) {
    value[i] = TestUtil.nextLong(random(), 1, 10);
  }
  doc.add(new LongPoint("point", value));
  w.addDocument(doc);
  IndexReader reader = w.getReader();
  IndexSearcher searcher = new IndexSearcher(reader);
  searcher.setQueryCache(null);
  LongPointMultiRangeBuilder builder = new LongPointMultiRangeBuilder("point", numDims);
  for (int j = 0;j < numVals; j++) {
    long[] lowerBound = new long[numDims];
    long[] upperBound = new long[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 24
Source Project: lucene-solr   Source File: TestBooleanClauseWeightings.java    License: Apache License 2.0 5 votes vote down vote up
public void testExactClausesPreferred() {
  Query bq = new BooleanQuery.Builder()
      .add(LongPoint.newRangeQuery("field2", 1, 2), BooleanClause.Occur.MUST)
      .add(new BooleanQuery.Builder()
          .add(new TermQuery(new Term("field1", "term1")), BooleanClause.Occur.SHOULD)
          .add(new TermQuery(new Term("field1", "term2")), BooleanClause.Occur.SHOULD)
          .build(), BooleanClause.Occur.MUST)
      .build();
  QueryTree tree = treeBuilder.buildTree(bq, TermWeightor.DEFAULT);
  Set<Term> terms = new HashSet<>();
  tree.collectTerms((f, b) -> terms.add(new Term(f, b)));
  assertEquals(2, terms.size());
}
 
Example 25
Source Project: lucene-solr   Source File: PointRangeQueryBuilder.java    License: 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 26
Source Project: lucene-solr   Source File: TestPointQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public void testLongs() throws Exception {
  StandardQueryParser parser = new StandardQueryParser();
  Map<String,PointsConfig> pointsConfig = new HashMap<>();
  pointsConfig.put("longField", new PointsConfig(NumberFormat.getIntegerInstance(Locale.ROOT), Long.class));
  parser.setPointsConfigMap(pointsConfig);
  
  assertEquals(LongPoint.newRangeQuery("longField", 1, 3),
               parser.parse("longField:[1 TO 3]", "body"));
  assertEquals(LongPoint.newRangeQuery("longField", 1, 1),
               parser.parse("longField:1", "body"));
}
 
Example 27
Source Project: crate   Source File: NumberFieldMapper.java    License: 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 28
Source Project: lucene-solr   Source File: TestPointQueries.java    License: 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 29
Source Project: lucene-solr   Source File: TestPointQueries.java    License: 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 30
Source Project: crate   Source File: LuceneChangesSnapshot.java    License: Apache License 2.0 5 votes vote down vote up
private TopDocs searchOperations(ScoreDoc after) throws IOException {
    final Query rangeQuery = LongPoint.newRangeQuery(SeqNoFieldMapper.NAME, Math.max(fromSeqNo, lastSeenSeqNo), toSeqNo);
    final Sort sortedBySeqNoThenByTerm = new Sort(
        new SortField(SeqNoFieldMapper.NAME, SortField.Type.LONG),
        new SortField(SeqNoFieldMapper.PRIMARY_TERM_NAME, SortField.Type.LONG, true)
    );
    return indexSearcher.searchAfter(after, rangeQuery, searchBatchSize, sortedBySeqNoThenByTerm);
}