lucene 對搜尋結果進行排序

2021-08-30 09:33:08 字數 2421 閱讀 2102

1、在indexsearcher類中包含了幾個可過載的search方法,有乙個對結果進行排序的search方法宣告為

search(query,sort)

public classsortingexample{

private directory directory;

public sortingexample(directory direcotry){

this.directory=directory;

public void displayhits(query query,sort sort) throwsioexception{

indexsearcher searcher=new indexsearcher(directory);

hit****s=searcher.search(query,sort);

system.out.println(stringutils.rightpad("title",30))+

stringutils.rightpad("pubmonth",10)+

stringutils.center("id",4)+stringutils.center("score",15));

decimalformat scoreformatter=new deciamalformat("0.#####");

for (int i=0;idocument doc=hits.doc(i);

system.out.println(stringutils.rightpad(

stringutils.abbreviae(doc.get("title"),29),30)+

stringutils.rightpad(doc.get("pubmonth"),10)+

stringutils.center(""+hits.id(i),4)+

stringutils.leftpad(scoreformatter.format(hits.score(i)),12)

system.out.println(""+doc.get("category"));

searcher.close();

2、使用搜尋排序

stringindexdir=system.getproperty("index.dir");

fsdirectorydirectory=fsdirectory.getdirectory(indexdir,false);

sortingexample example=new sortingexample(directory);

3、按相關性進行排序(降序),以下三個語句通過displayhits呼叫了searcher.search,實際效果一樣,後2個語句還要另外建立乙個sort物件

example.displayhits(allbooks,null);

example.displayhits(allbooks,sort.relevance);

example.displayhits(allbooks,new sort());

4、按照索引順序排序(文件id公升序)

example.displayhits(allbooks,sort.indexorder);

5、通過某個域進行排序

首先,這個域索引時要指定為field.keyword域:如:field.keyword("size",1024);

其次,要建立新的索引物件

example.displayhits(allbooks,new sort("category"));

6、以相反順序排序

對於相關性和文件id使用預設排序,其他域使用公升序排序。使用true指明為倒序

example.displayhits(allbooks,new sort("category",true));

7、根據多個域進行排序和指定排序域型別

先按category域,再按字段得分,然後按pubmonth(指定了pubmonth的排序域外為int型別)

example.displayhits(allbooks,new sort(new sortfield{

new sortfield("category"),

sortfield.field_score,

new sortfield("pubmonth",sortfield.int,true)

8、使用非預設的locale排序

public sortfield(string field,locale locale)

public sortfield(string field,locale locale,boolean reverse)

當按時sortfield.string型別排序時,應用程式內部會預設地呼叫string.compareto()來確定各個項的次序。通過collator.getinstance(locale)方法來指定本地的collator物件,而該物件的collator.compare()用於決定排序的方式

使用lucene對搜尋結果排序

lucene預設根據匹配度對搜尋結果降序排,如果對某個域進行排序?通常分兩步 step1 建索引時 newfield audittime row.get audittime tostring 關鍵點是你需要排序的字段建索引時應該採用 field.index.un tokenized,至於需不需要 f...

使用lucene對搜尋結果排序

lucene預設根據匹配度對搜尋結果降序排,如果對某個域進行排序?通常分兩步 step1 建索引時 newfield audittime row.get audittime tostring 關鍵點是你需要排序的字段建索引時應該採用 field.index.un tokenized,至於需不需要 f...

Lucene 對所查的結果進行排序

前面介紹完查詢以後,現在要對查詢結果進行排序的顯示。package com.querytype import org.apache.lucene.analysis.standard.standardanalyzer import org.apache.lucene.document.document...