国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Lucene相關(guān)度排序的調(diào)整 - Lucene - JavaEye知識(shí)庫

Lucene的搜索結(jié)果默認(rèn)按相關(guān)度排序,這個(gè)相關(guān)度排序是基于內(nèi)部的Score和DocID,Score又基于關(guān)鍵詞的內(nèi)部評(píng)分和做索引時(shí)的boost。默認(rèn)Score高的排前面,如果Score一樣,再按索引順序,先索引的排前面。那么有人問了,如果我要先索引的排后面怎么辦呢?隱士研究了源碼后發(fā)現(xiàn)這是相當(dāng)簡單的事情。以下代碼基于Lucene 2.0。

看Sort的默認(rèn)構(gòu)造函數(shù),相關(guān)度就是SortField.FIELD_SCORE和SortField.FIELD_DOC的組合。

java 代碼
  1. /**  
  2.  * Sorts by computed relevance. This is the same sort criteria as calling  
  3.  * {@link Searcher#search(Query) Searcher#search()}without a sort criteria,  
  4.  * only with slightly more overhead.  
  5.  */  
  6. public Sort() {   
  7.   this(new SortField[] { SortField.FIELD_SCORE, SortField.FIELD_DOC });   
  8. }  

那么該如何構(gòu)造我們需要的SortField呢?請(qǐng)看SortField的一個(gè)構(gòu)造函數(shù),有一個(gè)參數(shù)reverse可供我們調(diào)整結(jié)果集的順序。

java 代碼
  1. /** Creates a sort, possibly in reverse, by terms in the given field with the  
  2.    * type of term values explicitly given.  
  3.    * @param field  Name of field to sort by.  Can be <code>null</code> if  
  4.    *               <code>type</code> is SCORE or DOC.  
  5.    * @param type   Type of values in the terms.  
  6.    * @param reverse True if natural order should be reversed.  
  7.    */  
  8.   public SortField (String field, int type, boolean reverse) {   
  9.     this.field = (field != null) ? field.intern() : field;   
  10.     this.type = type;   
  11.     this.reverse = reverse;   
  12.   }  

由此可見,只要構(gòu)造一個(gè)SortField[]就可以實(shí)現(xiàn)我們要的功能,請(qǐng)看:

java 代碼
  1. // 評(píng)分降序,評(píng)分一樣時(shí)后索引的排前面   
  2. new SortField[] { SortField.FIELD_SCORE, new SortField(null, SortField.DOC, true) }   
  3.   
  4. // 評(píng)分升序,評(píng)分一樣時(shí)后索引的排前面,呵呵,此為最不相關(guān)的排前面,挺有趣的   
  5. new SortField[] { new SortField(null, SortField.SCORE, true), new SortField(null, SortField.DOC, true) }  

呵呵,只要將此SortField[]作為參數(shù)傳入Sort的構(gòu)造函數(shù)得到Sort的一個(gè)instance,將此instance傳入searcher.search(query, sort)即可得到了期望的結(jié)果。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Lucene 排序
lucene中的數(shù)值型字段(NumericField)【精】
lucene3.0創(chuàng)建索引及多目錄搜索詳解
Lucene與搜索引擎技術(shù)(Document包詳解)
Lucene
Lucene-2.0學(xué)習(xí)文檔(2) - 企業(yè)應(yīng)用 - Java - JavaEye論壇
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服