2

I'm using SolrNet to access a Solr index where I have a multivalue field called "tags". I want to perform the following pseudo-code query:

(tags:stack)^10 OR (tags:over)^5 OR (tags:flow)^2

where the term "stack" is being boosted by 10, "over" is being boosted by 5 and "flow" is being boosted by 2. The result I'm after is that results with "stack" will appear higher than those with "flow", etc.

The problem I'm having is that say "flow" only appears in a couple of documents, but "stack" appears in loads, then due to a high idf value, documents with "flow" appear above those with "stack".

When this was project was implemented straight in Lucene, I used ConstantScoreQuery and these eliminated the idf based the score solely on the boost value.

How can this be achieved with Solr and SolrNet, where I'm effectivly just passing Solr a query string? If it can't, is there an alternative way I can approach this problem?

Thanks in advance!

sehe
  • 374,641
  • 47
  • 450
  • 633
robinbetts
  • 104
  • 1
  • 10
  • what if you take the score boost off your last clause? `(tags:flow)` I believe that is a constant score query. Effectively, it's a score boost of 1, which is the default. – Josh Dec 15 '10 at 15:39

2 Answers2

9

Solr 5.1 and later has this built into the query parser syntax via the ^= operator.

So just take your original query: (tags:stack)^10 OR (tags:over)^5 OR (tags:flow)^2

And replace the ^ with ^= to change from boosted to constant: (tags:stack)^=10 OR (tags:over)^=5 OR (tags:flow)^=2

Flexo
  • 87,323
  • 22
  • 191
  • 272
Yonik
  • 2,341
  • 1
  • 18
  • 14
3

I don't think there any way to directly express a ConstantScoreQuery in Solr, but it seems that range and prefix queries use ConstantScoreQuery under the hood, so you could try faking a range query, e.g. tags:[flow TO flow]

Alternatively, you could implement your own Solr QueryParser.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • Faking a range query works a treat (for now at least). I may look at implementing a custom query parser if time allows. Thanks. – robinbetts Dec 17 '10 at 09:25