assigning different weights to different query terms in lucene

一曲冷凌霜 提交于 2019-12-24 17:07:55

问题


I'm very new to lucene and wants to do the following. Suppose my query is,

query = "apple growers fruit ipad mac"

,but I want to give different weights to these query terms like,

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.

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.

How can I do this?


回答1:


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



回答2:


an another way which is much easier if the boosting scores are all positive.

QueryParser parser = new QueryParser( "content", new StandardAnalyzer() );
Query q = parser.parse( "Apple^1 juice^5 grower^4 mac^0.2 iphone^0.1 );

searcher.search( q, 10 )


来源:https://stackoverflow.com/questions/34783819/assigning-different-weights-to-different-query-terms-in-lucene

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!