在Lucene中为不同的查询词分配不同的权重 [英] assigning different weights to different query terms in lucene
问题描述
我对lucene还是很陌生,想做以下事情.假设我的查询是
I'm very new to lucene and wants to do the following. Suppose my query is,
query =苹果种植者的果实ipad mac"
query = "apple growers fruit ipad mac"
,但是我想对这些查询字词赋予不同的权重,
,but I want to give different weights to these query terms like,
query =苹果(0.2)种植者(0.7)水果(0.9)ipad(0.05)mac(0.06)
query = "apple (0.2) growers (0.7) fruit (0.9) ipad (0.05) mac (0.06)
,直觉是,我想将在农业意义上谈论苹果的文档的排名高于在技术方面的文档.
, the intuition is that i want to rank the documents that talks about apple in the sense of agriculture higher than those of which about tech.
我在这里看到了(如何在Lucene/Solr 中为词条查询分配权重,您可以使用Query.setBoost()
,但是据我了解,它通过指定的分数平均提高了查询中的所有词条,这不是我所需要的想要.
I have seen here (How to assign a weight to a term query in Lucene/Solr), that you can use Query.setBoost()
but as I understand, it boosts all the terms equally in the query by the score specified, which is not what I want.
我该怎么做?
推荐答案
Query query1 = new TermQuery(new Term("your_default_field", "apple"));
query1.setBoost(0.2);
Query query2 = new TermQuery(new Term("your_default_field", "growers"));
query2.setBoost(0.7);
Query query3 = new TermQuery(new Term("your_default_field", "fruit"));
query3.setBoost(0.9);
Query query4 = new TermQuery(new Term("your_default_field", "ipad"));
query4.setBoost(0.05);
Query query5 = new TermQuery(new Term("your_default_field", "mac"));
query5.setBoost(0.06);
BooleanQuery combining = new BooleanQuery();
combining.add(query1, Occur.SHOULD);
combining.add(query2, Occur.SHOULD); // and so on and so forth
这篇关于在Lucene中为不同的查询词分配不同的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!