我对lucene很新,并希望做到以下几点。假设我的查询是,
query =“apple growers fruit ipad mac”
,但我想对这些查询字词赋予不同的权重,例如
query =“apple(0.2)种植者(0.7)水果(0.9)ipad(0.05)mac(0.06)
,直觉是我想对农业意义上谈论苹果的文件进行排序,而不是关于技术的文件。
我在这里看到(How to assign a weight to a term query in Lucene/Solr),您可以使用Query.setBoost()
,但据我了解,它会通过指定的分数在查询中平均提升所有条款,这不是我想要的。
我该怎么做?
答案 0 :(得分:2)
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
答案 1 :(得分:0)
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 )