1 packagecom.empirefree.lucene;2 /**
3 *@authorEmpirefree 胡宇乔:4 *@version创建时间:2020年3月31日 下午5:45:395 */
6
7 importjava.io.IOException;8 importjava.io.StringReader;9 importjava.util.List;10 importjava.util.Scanner;11
12 importorg.apache.lucene.analysis.TokenStream;13 importorg.apache.lucene.document.Document;14 importorg.apache.lucene.document.Field;15 importorg.apache.lucene.document.TextField;16 importorg.apache.lucene.index.DirectoryReader;17 importorg.apache.lucene.index.IndexReader;18 importorg.apache.lucene.index.IndexWriter;19 importorg.apache.lucene.index.IndexWriterConfig;20 importorg.apache.lucene.index.IndexableField;21 importorg.apache.lucene.queryparser.classic.QueryParser;22 importorg.apache.lucene.search.IndexSearcher;23 importorg.apache.lucene.search.Query;24 importorg.apache.lucene.search.ScoreDoc;25 importorg.apache.lucene.search.highlight.Highlighter;26 importorg.apache.lucene.search.highlight.QueryScorer;27 importorg.apache.lucene.search.highlight.SimpleHTMLFormatter;28 importorg.apache.lucene.store.Directory;29 importorg.apache.lucene.store.RAMDirectory;30 importorg.wltea.analyzer.lucene.IKAnalyzer;31
32
33 public classTestLucene2 {34
35 private static Directory createIndex(IKAnalyzer analyzer) throwsIOException {36 Directory index = newRAMDirectory();37 IndexWriterConfig config = newIndexWriterConfig(analyzer);38 IndexWriter writer = newIndexWriter(index, config);39 String fileName = "140k_products.txt";40
41 //List products = ProductUtil.filelist(fileName);
42 List products =ProductUtil.mysqllist();43 int total =products.size();44 int count = 0;45 int per = 0;46 int oldPer = 0;47 for(Product p : products) {48 addDoc(writer, p);49 count++;50 per = count*100/total;51 if(per!=oldPer){52 oldPer =per;53 System.out.printf("索引中,总共要添加 %d 条记录,当前添加进度是: %d%% %n",total,per);54 }55 }56 writer.close();57 returnindex;58 }59
60 private static void addDoc(IndexWriter w, Product p) throwsIOException {61 Document doc = newDocument();62 //doc.add(new TextField("id", String.valueOf(p.getId()), Field.Store.YES));
63 doc.add(new TextField("name", p.getName(), Field.Store.YES));64 //doc.add(new TextField("category", p.getCategory(), Field.Store.YES));65 //doc.add(new TextField("price", String.valueOf(p.getPrice()), Field.Store.YES));66 //doc.add(new TextField("place", p.getPlace(), Field.Store.YES));67 //doc.add(new TextField("code", p.getCode(), Field.Store.YES));
68 w.addDocument(doc);69 }70
71 private static void showSearchResults(IndexSearcher searcher, ScoreDoc[] hits, Query query, IKAnalyzer analyzer) throwsException {72 System.out.println("找到 " + hits.length + " 个命中.");73
74 SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("", "");75 Highlighter highlighter = new Highlighter(simpleHTMLFormatter, newQueryScorer(query));76
77 System.out.println("找到 " + hits.length + " 个命中.");78 System.out.println("序号\t匹配度得分\t结果");79 for (int i = 0; i < hits.length; ++i) {80 ScoreDoc scoreDoc=hits[i];81 int docId =scoreDoc.doc;82 Document d =searcher.doc(docId);83 List fields=d.getFields();84 System.out.print((i + 1) );85 System.out.print("\t" +scoreDoc.score);86 for(IndexableField f : fields) {87
88 if("name".equals(f.name())){89 TokenStream tokenStream = analyzer.tokenStream(f.name(), newStringReader(d.get(f.name())));90 String fieldContent =highlighter.getBestFragment(tokenStream, d.get(f.name()));91 System.out.print("\t"+fieldContent);92 System.out.print("?????????\n");93 }94 else{95 System.out.print("\t"+d.get(f.name()));96 }97 }98 System.out.println("
");99 }100 }101
102
103
104 public static void main(String[] args) throwsException {105 Scanner s = newScanner(System.in);106 System.out.print("请输入查询关键字:");107 String keyword =s.nextLine();108 System.out.println("当前关键字是:"+keyword);109 long startTime =System.currentTimeMillis();110 List products =ProductUtil.mysqllist2(keyword);111 long endTime =System.currentTimeMillis();112 System.out.println("Like程序运行时间:" + (endTime - startTime) + "ns");113
114 for(Product name : products){115 System.out.println(name.getName());116 }117
118 /******************************************************************************/
119 //1. 准备中文分词器
120 IKAnalyzer analyzer = newIKAnalyzer();121 //2. 索引
122 Directory index =createIndex(analyzer);123
124 //3. 查询器
125 s = newScanner(System.in);126 System.out.print("请输入查询关键字:");127 keyword =s.nextLine();128 System.out.println("当前关键字是:"+keyword);129 Query query = new QueryParser("name", analyzer).parse(keyword);130
131 startTime =System.currentTimeMillis();132 //4. 搜索
133 IndexReader reader =DirectoryReader.open(index);134 IndexSearcher searcher=newIndexSearcher(reader);135 int numberPerPage = 10;136 ScoreDoc[] hits =searcher.search(query, numberPerPage).scoreDocs;137 endTime =System.currentTimeMillis();138 System.out.println("Lucene程序运行时间:" + (endTime - startTime) + "ns");139
140 //5. 显示查询结果
141 showSearchResults(searcher, hits,query,analyzer);142 //6. 关闭查询
143 reader.close();144
145 ProductUtil.deleteconnection();146 }147 }