@Test public void testIntPointQuery() throws IOException { Directory directory = new RAMDirectory(); IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig( new StandardAnalyzer())); // 创建 Document Document document = new Document(); // 创建 Field 字段/域 Field intPoint = new IntPoint( "age" , 11 ); document.add(intPoint); intPoint = new StoredField( "age" , 11 ); document.add(intPoint); indexWriter.addDocument(document); Field intPoint1 = new IntPoint( "age" , 22 ); document = new Document(); document.add(intPoint1); intPoint1 = new StoredField( "age" , 22 ); document.add(intPoint1); indexWriter.addDocument(document); indexWriter.close(); IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(directory)); //精确查询 Query query = IntPoint.newExactQuery( "age" , 11 ); ScoreDoc[] scoreDocs = indexSearcher.search(query, 10 ).scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { System.out.println( "精确查询:" + indexSearcher.doc(scoreDoc.doc)); } //范围查询,不包含边界 query = IntPoint.newRangeQuery( "age" , Math.addExact( 11 , 1 ), Math.addExact( 22 , - 1 )); scoreDocs = indexSearcher.search(query, 10 ).scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { System.out.println( "不包含边界:" + indexSearcher.doc(scoreDoc.doc)); } //范围查询,包含边界 query = IntPoint.newRangeQuery( "age" , 11 , 22 ); scoreDocs = indexSearcher.search(query, 10 ).scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { System.out.println( "包含边界:" + indexSearcher.doc(scoreDoc.doc)); } //范围查询,左包含,右不包含 query = IntPoint.newRangeQuery( "age" , 11 , Math.addExact( 22 , - 1 )); scoreDocs = indexSearcher.search(query, 10 ).scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { System.out.println( "左包含右不包含:" + indexSearcher.doc(scoreDoc.doc)); } //集合查询 query = IntPoint.newSetQuery( "age" , 11 , 22 , 33 ); scoreDocs = indexSearcher.search(query, 10 ).scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { System.out.println( "集合查询:" + indexSearcher.doc(scoreDoc.doc)); } } |