一、 package com.lucene.demo; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import java.io.IOException; import java.io.StringReader; import java.nio.file.Paths; public class Test01 { public static void main(String[] args) { addDoc(); //upDoc(); //delDoc(); search(); } public static void addDoc(){ try { Analyzer analyzer = new SmartChineseAnalyzer(); Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("id", "001", Field.Store.YES)); doc.add(new TextField("name","中华史", Field.Store.YES)); doc.add(new TextField("content","易中天的中华史", Field.Store.YES)); iwriter.addDocument(doc); iwriter.close(); System.out.println("*****************--新增--end**************************"); } catch (IOException e) { e.printStackTrace(); } } public static void search(){ try { Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); Analyzer analyzer = new SmartChineseAnalyzer(); DirectoryReader ireader = DirectoryReader.open(directory); IndexSearcher isearcher = new IndexSearcher(ireader); // Parse a simple query that searches for "text": QueryParser parser = new QueryParser("name", analyzer); Query query = parser.parse("中华"); ScoreDoc[] hits = isearcher.search(query, 10).scoreDocs; // Iterate through the results SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("", ""); Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query)); for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits[i].doc); TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(hitDoc.get("name"))); String content = highlighter.getBestFragment(tokenStream, hitDoc.get("name")); System.out.println("id="+hitDoc.get("id")+"|name="+hitDoc.get("name")+"|content="+content); } ireader.close(); directory.close(); System.out.println("*****************--查询--end**************************"); } catch (Exception e) { e.printStackTrace(); } } public static void upDoc(){ try { Analyzer analyzer = new SmartChineseAnalyzer(); Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("id", "001", Field.Store.YES)); doc.add(new TextField("name","中华史-近代史", Field.Store.YES)); doc.add(new TextField("content","易中天的中华史-近代史", Field.Store.YES)); Term term=new Term("id","001"); iwriter.updateDocument(term,doc); iwriter.close(); System.out.println("*****************--修改--end**************************"); } catch (IOException e) { e.printStackTrace(); } } public static void delDoc(){ try { Analyzer analyzer = new SmartChineseAnalyzer(); Directory directory = FSDirectory.open( Paths.get("F:/tools/lucene_db/users")); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter iwriter = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new TextField("id", "001", Field.Store.YES)); doc.add(new TextField("name","中华史-近代史", Field.Store.YES)); doc.add(new TextField("content","易中天的中华史-近代史", Field.Store.YES)); Term term=new Term("id","001"); iwriter.deleteDocuments(term); iwriter.close(); System.out.println("*****************--删除---end**************************"); } catch (IOException e) { e.printStackTrace(); } } } 二、pom.xml
4.0.0 com lucene-01 1.0-SNAPSHOT war lucene-01 Maven Webapp http://www.example.com UTF-8 1.7 1.7 junit junit 4.11 test org.apache.lucene lucene-core 8.0.0 org.apache.lucene lucene-queryparser 8.0.0 org.apache.lucene lucene-highlighter 8.0.0 org.apache.lucene lucene-analyzers-common 8.0.0 org.apache.lucene lucene-analyzers 3.6.2 org.apache.lucene lucene-queries 8.0.0 org.apache.lucene lucene-memory 8.0.0 org.apache.lucene lucene-analyzers-smartcn 8.0.0 lucene-01 maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2