lucene 多欄位查詢 文字高亮顯示

2021-08-30 20:04:49 字數 1445 閱讀 4463

先建立索引,lucene3.0

directory directory = null;

indexsearcher isearcher = null;

analyzer analyzer = new ikanalyzer();

directory = new ******fsdirectory(new file("d:\data"));

isearcher = new indexsearcher(directory); //建立索引

//在索引器中使用iksimilarity相似度評估器 分詞器

isearcher.setsimilarity(new iksimilarity());

booleanquery bquery = new booleanquery(); //組合查詢

query keywordquery=null;

booleanclause.occur flags = ; //or 或者

keywordquery = multifieldqueryparser.parse(version.lucene_30,word,new string,flags,analyzer); //多欄位分詞搜尋

bquery.add(keywordquery, booleanclause.occur.must); //and

if(username=="ddddd")

//搜尋相似度最高的5條記錄

topdocs topdocs = isearcher.search(bquery , 500000);

//輸出結果

scoredoc scoredocs = topdocs.scoredocs;

//關鍵字高亮顯示

formatter formatter = new ******htmlformatter("", ""); //字首和字尾

scorer scorer = new queryscorer(bquery);

highlighter highlighter = new highlighter(formatter, scorer);

highlighter.settextfragmenter(new ******fragmenter(200)); //字長度

//高亮結束

[code]for(int i=0;i<10;i++) else

string descripe = highlighter.getbestfragment(analyzer,

"descripe", targetdoc.get("descripe"));

if (descripe == null) else

resourceindex.setversion(targetdoc.get("version")); //如果不高亮顯示則不判斷

}[/code]

所需的包

lucene多欄位查詢

booleanquery typenegativesearch new booleanquery queryparser parser new queryparser contents new analyzer parser.setdefaultoperator queryparser.and op...

lucene多欄位查詢

我的例子就是2.0的,現在給你的是兩個域,你可以用n個域 booleanquery typenegativesearch new booleanquery queryparser parser new queryparser contents new analyzer parser.setdefau...

Lucene 排序及多字段查詢

預設情況下,indexsearcher類的search方法返回查詢結果時,是按文件的分值排序的,可以使用過載的search方法對結果排序 indexsearcher.search query,sort new sort 和 sort.relevance,以及null一樣,採用預設排序,要定義排序字段...