1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.*; import org.apache.lucene.index.*; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory;
public class LuceneExample { private Directory directory; private StandardAnalyzer analyzer; public LuceneExample() { directory = new RAMDirectory(); analyzer = new StandardAnalyzer(); } public void createIndex() throws Exception { IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(directory, config); addDocument(writer, "1", "Apache Lucene搜索引擎", "Lucene是一个高性能的全文搜索引擎库"); addDocument(writer, "2", "Apache Solr搜索平台", "Solr是基于Lucene构建的企业级搜索平台"); addDocument(writer, "3", "Elasticsearch分布式搜索", "Elasticsearch是分布式的RESTful搜索引擎"); addDocument(writer, "4", "Java编程语言", "Java是面向对象的编程语言"); writer.close(); System.out.println("索引创建完成"); } private void addDocument(IndexWriter writer, String id, String title, String content) throws Exception { Document doc = new Document(); doc.add(new StringField("id", id, Field.Store.YES)); doc.add(new TextField("title", title, Field.Store.YES)); doc.add(new TextField("content", content, Field.Store.YES)); doc.add(new IntPoint("length", content.length())); doc.add(new StoredField("length", content.length())); doc.add(new SortedDocValuesField("title_sort", new BytesRef(title))); writer.addDocument(doc); } public void search(String queryString) throws Exception { IndexReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser("content", analyzer); Query query = parser.parse(queryString); System.out.println("查询: " + query.toString()); TopDocs topDocs = searcher.search(query, 10); System.out.println("找到 " + topDocs.totalHits + " 个结果:"); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); float score = scoreDoc.score; System.out.printf("评分: %.3f, ID: %s, 标题: %s%n", score, doc.get("id"), doc.get("title")); } reader.close(); } public void complexSearch() throws Exception { IndexReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); TermQuery mustQuery = new TermQuery(new Term("content", "搜索")); booleanQuery.add(mustQuery, BooleanClause.Occur.MUST); TermQuery shouldQuery = new TermQuery(new Term("title", "Apache")); booleanQuery.add(shouldQuery, BooleanClause.Occur.SHOULD); TermQuery mustNotQuery = new TermQuery(new Term("content", "Java")); booleanQuery.add(mustNotQuery, BooleanClause.Occur.MUST_NOT); Query query = booleanQuery.build(); Sort sort = new Sort(new SortField("title_sort", SortField.Type.STRING)); TopDocs topDocs = searcher.search(query, 10, sort); System.out.println("复杂查询结果:"); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); System.out.println("标题: " + doc.get("title")); } reader.close(); } public static void main(String[] args) throws Exception { LuceneExample example = new LuceneExample(); example.createIndex(); example.search("搜索引擎"); System.out.println("\n" + "=".repeat(50) + "\n"); example.complexSearch(); } }
|