开发者

How to do a constant score query in Solr

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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