0

From the following JSON I would like to know if elasticsearch has the ability to return unique results even if it is repeated in the JSON.

i.e Output

{
  "hits" : {
    "hits" : [
      {
        "inner_hits" : {
          "data.addresses" : {
            "hits" : {
              "hits" : [
                {
                  "_source" : {
                    "name" : "Peter"
                  },
                  "inner_hits" : {
                    "data.addresses.services" : {
                      "hits" : {
                        "hits" : [
                          {
                            "_source" : {
                              "number" : "1",
                              "version" : "1.0",
                              "desc" : "Texas"
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  "_source" : {
                    "name" : "Peter"
                  },
                  "inner_hits" : {
                    "data.addresses.services" : {
                      "hits" : {
                        "hits" : [
                          {
                            "_source" : {
                              "number" : "1",
                              "version" : "1.0",
                              "city" : "Texas"
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  "_source" : {
                    "name" : "Mike"
                  },
                  "inner_hits" : {
                    "data.addresses.services" : {
                      "hits" : {
                        "hits" : [
                          {
                            "_source" : {
                              "number" : "2",
                              "version" : "2.0",
                              "city" : "Iowa"
                            }
                          }
                        ]
                      }
                    }
                  }
                },

Desired result

    {
        "inner_hits" : {
          "data.addresses" : {
            "hits" : {
              "hits" : [
                {
                  "_source" : {
                    "name" : "Peter"
                  },
                  "inner_hits" : {
                    "data.addresses.services" : {
                      "hits" : {
                        "hits" : [
                          {
                            "_source" : {
                              "number" : "1",
                              "version" : "1.0",
                              "desc" : "Texas"
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  "_source" : {
                    "name" : "Mike"
                  },
                  "inner_hits" : {
                    "data.addresses.services" : {
                      "hits" : {
                        "hits" : [
                          {
                            "_source" : {
                              "number" : "2",
                              "version" : "2.0",
                              "city" : "Iowa"
                            }
                          }
                        ]
                      }
                    }
                  }
                },

I am looking in the Elasticsearch documentation if there is a possibility to filter the repeated results from the query. Does anyone know if the function exists?

Miguel Barrios
  • 453
  • 7
  • 17

1 Answers1

1

You can try the collapsed parameter if you got one field of reference (or you can create one for this need). You can also play with term aggregations and top_hits aggs like describe in this post : Dedup elasticsearch results using multiple fields as unique key

Jaycreation
  • 2,029
  • 1
  • 15
  • 30