Use Random_score along with multiple filter

Hello.

I am quite new to Elasticsearch, and I've been trying my way around, but I just can't seem to get this to work exactly the way I want.

I wish to to retrieve 3 random documents based on filters.
Lets just take two, so both of these needs to be true

  • Where "propertyName" == "hot"
  • Where "active" == true

Along with then it should use the random_score function, I've tried combining those two, but with no help.

https://pastebin.com/vYqJPA1i

Any help would be appreciated!

How is your query behaving relative to what you want? What's wrong about it?

I think I'd probably structure the query a bit more like this, although your example looks like it would work too.

{
  "size": 3,
  "query": {
    "bool": {
      "filter": [
        {
            "term" : {"propertyName" : "hot"}
        },
        {
            "term" : {"active" : true}
        }
      ],
      "must": {
        {
          "function_score": {
            "functions": [
                {
                    "random_score": {
                        "seed": seed
                    }
                }
            ],
            "boost_mode": "replace"
          }
        }
      }
    }    
  }
}

E.g. use a boolean query, put the term's inside the filter clause, and then use the function_score in the must clause so it does random scoring.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.