Lucene 评分机制二 Payload
这里使用的Lucene4.7.0和Lucene3.X稍有不同
有下面三段内容,我想对船一系列的搜索进行加分
bike car jeep truck bus boat
train car ship boat van subway
car plane taxi boat vessel railway
- 定义自定义的MyAnalyzer,实现对字段的有效载荷进行赋值
package com.pera.lucene.score.payload; import java.io.Reader; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
import org.apache.lucene.analysis.payloads.PayloadEncoder;
import org.apache.lucene.util.Version; public class MyAnalyzer extends Analyzer
{ private PayloadEncoder encoder; MyAnalyzer(PayloadEncoder encoder)
{
this.encoder = encoder;
} @Override
protected TokenStreamComponents createComponents(String fieldName, Reader reader)
{
// 用来解析空格分隔的各个类别
Tokenizer source = new WhitespaceTokenizer(Version.LUCENE_47, reader);
// 自定义的Filter,用来获取字段的Payload值
MyTokenFilter filter = new MyTokenFilter(source, encoder); return new TokenStreamComponents(source, filter);
} }
- 自定义TokenFilter来达到取得字段的PayLoad值或通过字段对PayLoad值进行分析赋值
package com.pera.lucene.score.payload; import java.io.IOException; import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.payloads.PayloadEncoder;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute; public class MyTokenFilter extends TokenFilter
{
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
private final PayloadAttribute payAtt = addAttribute(PayloadAttribute.class);
private final PayloadEncoder encoder; public MyTokenFilter(TokenStream input, PayloadEncoder encoder)
{
super(input);
this.encoder = encoder;
} @Override
public boolean incrementToken() throws IOException
{
if (input.incrementToken())
{
String term = termAtt.toString();
if (App.scoreMap.containsKey(term))
{
payAtt.setPayload(encoder.encode(App.scoreMap.get(term).toCharArray()));
} else
{
payAtt.setPayload(null);
}
return true;
} else
return false;
} }
public static ImmutableMap<String, String> scoreMap = ImmutableMap.of("boat", "5f", "ship", "20f", "vessel", "100f");
- 自定义PayloadSimilarity继承DefaultSimilarity 重载scorePayload方法,在检索时获得之前设置的PayLoad值
package com.pera.lucene.score.payload; import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.util.BytesRef; public class PayloadSimilarity extends DefaultSimilarity
{
@Override
public float scorePayload(int doc, int start, int end, BytesRef payload)
{
return PayloadHelper.decodeFloat(payload.bytes);
}
}
- 建立索引 需要将之前定义的Analyzer和PayloadSimilarity设置到Config中
package com.pera.lucene.score.payload; import java.io.File;
import java.io.IOException;
import java.util.Date; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.payloads.FloatEncoder;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; public class Indexing
{
public void indexPayload() throws IOException
{
Directory dir = FSDirectory.open(new File(App.indexPath));
Analyzer analyzer = new MyAnalyzer(new FloatEncoder());
Similarity similarity = new PayloadSimilarity(); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwc.setOpenMode(OpenMode.CREATE).setSimilarity(similarity);
Date start = new Date();
System.out.println("Indexing to directory '" + App.indexPath + "'...");
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new TextField("tools", "bike car jeep truck bus boat", Store.YES));
writer.addDocument(doc); doc = new Document();
doc.add(new TextField("tools", "train car ship boat van subway", Store.YES));
writer.addDocument(doc); doc = new Document();
doc.add(new TextField("tools", "car plane taxi boat vessel railway", Store.YES));
writer.addDocument(doc); writer.close(); Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");
}
}
- 进行检索 检索时要将PayloadSimilarity设置到searcher中
package com.pera.lucene.score.payload; import java.io.File;
import java.io.IOException; import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.payloads.AveragePayloadFunction;
import org.apache.lucene.search.payloads.PayloadTermQuery;
import org.apache.lucene.store.FSDirectory; public class Searching
{ public void searchPayload() throws IOException, ParseException
{
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(App.indexPath)));
IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery bq = new BooleanQuery(); PayloadTermQuery ptq1 = new PayloadTermQuery(new Term("tools", "ship"), new AveragePayloadFunction());
PayloadTermQuery ptq2 = new PayloadTermQuery(new Term("tools", "boat"), new AveragePayloadFunction());
PayloadTermQuery ptq3 = new PayloadTermQuery(new Term("tools", "vessel"), new AveragePayloadFunction()); bq.add(ptq1, Occur.SHOULD);
bq.add(ptq2, Occur.SHOULD);
bq.add(ptq3, Occur.SHOULD); // 设置自定义的PayloadSimilarity
searcher.setSimilarity(new PayloadSimilarity());
TopDocs results = searcher.search(bq, 10);
ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits;
System.out.println(numTotalHits + " total matching documents"); for (int i = 0; i < hits.length; i++)
{
int docId = hits[i].doc; // 文档编号
float lucene_score = hits[i].score;
String tools = searcher.doc(docId).get("tools");
System.out.println("DocId:" + docId + "\tLucene Score:" + lucene_score + "\tTools:" + tools);
Explanation explanation = searcher.explain(bq, docId);
System.out.println(explanation.toString());
}
}
}
- 检索结果 可以看到Doc2的排序由于有了PayLoad值排名得到了提升
3 total matching documents
DocId:2 Lucene Score:16.750757 Tools:car plane taxi boat vessel railway
16.750757 = (MATCH) product of:
25.126135 = (MATCH) sum of:
0.3186112 = (MATCH) btq, product of:
0.06372224 = weight(tools:boat in 2) [PayloadSimilarity], result of:
0.06372224 = score(doc=2,freq=0.5 = phraseFreq=0.5
), product of:
0.33736566 = queryWeight, product of:
0.71231794 = idf(docFreq=3, maxDocs=3)
0.4736167 = queryNorm
0.18888181 = fieldWeight in 2, product of:
0.70710677 = tf(freq=0.5), with freq of:
0.5 = phraseFreq=0.5
0.71231794 = idf(docFreq=3, maxDocs=3)
0.375 = fieldNorm(doc=2)
5.0 = AveragePayloadFunction.docScore()
24.807524 = (MATCH) btq, product of:
0.24807523 = weight(tools:vessel in 2) [PayloadSimilarity], result of:
0.24807523 = score(doc=2,freq=0.5 = phraseFreq=0.5
), product of:
0.66565174 = queryWeight, product of:
1.4054651 = idf(docFreq=1, maxDocs=3)
0.4736167 = queryNorm
0.37268022 = fieldWeight in 2, product of:
0.70710677 = tf(freq=0.5), with freq of:
0.5 = phraseFreq=0.5
1.4054651 = idf(docFreq=1, maxDocs=3)
0.375 = fieldNorm(doc=2)
100.0 = AveragePayloadFunction.docScore()
0.6666667 = coord(2/3) DocId:1 Lucene Score:3.5200772 Tools:train car ship boat van subway
3.5200772 = (MATCH) product of:
5.2801156 = (MATCH) sum of:
4.9615045 = (MATCH) btq, product of:
0.24807523 = weight(tools:ship in 1) [PayloadSimilarity], result of:
0.24807523 = score(doc=1,freq=0.5 = phraseFreq=0.5
), product of:
0.66565174 = queryWeight, product of:
1.4054651 = idf(docFreq=1, maxDocs=3)
0.4736167 = queryNorm
0.37268022 = fieldWeight in 1, product of:
0.70710677 = tf(freq=0.5), with freq of:
0.5 = phraseFreq=0.5
1.4054651 = idf(docFreq=1, maxDocs=3)
0.375 = fieldNorm(doc=1)
20.0 = AveragePayloadFunction.docScore()
0.3186112 = (MATCH) btq, product of:
0.06372224 = weight(tools:boat in 1) [PayloadSimilarity], result of:
0.06372224 = score(doc=1,freq=0.5 = phraseFreq=0.5
), product of:
0.33736566 = queryWeight, product of:
0.71231794 = idf(docFreq=3, maxDocs=3)
0.4736167 = queryNorm
0.18888181 = fieldWeight in 1, product of:
0.70710677 = tf(freq=0.5), with freq of:
0.5 = phraseFreq=0.5
0.71231794 = idf(docFreq=3, maxDocs=3)
0.375 = fieldNorm(doc=1)
5.0 = AveragePayloadFunction.docScore()
0.6666667 = coord(2/3) DocId:0 Lucene Score:0.106203735 Tools:bike car jeep truck bus boat
0.106203735 = (MATCH) product of:
0.3186112 = (MATCH) sum of:
0.3186112 = (MATCH) btq, product of:
0.06372224 = weight(tools:boat in 0) [PayloadSimilarity], result of:
0.06372224 = score(doc=0,freq=0.5 = phraseFreq=0.5
), product of:
0.33736566 = queryWeight, product of:
0.71231794 = idf(docFreq=3, maxDocs=3)
0.4736167 = queryNorm
0.18888181 = fieldWeight in 0, product of:
0.70710677 = tf(freq=0.5), with freq of:
0.5 = phraseFreq=0.5
0.71231794 = idf(docFreq=3, maxDocs=3)
0.375 = fieldNorm(doc=0)
5.0 = AveragePayloadFunction.docScore()
0.33333334 = coord(1/3)
Lucene 评分机制二 Payload的更多相关文章
- Apache Lucene评分机制的内部工作原理
Apache Lucene评分机制的内部工作原理' 第5章
- Lucene 评分机制一
1. 评分公式 1.1 公式介绍 这个公式是Lucene实际计算时使用的公式,是由原型公式推导而来 tf(t in d) 表示某个term的出现频率,定义了term t出现在当前document d的 ...
- lucene 的评分机制
lucene 的评分机制 elasticsearch是基于lucene的,所以他的评分机制也是基于lucene的.评分就是我们搜索的短语和索引中每篇文档的相关度打分. 如果没有干预评分算法的时候,每次 ...
- Lucene Scoring 评分机制
原文出处:http://blog.chenlb.com/2009/08/lucene-scoring-architecture.html Lucene 评分体系/机制(lucene scoring)是 ...
- Lucene 的 Scoring 评分机制
转自: http://www.oschina.net/question/5189_7707 Lucene 评分体系/机制(lucene scoring)是 Lucene 出名的一核心部分.它对用户来 ...
- Solr4.8.0源码分析(19)之缓存机制(二)
Solr4.8.0源码分析(19)之缓存机制(二) 前文<Solr4.8.0源码分析(18)之缓存机制(一)>介绍了Solr缓存的生命周期,重点介绍了Solr缓存的warn过程.本节将更深 ...
- Solr In Action 笔记(2) 之 评分机制(相似性计算)
Solr In Action 笔记(2) 之评分机制(相似性计算) 1 简述 我们对搜索引擎进行查询时候,很少会有人进行翻页操作.这就要求我们对索引的内容提取具有高度的匹配性,这就搜索引擎文档的相似性 ...
- Elasticseach的评分机制
lucene 的评分机制 elasticsearch是基于lucene的,所以他的评分机制也是基于lucene的.评分就是我们搜索的短语和索引中每篇文档的相关度打分. 如果没有干预评分算法的时候,每次 ...
- Wifi 评分机制分析
从android N开始,引入了wifi评分机制,选择wifi的时候会通过评分来选择. android O源码 frameworks\opt\net\wifi\service\java\com\and ...
随机推荐
- Unable to find explicit activity class报错问题解决方法
转:http://hi.baidu.com/mz_mz/item/f5672ad814e1ce30e2108f69 1.首先查看是否在已经在AndroidMainfest.xml中添加了你的Activ ...
- SGI STL rope
rope实现的接口可以参考这里. rope是可伸缩的string实现: 它们被设计为用于把string看作一个整体的高效操作 . 比如赋值.串联和子串的操作所花的时间差不多不依赖字符串的长度.与C的字 ...
- Windows下DNS ID欺骗的原理与实现
域名系统(DNS)是一种用于TCP/IP应用程序的分布式数据库,它提供主机名字和IP地址之间的转换信息.通常,网络用户通过UDP协议和DNS服务器进行通信,而服务器在特定的53端口监听,并返回用户所需 ...
- springboot配置文件application.properties更新记录(自学使用)
#应用名称spring.application.name=demo #应用根目录server.context-path=/demo #应用端口server.port=8084 #错误页,指定发生错误时 ...
- 牛客集训第七场J /// DP
题目大意: 在矩阵(只有52种字符)中找出所有不包含重复字符的子矩阵个数 #include <bits/stdc++.h> #define ll long long using names ...
- P1006 传纸条 /// DP+滚动数组
题目大意: https://www.luogu.org/problemnew/show/P1006 题解 不难想到 求从起点到终点的两条不同的路 因为只能向右或向下走 所以纸条1和2不可能同时位于同一 ...
- 一个上午,勉强记住了几种不同语言编译PE的启动函数
VC:启动函数最乱,三大函数都在后面.前面8个PUSH DELPHI7:启动函数最整洁,2.3.4.2,形式排队 VB:启动函数最好记,12个0.... 汇编:三大函数距离最紧凑,除VB外,启动函数最 ...
- Python学习之--迭代器、生成器
迭代器 迭代器是访问集合元素的一种方式.从对象第一个元素开始访问,直到所有的元素被访问结束.迭代器只能往前,不能往后退.迭代器与普通Python对象的区别是迭代器有一个__next__()方法,每次调 ...
- Map和Reduce函数
- U-BOOT 命令的介绍
UBOOT 常用命令 通常使用 help(或者只使用问号?),来查看所有的 UBOOT 命令.将会列出在当前配置下所有支持的命令. 但是我们要注意,尽管 UBOOT 提供了很多配置选项,并不是所 ...