在弹性搜索中结合内部命中?

时间:2018-02-27 20:50:40

标签: elasticsearch

我目前有一个在产品中具有嵌套数据类型的数据集,这些数据集都列在不同的供应商中。我有各种查询来检查嵌套产品数组中的搜索条件,理想情况下,我希望能够组合所有内部命中,以便我可以对分数排名和价格等内容进行排序。目前,搜索结果以每个文档为基础返回。是否可以在弹性搜索中组合内部命中,以便我只获得所有匹配产品的列表?

示例查询

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

示例输出

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

1 个答案:

答案 0 :(得分:1)

没关系,我应该更好地关注弹性搜索文档,其中指出:

  

搜索请求返回整个文档,而不仅仅是匹配   嵌套文件。虽然有计划支持返回   与根文档匹配的最佳匹配嵌套文档,这是   尚不支持。

我认为亲子关系可能是解决这个问题的方法。