Search code examples
redisearch

Tag Field search scoring in RediSearch


I am performing a REDISEARCH tag search only query like this:

'@tags:{Food|Restaurant|Chicago}'

I would like to sort the result based on number of successful matches of these tags in the document.

So that:

  • a document containing all 3 tags would rank first
  • a document matching only 2 of these would rank second
  • a document mathcing only 1 would rank third.

Is this possible ? If so, please how can I perform such a sort ?

Thanks!


Solution

  • At current state, this isn't directly in the feature set of RediSearchemphasized text. Matching tags are not counted towards the score. An example:

    127.0.0.1:6379> ft.create test SCHEMA mytags TAG
    OK
    127.0.0.1:6379> ft.add test one 1 FIELDS mytags "aa"
    OK
    127.0.0.1:6379> ft.add test two 1 FIELDS mytags "aa,bb"
    OK
    127.0.0.1:6379> ft.add test three 1 FIELDS mytags "aa,bb,cc"
    OK
    127.0.0.1:6379> ft.add test four 1 FIELDS mytags "aa,bb"
    OK
    

    When you query, add the WITHSCORES argument:

    127.0.0.1:6379> FT.SEARCH test "@mytags:{aa|bb|cc}" WITHSCORES
     1) (integer) 4
     2) "four"
     3) "inf"
     4) 1) "mytags"
        2) "aa,bb"
     5) "three"
     6) "inf"
     7) 1) "mytags"
        2) "aa,bb,cc"
     8) "two"
     9) "inf"
    10) 1) "mytags"
        2) "aa,bb"
    11) "one"
    12) "inf"
    13) 1) "mytags"
        2) "aa"
    

    The "inf" is indicating that there is no score attributed to this match so the order is not taken into account here for scoring purposes.

    I also attempted to do this with the aggregation engine and could not find a way to create a pipeline that would effectively count matched tags.

    This seems like a straight forward use, I did file an issue for this.

    A couple of workarounds:

    1. You maybe be able to use a TEXT field and optional clauses ("~Food|~Restaurant|~Chicago"), but a text field will have slightly different properties.
    2. RedisGears could be used to compose a few queries into a single result.