LAB
LAB

Reputation: 83

Combine inner hits in elasticsearch?

I currently have a dataset that features a nested datatype in products, these are all listed within different vendors. I have various queries that check for search terms within the nested products array, ideally I want to be able to combine all the inner hits so that I can sort on such things as score rankings and price. At the moment the search results come back on a per document basis. Is it possible to combine inner hits in elasticsearch so that I get just a list of all the matching products?

Example Query

{
  "_source": {
    "includes": [ "*" ],
    "excludes": [ "products" ]
  },
  "query": {
    "nested": {
      "path": "products",
      "inner_hits": {     
        "size": 10,
        "_source": [
          "title"
        ]
      },
      "query": {
        "bool": {
          "must": [
            { 
                "match": {
                    "products.title" : {
                        "query": "Dress",
                        "fuzziness" : 0
                    }
                }
            }
          ]
        }
      }
    }
  }
}

Example output

{
    "took": 477,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.9072125,
        "hits": [
            {
                "_index": "shopit",
                "_type": "businesses",
                "_id": "5a806c7af36d28314de953ff",
                "_score": 2.9072125,
                "_source": {
                    "name": "Argos",
                    "locations": [
                        {
                            "lon": -2.242797,
                            "lat": 53.482952
                        }
                    ]
                },
                "inner_hits": {
                    "products": {
                        "hits": {
                            "total": 3,
                            "max_score": 3.0782251,
                            "hits": [
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a806c7af36d28314de953ff",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 3348
                                    },
                                    "_score": 3.0782251,
                                    "_source": {
                                        "title": "HOME Set of 2 Dress Covers - White"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a806c7af36d28314de953ff",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 2599
                                    },
                                    "_score": 3.0782251,
                                    "_source": {
                                        "title": "Chad Valley Designabear Spotty Dress Outfit"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a806c7af36d28314de953ff",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 771
                                    },
                                    "_score": 2.5651875,
                                    "_source": {
                                        "title": "Melissa and Doug Abby & Emma Magnetic Wooden Dress Up"
                                    }
                                }
                            ]
                        }
                    }
                }
            },
            {
                "_index": "shopit",
                "_type": "businesses",
                "_id": "5a5c3beb734d1d3471839b1d",
                "_score": 2.3227787,
                "_source": {
                    "name": "Superdry",
                    "locations": [
                        {
                            "lon": -2.241703,
                            "lat": 53.483469
                        }
                    ]
                },
                "inner_hits": {
                    "products": {
                        "hits": {
                            "total": 186,
                            "max_score": 2.378731,
                            "hits": [
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6420
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Alexia Off Shoulder Dress"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6417
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Erin Festival Skater Dress "
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6416
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Erin Racer Dress "
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6415
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Alice Knot Dress"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6412
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Alice Knot Dress"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6389
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Lagoon Logo Midi Dress"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6388
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "50's Boardwalk Dress "
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6386
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "50's Boardwalk Dress "
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6385
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Graphic Sweat Dress"
                                    }
                                },
                                {
                                    "_index": "shopit",
                                    "_type": "businesses",
                                    "_id": "5a5c3beb734d1d3471839b1d",
                                    "_nested": {
                                        "field": "products",
                                        "offset": 6382
                                    },
                                    "_score": 2.378731,
                                    "_source": {
                                        "title": "Breton Bardot Stripe Dress"
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

Upvotes: 0

Views: 1481

Answers (1)

LAB
LAB

Reputation: 83

Nevermind, I should of paid better attention to the elasticsearch documentation which states:

Search requests return the whole document, not just the matching nested documents. Although there are plans afoot to support returning the best -matching nested documents with the root document, this is not yet supported.

I think parent-child relationships are probably the way to go with this.

Upvotes: 2

Related Questions