searchsolrlucenefull-text-searchrelevance

Using multi-layer queries in Solr


The Solr "qf" parameter works as follows:

Let's say I have: query = "sid" and qf = [field1, field1_edge, field2, field2_edge].

The Solr score is calculated as follows:

max(f1, f1_e, f2, f2_e) + tie * (sum of other 3 fields) where: "tie" lies in [0,1]

Let's call: winner1 = field with max(f1, f1_e) and winner2 = field with max(f2, f2_e)

I would like to score a given query in Solr as follows:

score1 = winner1_score + tie_1 * loser1_score
score2 = winner2_score + tie_1 * loser2_score

final score = score1 + tie_2 * score2

Effectively, I want to apply qf in two layers (taking tie_1 = 0 and tie_2 = 1). What are my options to implement this idea of relevance? I think neither "qf" parameter nor function boosts support this. Thanks!


Solution

  • It seems to me that's the way to do it is to use the query function which allows you to apply functions to queries. You combine this with nested query parsers which allows you to run multiple dismax queries.

    You can do something like this (where you set tie1 and tie2 according to what you want):

    q=_val_:"add(query($qq1),product(query($qq2),${tie2}))"
    qq1={!edismax qf='field1 field1_edge' v='sid' tie=${tie1}}
    qq2={!edismax qf='field2 field2_edge' v='sid' tie=${tie1}}
    tie1=0.5
    tie2=0.3
    

    If you used Solr 7.2 (or higher) you also need to set uf=_query_ * in order for the _val_ hook to work.

    P.S: it should be possible (though I haven't tested it) to move the content of q into the qf parameter and that way you don't have to use the _val_ hook:

    qf=add(query($qq1),product(query($qq2),${tie2}))