准备jar
jar的下载地址:https://www.apache.org/dyn/closer.lua/lucene/java/4.10.3/lucene-4.10.3.tgz
lucene-4.10.3\analysis\common
lucene-4.10.3\core
lucene-4.10.3\queryparser
src下存放的三个文件,配置自定义词和过滤词
package com.itheima.lucene;
import static org.hamcrest.CoreMatchers.nullValue;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.lucene.analysis.Analyzer;
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.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.FieldCache.Parser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class IkAnalyzer02 {
private static final String driverClassName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://192.168.1.18:3306/zy_pls?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
private static final String username = "root";
private static final String password = "123456";
private static final Version version = Version.LATEST;
private DirectoryReader ireader = null;
private IndexWriter iwriter = null;
private IKAnalyzer analyzer;// 存放索引文件的位置,即索引库
private String searchDir = "d:\\temp\\index2";
private static File indexFile = null;
private static Connection conn;
//private Directory directory=FSDirectory.open(new File("searchDir"));
/**
* 读取索引文件
* @return
* @throws IOException
* @throws Exception
*/
public IndexSearcher getsearch() throws IOException {
indexFile=new File(searchDir);
ireader = DirectoryReader.open(FSDirectory.open(indexFile));
return new IndexSearcher(ireader);
}
/**
* 获取数据库连接
* @return
*/
public static Connection getConnection() {
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 中文分词工具
* @return
*/
public IKAnalyzer getAnalyzer() {
if(analyzer==null) {
return new IKAnalyzer();
}else {
return analyzer;
}
}
public void createIndex() throws SQLException, IOException{
Connection conn = getConnection();
ResultSet rs = null;
PreparedStatement pstmt = null;
if(conn == null){
System.out.println("get the connection error...");
return ;
}
String sql = "select id,content from t_lucene ";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
/**
* 获取索引文件位置
*/
indexFile=new File(searchDir);
if(!indexFile.exists()) {
indexFile.mkdirs();
}
/**
* 设置索引参数
*/
Directory directory=FSDirectory.open(indexFile);
//directory=FSDirectory.open(indexFile);
IndexWriterConfig iwConfig =new IndexWriterConfig(version, getAnalyzer());
iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
iwriter = new IndexWriter(directory,iwConfig);
/*lucene本身不支持更新
* 通过删除索引然后再建立索引来更新
*/
iwriter.deleteAll();//删除上次的索引文件,重新生成索引
while(rs.next()) {
Long id=rs.getLong("id");
String content = rs.getString("content");
Document doc = new Document();
doc.add(new TextField("id", id+"",Field.Store.YES));
doc.add(new TextField("content", content+"",Field.Store.YES));
iwriter.addDocument(doc);
}
iwriter.close();
rs.close();
pstmt.close();
conn.close();
}
public void searchByTerm(String field,String keyword,int num) throws Exception{
String fieldstr = "id";
IndexSearcher isearcher=getsearch();
Analyzer analyzer=getAnalyzer();
QueryParser queryParser=new QueryParser(field, analyzer);
Query query=queryParser.parse(keyword);
/*QueryParser qp=new QueryParser(version,field,analyzer);
qp.setDefaultOperator(QueryParser.OR_OPERATOR);
Query query = qp.parse(keyword);*/
Sort sort = new Sort(new SortField(fieldstr,SortField.Type.LONG,true)); //排序 false 升序 true降序
ScoreDoc[] hits = isearcher.search(query, num,sort).scoreDocs;
//ScoreDoc[] hits = isearcher.search(query, num).scoreDocs;
long startTime=System.currentTimeMillis();
long endTime=System.currentTimeMillis();
// System.out.println("匹配 '"+keyword+"' 总共花费:"+(endTime-startTime)+"毫秒,查询到"+hits.length+"条记录");
for (int i = 0; i < hits.length; i++) {
Document doc=isearcher.doc(hits[i].doc);
System.out.println("id ="+doc.get("id"));
System.out.println("content="+doc.get("content"));
}
}
/**
* 搜索索引
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
IkAnalyzer02 test = new IkAnalyzer02();
test.createIndex();
test.searchByTerm("content", "荷花", 100);
}
}