作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 lucene 3.5 中运行代码。但是,如何在 lucene 4.5 中运行代码,
public class CustomScoreQuerySample {
private Log logger = LogFactory.getLog(CustomScoreQuerySample.class);
public static void main(String[] args) throws Exception {
CustomScoreQuerySample sample = new CustomScoreQuerySample();
sample.makeIndex();
sample.execute("??");
}
private static final String[][] DATAS = {
{"??","981"},
{"??","672"},
{"??","521"},
{"??","124"},
{"??","908"},
{"??","652"},
{"??","872"},
{"??","278"},
{"??","485"},
{"??","372"}};
private RAMDirectory dir = new RAMDirectory();
private Analyzer analyzer = new SimpleAnalyzer();
public void makeIndex() throws Exception{
IndexWriter writer = new IndexWriter( dir, analyzer , true, MaxFieldLength.UNLIMITED);
try{
for(int i=0;i<DATAS.length;i++){
Document doc = new Document();
doc.add( new Field( "value", DATAS[i][0], Store.YES, Index.NOT_ANALYZED ) );
doc.add( new Field( "count", DATAS[i][1], Store.YES, Index.NOT_ANALYZED ) );
writer.addDocument( doc );
}
}finally{
writer.close();
}
}
public void execute(String value) throws Exception{
IndexSearcher searcher = new IndexSearcher( dir );
try{
search(searcher,value);
}finally{
searcher.close();
}
}
private void search(Searcher searcher,String value) throws Exception{
Query tq = new TermQuery(new Term("value",value));
FloatFieldSource source = new FloatFieldSource("count");
ValueSourceQuery vq = new ValueSourceQuery(source);
//FieldScoreQuery vq = new FieldScoreQuery("count",FieldScoreQuery.Type.INT);
CustomScoreQuery query = new CustomScoreQuery(tq,vq) {
@Override
public float customScore(int doc, float subQueryScore, float valSrcScore) {
float result = subQueryScore * valSrcScore;
System.out.println("doc:"+doc+" / subQueryScore:"+subQueryScore+" / valSrcScore:"+valSrcScore + " / result:"+result);
return valSrcScore;
}
};
query.setStrict(true);
TopScoreDocCollector collector = null;
collector = TopScoreDocCollector.create(5 * 5, false);
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
int count = hits.length;
for( int i = 0; i < count; i++ ){
Document doc = searcher.doc(hits[i].doc);
float score = hits[i].score;
System.out.println( score + " : " + doc.get( "value" )+" / "+ doc.get( "count" ));
//Explanation exp = searcher.explain( query, hits[i].doc );
//System.out.println( exp.toString() );
}
}
}
如何在 lucene 4.5 中使用这个“CustomScoreQuery”?。我尝试了以下代码,但我无法得到任何答案,
CustomScoreQuery cstquery =new CustomScoreQuery(tq);
但在这里我没有看到 valueScore。我在 API 中找不到。任何人都可以,请帮我解决这个问题。
最佳答案
从 3.X 到 4.X 已经有很多变化,但基本上您需要使用 FunctionQuery 而不是 ValueSourceQuery。这是为 4.4 编写的,但对于 4.5 应该是相同的。
public class CustomScoreQuerySample {
public static void main(String[] args) throws Exception {
CustomScoreQuerySample sample = new CustomScoreQuerySample();
sample.makeIndex();
sample.search("XX");
}
private static final String[][] DATAS = { { "XX", "981" }, { "XX", "672" },
{ "XX", "521" }, { "XX", "124" }, { "XX", "908" }, { "XX", "652" },
{ "XX", "872" }, { "XX", "278" }, { "XX", "485" }, { "XX", "372" } };
private RAMDirectory dir = new RAMDirectory();
private Analyzer analyzer = new SimpleAnalyzer(Version.LUCENE_44);
public void makeIndex() throws Exception {
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
IndexWriter writer = new IndexWriter(dir, config);
try {
for (int i = 0; i < DATAS.length; i++) {
Document doc = new Document();
doc.add(new TextField("value", DATAS[i][0], Field.Store.YES ));
doc.add(new FloatField("count", Float.parseFloat(DATAS[i][1]), Field.Store.YES ));
writer.addDocument(doc);
}
} finally {
writer.close();
}
}
private void search(String value) throws Exception {
DirectoryReader reader = DirectoryReader.open(dir);
try {
QueryParser parser = new QueryParser(Version.LUCENE_44, "value", analyzer);
Query tq = parser.parse("XX");
FloatFieldSource source = new FloatFieldSource("count");
FunctionQuery fq = new FunctionQuery(source);
CustomScoreQuery query = new CustomScoreQuery(tq, fq);
TopScoreDocCollector collector = TopScoreDocCollector.create(5 * 5, false);
IndexSearcher searcher = new IndexSearcher(reader);
searcher.search(query,collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
int count = hits.length;
for (int i = 0; i < count; i++) {
Document doc = searcher.doc(hits[i].doc);
float score = hits[i].score;
System.out.println(score + " : " + doc.get("value") + " / "
+ doc.get("count"));
}
} finally {
reader.close();
}
}
}
关于java - 如何让 CustomScoreQuery 在 lucene 4.5 版中运行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680883/
我是一名优秀的程序员,十分优秀!