简体   繁体   中英

Elasticsearch query with sort by script [syntax]

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.

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": {}
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM