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

The following examples show how to use org.apache.lucene.document.IntPoint#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: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDimensionalRangeQuery() throws Exception {
    // doesn't currently highlight, but make sure it doesn't cause exception either
    query = IntPoint.newRangeQuery(NUMERIC_FIELD_NAME, 2, 6);
    searcher = newSearcher(reader);
    hits = searcher.search(query, 100);
    int maxNumFragmentsRequired = 2;

    QueryScorer scorer = new QueryScorer(query, FIELD_NAME);
    Highlighter highlighter = new Highlighter(this, scorer);
    
    for (int i = 0; i < hits.totalHits.value; i++) {
      String text = searcher.doc(hits.scoreDocs[i].doc).getField(NUMERIC_FIELD_NAME).numericValue().toString();
      TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, text);

      highlighter.setTextFragmenter(new SimpleFragmenter(40));

//      String result = 
        highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired,"...");
      //if (VERBOSE) System.out.println("\t" + result);
    }


  }
 
Example 2
Source File: IntPointField.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) {
  int actualMin, actualMax;
  if (min == null) {
    actualMin = Integer.MIN_VALUE;
  } else {
    actualMin = parseIntFromUser(field.getName(), min);
    if (!minInclusive) {
      if (actualMin == Integer.MAX_VALUE) return new MatchNoDocsQuery();
      actualMin++;
    }
  }
  if (max == null) {
    actualMax = Integer.MAX_VALUE;
  } else {
    actualMax = parseIntFromUser(field.getName(), max);
    if (!maxInclusive) {
      if (actualMax == Integer.MIN_VALUE) return new MatchNoDocsQuery();
      actualMax--;
    }
  }
  return IntPoint.newRangeQuery(field.getName(), actualMin, actualMax);
}
 
Example 3
Source File: IntQuery.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");
    }
    int n2, x2;
    if (n == null || "".equals(n)) {
        n2 = Integer.MIN_VALUE;
    } else {
        n2 = Synt.asInt(n);
        if (!l) {
            n2 = n2 + 1;
        }
    }
    if (x == null || "".equals(x)) {
        x2 = Integer.MAX_VALUE;
    } else {
        x2 = Synt.asInt(x);
        if (!g) {
            x2 = x2 - 1;
        }
    }
    Query   q2 = IntPoint.newRangeQuery("@"+k, n2, x2);
    return  q2;
}
 
Example 4
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Query createIntRangeQuery(final String name, final Object value,
        final ConditionType type, final boolean minInclusive, final boolean maxInclusive) {
    final Integer intValue = Integer.valueOf(value.toString());
    Integer min = getMin(type, intValue);
    if (min == null) {
        min = Integer.MIN_VALUE;
    } else if (!minInclusive) {
        min = Math.addExact(min, 1);
    }

    Integer max = getMax(type, intValue);
    if (max == null) {
        max = Integer.MAX_VALUE;
    } else if (!maxInclusive) {
        max = Math.addExact(max, -1);
    }

    return IntPoint.newRangeQuery(name, min, max);
}
 
Example 5
Source File: LuceneQueryTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testPointRangeQuery() throws Exception {
    // 范围查询
    Query rangeQuery = IntPoint.newRangeQuery("id", 501, 1000);
    TopDocs search = searcher.search(rangeQuery, 1000);
    Assert.assertEquals(500, search.totalHits.value);
}
 
Example 6
Source File: LuceneQueryConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private Query toRangeQuery(RangeInt range) {
  return IntPoint.newRangeQuery(
      range.getField(),
      range.hasMin() ?
        range.getMinInclusive() ? range.getMin()
        : (range.getMin() + 1)
      : -Integer.MAX_VALUE,
      range.hasMax() ?
        range.getMaxInclusive() ? range.getMax()
        : (range.getMax() - 1)
      : Integer.MAX_VALUE );
}
 
