0

I'm trying to sort my query by using a random script as suggested here

I'm having a hard time to validate and run my query as it keeps returning me errors.

The query I'm running is:

{
  "sort": {
    "_script": {
      "script": "(doc['_id'].value + salt).hashCode()",
      "type": "string",
      "params": {
        "salt": "some_random_string"
      },
      "order": "asc"
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "exists": {
              "field": "location"
            }
          },
          {
            "terms": {
              "streamIds": [
                796
              ]
            }
          }
        ]
      }
    }
  }
}

The query without the sorting works and returns results.

What am I doing wrong?

I've tried following the queries suggested here but to no avail.

Community
  • 1
  • 1
AlexD
  • 4,062
  • 5
  • 38
  • 65

1 Answers1

0

Found out that Elasticsearch disabled scripting from 1.4.3 and this is why it didn't work.

The code that works:

{
   "query": {
      "function_score": {
         "query": {
            "filtered": {
               "filter": {
                  "and": [
                     {
                        "exists": {
                           "field": "location"
                        }
                     },
                     {
                        "terms": {
                           "streamIds": [
                              796
                           ]
                        }
                     }
                  ]
               }
            }
         },
         "random_score": {}
      }
   }
}
AlexD
  • 4,062
  • 5
  • 38
  • 65