lucence实现的两步
1.第一步先建立索引,其中先查询出来数据放到一个数组中
public
class
IndexerFile {
public
static
int
indexFile(String indexDir, Music[] list)
throws
IOException {
//采用IKAnalyzer分词;
Analyzer analyzer =
new
IKAnalyzer();
IndexWriterConfig conf =
new
IndexWriterConfig(Version.LUCENE_36, analyzer);
conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter writer =
new
IndexWriter(FSDirectory.open(
new
File(indexDir)), conf);
for
(
int
i =
0
; i < list.length; i++) {
Document doc =
new
Document();
doc.add(
new
Field(
"musicId"
, String.valueOf(list[i].getMusic_id()), Field.Store.YES, Field.Index.NO));
doc.add(
new
Field(
"description"
, list[i].getDescription(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(
new
Field(
"userName"
, list[i].getUserName(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(
new
Field(
"tag"
, list[i].getTag(), Field.Store.YES, Field.Index.ANALYZED));
}
int
numIndexed = writer.maxDoc();
writer.forceMerge(
1
);
writer.close();
return
numIndexed;
}
2.进行搜索
public
class
SearcherFile {
public
static
void
search(String[] q)
throws
IOException, ParseException {
//采用IKAnalyzer分词;
Analyzer analyzer =
new
IKAnalyzer();
//采用标准分词器;
IndexSearcher searcher=
null
;
IndexReader reader = IndexReader.open(FSDirectory.open(
new
File(indexDir)));
searcher =
new
IndexSearcher(reader);
//Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
String[] fields = {
"title"
,
"description"
,
"tag"
,
"userName"
};
Query query = MultiFieldQueryParser.parse(Version.LUCENE_36, q, fields, analyzer);
TopDocs topDocs = searcher.search(query,
100
);
//100是显示队列的Size
ScoreDoc[] hits = topDocs.scoreDocs;
System.out.println(
"共有"
+ searcher.maxDoc() +
"条索引,命中"
+ hits.length +
"条"
);
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<span>", "</span>");
Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
for
(
int
i =
0
; i < hits.length; i++) {
int
DocId = hits[i].doc;
Document document = searcher.doc(DocId);
//tokenStream()中第一个参数可以为空,具体不知道什么作用
TokenStream tokens = analyzer.tokenStream("",new StringReader(document.get("filename"))); // 7.根据searcher和ScordDoc对象获取具体的Document对象
System.out.println(highlighter.getBestFragment(tokens, document.get("filename")));
}
}
}