Example 7
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 8
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testRangeOptimizesIfAllPointsMatch() throws IOException {
  final int numDims = TestUtil.nextInt(random(), 1, 3);
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  int[] value = new int[numDims];
  for (int i = 0; i < numDims; ++i) {
    value[i] = TestUtil.nextInt(random(), 1, 10);
  }
  doc.add(new IntPoint("point", value));
  w.addDocument(doc);
  IndexReader reader = w.getReader();
  IndexSearcher searcher = new IndexSearcher(reader);
  searcher.setQueryCache(null);
  int[] lowerBound = new int[numDims];
  int[] upperBound = new int[numDims];
  for (int i = 0; i < numDims; ++i) {
    lowerBound[i] = value[i] - random().nextInt(1);
    upperBound[i] = value[i] + random().nextInt(1);
  }
  Query query = IntPoint.newRangeQuery("point", lowerBound, upperBound);
  Weight weight = searcher.createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1);
  Scorer scorer = weight.scorer(searcher.getIndexReader().leaves().get(0));
  assertEquals(DocIdSetIterator.all(1).getClass(), scorer.iterator().getClass());

  // When not all documents in the query have a value, the optimization is not applicable
  reader.close();
  w.addDocument(new Document());
  w.forceMerge(1);
  reader = w.getReader();
  searcher = new IndexSearcher(reader);
  searcher.setQueryCache(null);
  weight = searcher.createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1);
  scorer = weight.scorer(searcher.getIndexReader().leaves().get(0));
  assertFalse(DocIdSetIterator.all(1).getClass().equals(scorer.iterator().getClass()));

  reader.close();
  w.close();
  dir.close();
}
 
Example 9
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 10
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 11
Source File: LuceneQueryConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private Query toTermIntQuery(SearchQuery.TermInt term) {
  return IntPoint.newRangeQuery(
    term.getField(),
    term.getValue(),
    term.getValue());
}
 
Example 12
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();
}
 
Example 13
Source File: BasicStorageTest.java    From lumongo with Apache License 2.0 4 votes vote down vote up
@Test
public void test2Query() throws IOException, ParseException {
	IndexReader indexReader = DirectoryReader.open(directory);

	StandardAnalyzer analyzer = new StandardAnalyzer();
	QueryParser qp = new QueryParser("title", analyzer) {

		@Override
		protected Query getRangeQuery(final String fieldName, final String start, final String end, final boolean startInclusive,
				final boolean endInclusive) throws ParseException {

			if ("testIntField".equals(fieldName)) {
				int startInt = Integer.parseInt(start);
				int endInt = Integer.parseInt(end);
				if (!startInclusive) {
					startInt += 1;
				}
				if (!endInclusive) {
					endInt -= 1;
				}
				return IntPoint.newRangeQuery(fieldName, startInt, endInt);

			}

			// return default
			return super.getRangeQuery(fieldName, start, end, startInclusive, endInclusive);

		}

		@Override
		protected Query newTermQuery(org.apache.lucene.index.Term term) {
			String field = term.field();
			String text = term.text();
			if ("testIntField".equals(field)) {
				int value = Integer.parseInt(text);
				return IntPoint.newExactQuery(field, value);
			}
			return super.newTermQuery(term);
		}
	};
	qp.setAllowLeadingWildcard(true);

	int hits;

	hits = runQuery(indexReader, qp, "java", 10);
	assertEquals("Expected 2 hits", 2, hits);
	hits = runQuery(indexReader, qp, "perl", 10);
	assertEquals("Expected 0 hits", 0, hits);
	hits = runQuery(indexReader, qp, "treatment", 10);
	assertEquals("Expected 0 hits", 0, hits);
	hits = runQuery(indexReader, qp, "long", 10);
	assertEquals("Expected 2 hits", 2, hits);
	hits = runQuery(indexReader, qp, "MongoDB", 10);
	assertEquals("Expected 1 hit", 1, hits);
	hits = runQuery(indexReader, qp, "java AND awesome", 10);
	assertEquals("Expected 1 hit", 1, hits);
	hits = runQuery(indexReader, qp, "testIntField:1", 10);
	assertEquals("Expected 0 hits", 0, hits);
	hits = runQuery(indexReader, qp, "testIntField:3", 10);
	assertEquals("Expected 5 hits", 5, hits);
	hits = runQuery(indexReader, qp, "testIntField:[1 TO 10]", 10);
	assertEquals("Expected 5 hits", 5, hits);


	indexReader.close();
}