0

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

3
  • As per doc "When you get a document back from Elasticsearch, any arrays will be in the same order as when you indexed the document. The _source field that you get back contains exactly the same JSON document that you indexed". Can you elaborate your issue more? Commented Sep 11, 2021 at 13:08
  • Please provide enough code so others can better understand or reproduce the problem. Commented Sep 13, 2021 at 8:29
  • I found a solution let me share it with you , although thanks a lot for your precious time Commented Sep 15, 2021 at 11:32

1 Answer 1

1
{
  "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
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.