Lucene其他查询方式 博客分类: Lucene lucene
程序员文章站
2024-03-18 20:46:46
...
1. 指定项范围查询TermRangeQuery
2. 指定数字范围查询NumericRangeQuery
3. 指定字符串开头搜索PrefixQuery
4. 组合查询BooleanQuery
2. 指定数字范围查询NumericRangeQuery
3. 指定字符串开头搜索PrefixQuery
4. 组合查询BooleanQuery
New maven project -> Create a simple project -> Group Id: com.andrew.lucene Artifact Id: Lucene04 Version: 0.0.1-SNAPSHOT Packaging: jar
pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.andrew.lucene</groupId> <artifactId>Lucene04</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
Indexer.java代码 package com.andrew.lucene; import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.IntField; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class Indexer { private Integer ids[] = { 1, 2, 3 }; private String citys[] = { "aingdao", "banjing", "changhai" }; private String descs[] = { "Qingdao is b beautiful city.", "Nanjing is c city of culture.", "Shanghai is d bustling city." }; private Directory dir; // 获取IndexWriter实例 private IndexWriter getWriter() throws Exception { Analyzer analyzer = new StandardAnalyzer(); // 标准分词器 IndexWriterConfig iwc = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(dir, iwc); return writer; } // 生成索引 private void index(String indexDir) throws Exception { dir = FSDirectory.open(Paths.get(indexDir)); IndexWriter writer = getWriter(); for (int i = 0; i < ids.length; i++) { Document doc = new Document(); doc.add(new IntField("id", ids[i], Field.Store.YES)); doc.add(new StringField("city", citys[i], Field.Store.YES)); doc.add(new TextField("desc", descs[i], Field.Store.YES)); writer.addDocument(doc); // 添加文档 } writer.close(); } public static void main(String[] args) throws Exception { new Indexer().index("E:\\lucene5"); } }
SearchTest.java代码 package com.andrew.lucene; import java.nio.file.Paths; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.BytesRef; import org.junit.After; import org.junit.Before; import org.junit.Test; public class SearchTest { private Directory dir; private IndexReader reader; private IndexSearcher is; @Before public void setUp() throws Exception { dir = FSDirectory.open(Paths.get("E:\\lucene5")); reader = DirectoryReader.open(dir); is = new IndexSearcher(reader); } @After public void tearDown() throws Exception { reader.close(); } // 指定项范围搜索 @Test public void testTermRangeQuery() throws Exception { TermRangeQuery query = new TermRangeQuery("desc", new BytesRef("b".getBytes()), new BytesRef("c".getBytes()), true, true); TopDocs hits = is.search(query, 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } // 指定数字范围 @Test public void testNumericRangeQuery() throws Exception { NumericRangeQuery<Integer> query = NumericRangeQuery.newIntRange("id", 1, 2, true, true); TopDocs hits = is.search(query, 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } // 指定字符串开头搜索 @Test public void testPrefixQuery() throws Exception { PrefixQuery query = new PrefixQuery(new Term("city", "a")); TopDocs hits = is.search(query, 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } // 多条件查询 @Test public void testBooleanQuery() throws Exception { NumericRangeQuery<Integer> query1 = NumericRangeQuery.newIntRange("id", 1, 2, true, true); PrefixQuery query2 = new PrefixQuery(new Term("city", "a")); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); booleanQuery.add(query1, BooleanClause.Occur.MUST); booleanQuery.add(query2, BooleanClause.Occur.MUST); TopDocs hits = is.search(booleanQuery.build(), 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } } 运行结果: 1 aingdao Qingdao is b beautiful city. 2 banjing Nanjing is c city of culture. 3 changhai Shanghai is d bustling city. 1 aingdao Qingdao is b beautiful city. 2 banjing Nanjing is c city of culture. 1 aingdao Qingdao is b beautiful city. 1 aingdao Qingdao is b beautiful city.
上一篇: 以太坊区块 博客分类: 以太坊
下一篇: 以太坊帐户 博客分类: 以太坊
推荐阅读
-
Lucene其他查询方式 博客分类: Lucene lucene
-
lucene中的PackedInts源码解读-1 博客分类: lucene lucenePackedInts
-
Lucene5学习之Facet(续) 博客分类: Lucene LuceneFacet
-
Annotation-Driven Indexing and Searching with Lucene (转载) 博客分类: 工作 lucene.net
-
Lucene5学习之Facet简单入门 博客分类: Lucene LuceneFacet
-
lucene 入门(转) 博客分类: Lucene lucene搜索引擎Apache全文检索Eclipse
-
Lucene 维度统计(facet) 博客分类: lucene facet
-
Lucene简介与实现 博客分类: Lucene lucene
-
lucene中的docValue实现源码解读(一)——综述 博客分类: lucene lucenedocValue存储格式
-
Jive学习心得 博客分类: jive学习 设计模式lucene网络应用数据结构全文检索