我最好的方法: 嵌套
嵌套数据类型
除了更容易查询之外,它更易于阅读和理解当前分散在不同数组中的那些对象之间的连接。
是的,如果您决定采用这种方法,则必须编辑映射并为整个数据重新编制索引。
映射将如何显示?像这样的东西:
{ "mappings": { "properties": { "last_updated": { "type": "date" }, "country": { "type": "string" }, "records": { "type": "nested", "properties": { "price": { "type": "string" }, "max_occupancy": { "type": "long" }, "type": { "type": "string" }, "availability": { "type": "long" }, "size": { "type": "string" } } } } }}编辑:新文档结构(包含嵌套文档)-
{ "last_updated": "2017-10-25T18:33:51.434706", "country": "Italia", "records": [ { "price": "€ 139", "max_occupancy": 2, "type": "Type 1", "availability": 10, "size": "26 m²" }, { "price": "€ 125", "max_occupancy": 2, "type": "Type 1 - (Tag)", "availability": 10, "size": "35 m²" }, { "price": "€ 120", "max_occupancy": 1, "type": "Type 2", "availability": 10, "size": "47 m²" }, { "price": "€ 108", "max_occupancy": 1, "type": "Type 2 (Tag)", "availability": 10, "size": "31 m²" } ]}现在,使用嵌套查询和内部匹配可以更轻松地查询任何特定条件。例如:
{ "_source": [ "last_updated", "country" ], "query": { "bool": { "must": [ { "term": { "country": "Italia" } }, { "nested": { "path": "records", "query": { "bool": { "must": [ { "range": {"records.max_occupancy": { "gte": 2} } } ] } }, "inner_hits": { "sort": { "records.price": "asc" }, "size": 1 } } } ] } }}条件是:
ItaliaAND
max_occupancy > 2。
内部点击: 按价格升序排序并获得第一个结果 。
希望你会发现它有用



