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

The following examples show how to use org.apache.lucene.document.IntPoint#newExactQuery() . 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: LuceneQueryTestCase.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Test
public void testPointExactQuery() throws Exception {
    // 精确查询
    Query exactQuery = IntPoint.newExactQuery("id", 1);
    TopDocs search = searcher.search(exactQuery, 1000);
    Assert.assertEquals(1, search.totalHits.value);
}
 
Example 2
Source File: IntQuery.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");
    }
    int     n2 = Synt.asInt(v);
    Query   q2 = IntPoint.newExactQuery("@"+k, n2);
    return  q2;
}
 
Example 3
Source File: NumberFieldMapper.java    From crate with 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");
    }
    int v = parse(value, true);
    return IntPoint.newExactQuery(field, v);
}
 
Example 4
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 5
Source File: TestMatchesIterator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testPointQuery() throws IOException {
  IndexOrDocValuesQuery pointQuery = new IndexOrDocValuesQuery(
      IntPoint.newExactQuery(FIELD_POINT, 10),
      NumericDocValuesField.newSlowExactQuery(FIELD_POINT, 10)
  );
  Term t = new Term(FIELD_WITH_OFFSETS, "w1");
  Query query = new BooleanQuery.Builder()
      .add(new TermQuery(t), BooleanClause.Occur.MUST)
      .add(pointQuery, BooleanClause.Occur.MUST)
      .build();

  checkMatches(pointQuery, FIELD_WITH_OFFSETS, new int[][]{});

  checkMatches(query, FIELD_WITH_OFFSETS, new int[][]{
      { 0, 0, 0, 0, 2 },
      { 1, 0, 0, 0, 2 },
      { 2, 0, 0, 0, 2 },
      { 3, 0, 0, 0, 2, 2, 2, 6, 8 },
      { 4 }
  });

  pointQuery = new IndexOrDocValuesQuery(
      IntPoint.newExactQuery(FIELD_POINT, 11),
      NumericDocValuesField.newSlowExactQuery(FIELD_POINT, 11)
  );

  query = new BooleanQuery.Builder()
      .add(new TermQuery(t), BooleanClause.Occur.MUST)
      .add(pointQuery, BooleanClause.Occur.MUST)
      .build();
  checkMatches(query, FIELD_WITH_OFFSETS, new int[][]{});

  query = new BooleanQuery.Builder()
      .add(new TermQuery(t), BooleanClause.Occur.MUST)
      .add(pointQuery, BooleanClause.Occur.SHOULD)
      .build();
  checkMatches(query, FIELD_WITH_OFFSETS, new int[][]{
      {0, 0, 0, 0, 2},
      {1, 0, 0, 0, 2},
      {2, 0, 0, 0, 2},
      {3, 0, 0, 0, 2, 2, 2, 6, 8},
      {4}
  });
}
 
Example 6
Source File: TestPointQueries.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testPointExactEquals() {
  Query q1, q2;

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

  assertTrue(q1 instanceof PointRangeQuery && q2 instanceof PointRangeQuery);
  PointRangeQuery pq1 = (PointRangeQuery) q1;
  PointRangeQuery pq2 = (PointRangeQuery) q2;

  assertTrue(Arrays.equals(pq1.getLowerPoint(), pq2.getLowerPoint()));
  assertTrue(Arrays.equals(pq1.getUpperPoint(), pq2.getUpperPoint()));

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

  assertTrue(q1 instanceof PointRangeQuery && q2 instanceof PointRangeQuery);
  pq1 = (PointRangeQuery) q1;
  pq2 = (PointRangeQuery) q2;

  assertTrue(Arrays.equals(pq1.getLowerPoint(), pq2.getLowerPoint()));
  assertTrue(Arrays.equals(pq1.getUpperPoint(), pq2.getUpperPoint()));

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

  assertTrue(q1 instanceof PointRangeQuery && q2 instanceof PointRangeQuery);
  pq1 = (PointRangeQuery) q1;
  pq2 = (PointRangeQuery) q2;

  assertTrue(Arrays.equals(pq1.getLowerPoint(), pq2.getLowerPoint()));
  assertTrue(Arrays.equals(pq1.getUpperPoint(), pq2.getUpperPoint()));

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

  assertTrue(q1 instanceof PointRangeQuery && q2 instanceof PointRangeQuery);
  pq1 = (PointRangeQuery) q1;
  pq2 = (PointRangeQuery) q2;

  assertTrue(Arrays.equals(pq1.getLowerPoint(), pq2.getLowerPoint()));
  assertTrue(Arrays.equals(pq1.getUpperPoint(), pq2.getUpperPoint()));

  byte[] ones = new byte[5];
  Arrays.fill(ones, (byte) 0xff);
  q1 = BinaryPoint.newExactQuery("a", ones);
  q2 = BinaryPoint.newExactQuery("a", ones);
  assertEquals(q1, q2);
  assertEquals(q1.hashCode(), q2.hashCode());
  byte[] other = ones.clone();
  other[2] = (byte) 5;
  assertFalse(q1.equals(BinaryPoint.newExactQuery("a", other)));

  assertTrue(q1 instanceof PointRangeQuery && q2 instanceof PointRangeQuery);
  pq1 = (PointRangeQuery) q1;
  pq2 = (PointRangeQuery) q2;

  assertTrue(Arrays.equals(pq1.getLowerPoint(), pq2.getLowerPoint()));
  assertTrue(Arrays.equals(pq1.getUpperPoint(), pq2.getUpperPoint()));
}
 
Example 7
Source File: IntPointField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected Query getExactQuery(SchemaField field, String externalVal) {
  return IntPoint.newExactQuery(field.getName(), parseIntFromUser(field.getName(), externalVal));
}
 
Example 8
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();
}