2

I am programatically constructing a BoostedQuery and I am creating two TermQuery instances which will be added to BoostedQuery instance:

Query query1 = new TermQuery(new Term("body", "new"));
Query query2 = new TermQuery(new Term("body", "york"));

But I am wondering if I could add some weight to this like I can do in qf field in Solr. Here is an example:

body:new^0.1
body:york^0.1

Any help or a pointer would be appreciated.

kee
  • 10,969
  • 24
  • 107
  • 168

1 Answers1

4

Query.setBoost

query1.setBoost(0.1);

Or in a parsed query, using StandardQueryParser, precisely as you specified in your question:

body:new^0.1

See also, Lucene scoring basics

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • is it possible to add different weights to query terms? such as new=0.1 and york=0.5? how can I do this? – samsamara Jan 14 '16 at 07:17
  • can you help me with this question? tnx http://stackoverflow.com/questions/34783819/assigning-different-weights-to-different-query-terms-in-lucene – samsamara Jan 14 '16 at 07:27
  • @KillBill - When you set the boost on `query1`, it doesn't affect `query2`. You can set each subquery to have whatever boost you want. Setting every subquery to the have same boost would actually be fairly pointless. – femtoRgon Jan 14 '16 at 07:40
  • i want to give different weights to each term in the query? as i've mentioned in my question – samsamara Jan 14 '16 at 07:47
  • 1
    @KillBill - Right, so give each `TermQuery` a different boost. If you are using the `QueryParser`, the syntax supports this easily as well, see [the documentation](https://lucene.apache.org/core/5_3_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Boosting_a_Term) – femtoRgon Jan 14 '16 at 07:49
  • if i set a negative score like, body:new^-0.1, throws an Lexical error saying, Encountered: "-" (45), after : "". do you know how to solve this? – samsamara Jan 21 '16 at 06:39
  • @KillBill - That's not supported by the `QueryParser`. You can set a negative boost with the `Query.setBoost` method. – femtoRgon Jan 21 '16 at 14:15