package com.mysqindex;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
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.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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import com.mysql.MySql;
public class MysqlIndex {
//对博客内容创建索引
public static boolean buildBlogIndex(final Analyzer analyzer, final Directory directory, String selectsql, final int num){
try {
IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_47, analyzer));
indexWriter.deleteAll();
int begin = 0;
String selectsql2 = selectsql;
selectsql2 += " limit " + begin + "," + num;
//读取表里面指定的内容
Map<String,String> map = MySql.selectBlogData(selectsql2);
while(map.size() != 0){
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Document doc = new Document();
Map.Entry<String, String> entry = (Entry<String, String>) it.next();
doc.add(new Field("id", entry.getKey(), TextField.TYPE_STORED));
doc.add(new Field("content", entry.getValue(), TextField.TYPE_NOT_STORED ));
indexWriter.addDocument(doc);
}
selectsql2 = selectsql;
begin += num;
selectsql2 += " limit " + (begin-1) + "," + num;
//indexWriter.close();
map = MySql.selectBlogData(selectsql2);
}
indexWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
//根据关键字进行搜索
public static LinkedList<String> searchBlogIndex(final Analyzer analyzer, final Directory directory, final String keyword, final int score) {
DirectoryReader ireader = null;
LinkedList<String> list = new LinkedList<String>();
try {
// 打开索引存储
ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
QueryParser par1 = new QueryParser(Version.LUCENE_47,"content",analyzer);
Query query = par1.parse(keyword);
//设置评分靠前的score条数据
ScoreDoc[] hits = isearcher.search(query, null, score).scoreDocs;
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
//得到查询数据的内容
// System.out.println(hitDoc.get("content")+ " +++++++++++++++++++");
//得到查询数据的地址
list.add(hitDoc.get("id"));
// System.out.println(hitDoc.get("id") );
//得到查询数据的题目
//System.out.println(hitDoc.get("title"));
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
ireader.close();
directory.close();
} catch (IOException e) {
}
}
return list;
}
public static void main(String []args){
File indexFile = new File("c:/lucene");
Analyzer analyzer = new IKAnalyzer();
Directory directory;
try {
directory = FSDirectory.open(indexFile);
String selectsql = "select * from blog";
int num = 10;
int score = 5;
String keyword = "winnerspring是何许人也";
buildBlogIndex(analyzer, directory,selectsql, num);
searchBlogIndex(analyzer, directory, keyword, score);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
lucene对数据库建立索引
最新推荐文章于 2021-01-09 16:12:26 发布