Lucene相关度排序
/**
*
* 相关度排序...
* @author Administrator
*
*/
public class TestScore {
/**
* @param args
* @throws ParseException
* @throws IOException
*/
public static void main(String[] args) throws ParseException, IOException {
IndexSearcher indexSearcher=LuceneUtils.getIndexSearcher();
String keywords="你不问";
//根据多个字段进行搜索...
/**
* 1:matchVersion
*
* 2:需要根据那些字段进行搜索,数组
*
* 3:分词器...
*/
String [] fields ={"title"};
QueryParser queryParser=new MultiFieldQueryParser(LuceneUtils.getCurrentVersion(),fields,LuceneUtils.getAnalyzer());
Query query=queryParser.parse(keywords);
TopDocs topDocs=indexSearcher.search(query, 100);
ScoreDoc scoreDocs []=topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
//这个id 是lucene 自己内部维护的
int docID=scoreDoc.doc;
float f=scoreDoc.score;
// new intFiled("id",article.getId(),Store.YES);肯定不一样,我这边只是去了一个字段的名称叫id,是int number
Document document=indexSearcher.doc(docID);
System.out.println("docID=="+document.get("id")+"==对应的得分..."+f);
}
}
}