mailerlite / laravel-elasticsearch

An easy way to use the official Elastic Search client in your Laravel applications.
MIT License
909 stars 187 forks source link

failed to parse [function_score] query. array [random_score] is not supported #131

Closed faizangulzar closed 2 years ago

faizangulzar commented 2 years ago
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "categorygroup": {
                  "query": "13",
                  "operator": "or"
                }
              }
            }
          ]
        }
      },
      "random_score": {}
    }
  }
}

When I search via "Postman", it returns random results every time because of using the "random_score" field in the search query. When I search using "cviebrock/laravel-elasticsearch" package in my laravel app, it returns an error :

failed to parse [function_score] query. array [random_score] is not supported

Without the 'random_score' field, it works fine. But I wanted to get Randomly sorted results every time.

I am using :

"php": "^7.3|^8.0",
"cviebrock/laravel-elasticsearch": "^8.0",
Elasticsearch: 7.15.1

Could anyone help me with this? I will be thankful!

cviebrock commented 2 years ago

This certainly isn't an issue with the package, since any queries you create just get passed through to the underlying ES client.

If I had to guess, then either random_score should be an empty array [] and not an empty object {} ... or the ES client is converting the empty object to an empty array by mistake. Depending on how you build your query, you might be able to coerce the conversion properly?

faizangulzar commented 2 years ago

Screenshot_query

Above is the query array before passing in package 'search' method Elasticsearch::search($query);

faizangulzar commented 2 years ago
 "random_score": {
    "seed": "1477072619038"
 }

I used "seed" field under "random_score", now its giving random results.

Thankyou for you response, @cviebrock

cviebrock commented 2 years ago

It might be worth testing your query (without the explicit seed) using the native ES PHP client directly. If it doesn't work there (and I suspect it won't), then raise this as an issue with them: https://github.com/elastic/elasticsearch-php/issues

Not sure if this issue is any help, but it suggests using

'random_score' => new \StdClass()

instead of an empty array. Again, I suspect the official PHP client is serializing things unexpectedly.