Search code examples
c#solrsolr-query-syntax

Solr TermFreq with conditional Filterquery


Hi Im trying to build a query in c# using solr:

Query.Append($"&fq={{frange l=3}}termfreq(Description,'keyword') OR {{frange l=1}}termfreq(title, 'keyword'")"

While this one below works 👇:

Query.Append($"&fq={{frange l=3}}termfreq(Description,'keyword')")

The occurrence should be 3 times in the description OR 1 time in title. But executing the query gives a syntax error. Using termfreq once works but with conditions, it's failing.

Any help? Thanks.


Solution

  • You can solve this by collapsing it into one single frange:

    {!frange l=3}add(termfreq(Description,'keyword'),mul(termfreq(Title,'keyword'), 3))
    

    This will give you 3 as a value if it's three times in Description, or if it's at least once in Title (being the sum of occurences of keyword in Description and occurences of keyword in Title - multiplied by three. So if it occurs once in the title, you get 1 * 3 => 3, but for the description field you don't multiply at all - meaning you'll have to have it occur three times to get to three.