依赖包
<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>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>5.3.1</version>
</dependency>
索引类
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
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[] = { "青岛", "南京", "上海" };
private String descs[] = { "青岛是一个美丽的城市。",
"南京是一个有文化的城市。南京是一个文化的城市南京,简称宁,是江苏省会,地处中国东部地区,长江下游,濒江近海。全市下辖11个区,总面积6597平方公里,2013年建成区面积752.83平方公里,常住人口818.78万,其中城镇人口659.1万人。[1-4] “江南佳丽地,金陵帝王州”,南京拥有着6000多年文明史、近2600年建城史和近500年的建都史,是中国四大古都之一,有“六朝古都”、“十朝都会”之称,是中华文明的重要发祥地,历史上曾数次庇佑华夏之正朔,长期是中国南方的政治、经济、文化中心,拥有厚重的文化底蕴和丰富的历史遗存。[5-7] 南京是国家重要的科教中心,自古以来就是一座崇文重教的城市,有“天下文枢”、“东南第一学”的美誉。截至2013年,南京有高等院校75所,其中211高校8所,仅次于北京上海;国家重点实验室25所、国家重点学科169个、两院院士83人,均居中国第三。[8-10] 。",
"上海是一个繁华的城市。" };
private Directory dir;
/**
* 获取IndexWriter实例
*/
private IndexWriter getWriter() throws Exception {
//Analyzer analyzer = new StandardAnalyzer();
SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(dir, iwc);
return writer;
}
/**
* 生成索引
*/
public 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) {
try {
new Indexer().index("D:\\Lucene");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
高亮查询
import java.io.StringReader;
import java.nio.file.Paths;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.TopDocs;
import org.apache.lucene.search.highlight.Fragmenter;
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.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.After;
import org.junit.Before;
public class SearchTest {
public static void search(String indexDir,String q) throws Exception{
Directory dir = FSDirectory.open(Paths.get(indexDir));
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher is = new IndexSearcher(reader);
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
QueryParser parser = new QueryParser("desc",analyzer);
Query query =parser.parse(q);
TopDocs hits = is.search(query, 10);
System.out.println("匹配 "+q+"查询到"+hits.totalHits+"个记录");
QueryScorer scorer = new QueryScorer(query);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
SimpleHTMLFormatter simpleHTMLFormatter = new
SimpleHTMLFormatter("<b><font color='red'>","</font></b>");
Highlighter highlighter = new Highlighter(simpleHTMLFormatter,scorer);
highlighter.setTextFragmenter(fragmenter);
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
System.out.println(doc.get("city"));
String desc = doc.get("desc");
if(desc!=null){
TokenStream tokenStream =analyzer.tokenStream("desc", new StringReader(desc));
System.out.println(highlighter.getBestFragment(tokenStream,desc));
}
}
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
public static void main(String[] args) {
String indexDir="D:\\Lucene";
String q="南京文明";
try {
search(indexDir,q);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.java00.lucene;
import java.io.StringReader;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
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.StringField;
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.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanQuery.Builder;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Fragmenter;
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.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import com.java00.entity.Blog;
import com.java00.util.DateUtil;
import com.java00.util.StringUtil;
/**
* 博客索引类
*
*
*/
public class BlogIndex {
private Directory dir;
/**
* 获取IndexWriter实例
*/
public IndexWriter getWriter() throws Exception {
dir = FSDirectory.open(Paths.get("D://Lucene"));
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(dir, iwc);
return indexWriter;
}
/**
* 添加博客索引
*/
public void addIndex(Blog blog) throws Exception {
IndexWriter indexWriter = getWriter();
Document doc = new Document();
doc.add(new StringField("id", String.valueOf(blog.getId()), Field.Store.YES));
doc.add(new TextField("title", blog.getTitle(), Field.Store.YES));
doc.add(new StringField("releaseDate", DateUtil.formatDate(blog.getReleaseDate(), "yyyy-MM-dd"),
Field.Store.YES));
doc.add(new TextField("content", blog.getContent(), Field.Store.YES));
indexWriter.addDocument(doc);
indexWriter.close();
}
/**
* 查询博客信息
*/
public List<Blog> searchBlog(String q) throws Exception{
dir =FSDirectory.open(Paths.get("D://Lucene"));
DirectoryReader reader = DirectoryReader.open(dir);
IndexSearcher is = new IndexSearcher(reader);
Builder booleanQuery = new BooleanQuery.Builder();
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
QueryParser parser = new QueryParser("title",analyzer);
Query query = parser.parse(q);
QueryParser parser2 = new QueryParser("content",analyzer);
Query query2 = parser.parse(q);
booleanQuery.add(query, Occur.SHOULD);
booleanQuery.add(query, Occur.SHOULD);
TopDocs hits = is.search(booleanQuery.build(), 100);
QueryScorer scorer = new QueryScorer(query);
Fragmenter fragmenter =new SimpleSpanFragmenter(scorer);
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer);
highlighter.setTextFragmenter(fragmenter);
List<Blog> blogList = new LinkedList<Blog>();
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc = is.doc(scoreDoc.doc);
Blog blog = new Blog();
blog.setId(Integer.parseInt(doc.get("id")));
blog.setReleaseDateStr(doc.get("releaseDate"));
String title = doc.get("title");
String content = doc.get("content");
if(title!=null){
TokenStream tokenStream = analyzer.tokenStream("title", new StringReader(title));
String hTitle = highlighter.getBestFragment(tokenStream, title);
if(StringUtil.isEmpty(hTitle)){
blog.setTitle(title);
}else{
blog.setTitle(hTitle);
}
}
if(content!=null){
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(title));
String hContent = highlighter.getBestFragment(tokenStream, title);
if(StringUtil.isEmpty(hContent)){
if(hContent.length()<200){
blog.setContent(hContent);
}else{
blog.setContent(hContent.substring(0, 200));
}
}else{
blog.setTitle(hContent);
}
}
blogList.add(blog);
}
return blogList;
}
}