Skip to content
Advertisement

Custom Sort Order by Ids as array in elastic search

The scenario is like this we send the products id’s in array as [101,102,103,…..so on ] but in response we aren’t getting the results in the same order is there any way to get the records in the same order as we send in the elastic search query

Advertisement

Answer

{
  "my_videos": {
    "mappings": {
      "doc": {
        "properties": {
          "createdAt": {
            "type": "date"
          },
          "description": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "duration": {
            "type": "long"
          },
          "id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "lastUpdatedByUserId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "status": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "tags": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "updatedAt": {
            "type": "date"
          },
          "url": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}



GET my_videos/doc/_search
    {
        "query": {
            "bool": {
                "should": [{
                        "match": {
                            "id.keyword": "01cm7kr0px0tmyzkmsjb55xd3a"
                        }
                    },
                    {
                        "match": {
                            "id.keyword": "01cktwwyfnyt9d2nqj9ycwxcme"
                        }
                    },
                    {
                        "match": {
                            "id.keyword": "01chyvzv678r1h0y0rx4e4bv8t"
                        }
                    }
                ],
                "minimum_should_match": 1
            }
        },
        "sort": [{
            "_script": {
                "type": "number",
                "script": {
                    "lang": "painless",
                    "inline": "if(params.scores.containsKey(doc['id.keyword'].value)) { return params.scores[doc['id.keyword'].value];} return 100000;",
                    "params": {
                        "scores": {
                            "01cm7kr0px0tmyzkmsjb55xd3a": 0,
                            "01cktwwyfnyt9d2nqj9ycwxcme": 1,
                            "01chyvzv678r1h0y0rx4e4bv8t": 2
                        }
                    }
                },
                "order": "asc"
            }
        }],
        "from": 0,
        "size": 10
    }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement