首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lucene评分函数

Lucene评分函数
EN

Stack Overflow用户
提问于 2016-05-15 17:22:09
回答 1查看 608关注 0票数 1

我正在开发一个在索引中索引和搜索tweet的系统,每个tweet都有一个定义其社会重要性(社会价值)的字段,我想将这个值添加到相似性评分中,这样我就可以通过将文档的社会价值和得分组合到查询中来对文档进行排序。

例如,我的评分函数将类似于

代码语言:javascript
复制
Final Score = QueryScore + Social score ( which is a float that I already calculated)

那我怎么能做到这一点呢?

我用的是lucene-5.5.0

代码语言:javascript
复制
package Lucene;


import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class SearchFiles {

    @SuppressWarnings("deprecation")
    public static void main(String[] args){
        try{

            Path path = Paths.get("C:\\Users\\JUGURTHA\\Desktop\\boulot\\index");
            Directory dir = FSDirectory.open(path);
            DirectoryReader ireader = DirectoryReader.open(dir);
            IndexSearcher isearcher = new IndexSearcher(ireader);
            StandardAnalyzer analyzer = new StandardAnalyzer();
            //get each token            
            QueryParser parser = new QueryParser("text", analyzer);
            Query query = parser.parse("Love");
            ScoreDoc[] hits = isearcher.search(query, null, 20).scoreDocs;
            for (int i = 0; i <  hits.length; i++){
                Document hitDoc = isearcher.doc(hits[i].doc);
                System.out.println("Tweet " + i + " : " + hitDoc.get("text"));
                System.out.println("created_at: " + hitDoc.get("date"));
                System.out.println("id: " + hitDoc.get("id"));
                System.out.println();
                System.out.println();

            }


        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-17 16:02:46

我想出了如何完成工作,我使用了CustomScoreProvider类,得到了我想要的结果。

代码语言:javascript
复制
class CustomizedScoreProvider extends CustomScoreProvider {

    public CustomizedScoreProvider(LeafReaderContext reader) {
            super(reader);
            // TODO Auto-generated constructor stub
        }

    public  float customScore(int doc, float subQueryScore,float valSrcScores[]){

     try {

         subQueryScore+=4; // I only added this for testing , 
     } catch(Exception e) { 
         e.printStackTrace();
            }
    return subQueryScore;
             }
    }

class CustomizedScoreQuery extends CustomScoreQuery{


public CustomizedScoreQuery(Query subQuery,IndexReader ireader) {
        super(subQuery);
        // TODO Auto-generated constructor stub
    }
public CustomizedScoreProvider getCustomScoreProvider (LeafReaderContext reader){
    CustomizedScoreProvider i=new CustomizedScoreProvider(reader);
     return (i);
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37241371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档