基于过滤器从多个数组中提取记录
Extract record from multiple arrays based on a filter
我在 ElasticSearch 中有以下结构的文档:
"_source": {
"last_updated": "2017-10-25T18:33:51.434706",
"country": "Italia",
"price": [
"€ 139",
"€ 125",
"€ 120",
"€ 108"
],
"max_occupancy": [
2,
2,
1,
1
],
"type": [
"Type 1",
"Type 1 - (Tag)",
"Type 2",
"Type 2 (Tag)",
],
"availability": [
10,
10,
10,
10
],
"size": [
"26 m²",
"35 m²",
"47 m²",
"31 m²"
]
}
}
基本上,明细记录被拆分成5个数组,同一条记录的字段在5个数组中的索引位置相同。从示例数据中可以看出,有 5 个数组(价格,max_occupancy,类型,可用性,大小)包含与同一元素相关的值。我想提取 max_occupancy 字段大于或等于 2 的元素(如果没有 2 的记录,如果没有 3,则抓取 3,...),价格较低,在在这种情况下,记录并将结果放入新的 JSON 对象中,如下所示:
{
"last_updated": "2017-10-25T18:33:51.434706",
"country": "Italia",
"price: ": "€ 125",
"max_occupancy": "2",
"type": "Type 1 - (Tag)",
"availability": 10,
"size": "35 m²"
}
基本上结果结构应该显示提取的记录(在本例中是所有数组的第二个索引),并向其中添加一般信息(字段:"last_updated","country") .
是否可以从弹性搜索中提取这样的结果?我需要执行什么样的查询?
有人可以建议最好的方法吗?
我的最佳方法:与 Nested Datatype
嵌套
除了更容易查询之外,更容易阅读和理解那些目前分散在不同数组中的对象之间的联系。
是的,如果您决定采用这种方法,则必须编辑 mapping 并重新索引整个数据。
映射会是什么样子?像这样:
{
"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²"
}
]
}
现在,使用 Nested Query and Inner Hits 可以更轻松地查询任何特定条件。例如:
{
"_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
}
}
}
]
}
}
}
条件是:Italia
AND max_occupancy > 2
.
内部命中:按价格升序排序,取第一个结果。
希望你会发现它有用
我在 ElasticSearch 中有以下结构的文档:
"_source": {
"last_updated": "2017-10-25T18:33:51.434706",
"country": "Italia",
"price": [
"€ 139",
"€ 125",
"€ 120",
"€ 108"
],
"max_occupancy": [
2,
2,
1,
1
],
"type": [
"Type 1",
"Type 1 - (Tag)",
"Type 2",
"Type 2 (Tag)",
],
"availability": [
10,
10,
10,
10
],
"size": [
"26 m²",
"35 m²",
"47 m²",
"31 m²"
]
}
}
基本上,明细记录被拆分成5个数组,同一条记录的字段在5个数组中的索引位置相同。从示例数据中可以看出,有 5 个数组(价格,max_occupancy,类型,可用性,大小)包含与同一元素相关的值。我想提取 max_occupancy 字段大于或等于 2 的元素(如果没有 2 的记录,如果没有 3,则抓取 3,...),价格较低,在在这种情况下,记录并将结果放入新的 JSON 对象中,如下所示:
{
"last_updated": "2017-10-25T18:33:51.434706",
"country": "Italia",
"price: ": "€ 125",
"max_occupancy": "2",
"type": "Type 1 - (Tag)",
"availability": 10,
"size": "35 m²"
}
基本上结果结构应该显示提取的记录(在本例中是所有数组的第二个索引),并向其中添加一般信息(字段:"last_updated","country") .
是否可以从弹性搜索中提取这样的结果?我需要执行什么样的查询?
有人可以建议最好的方法吗?
我的最佳方法:与 Nested Datatype
嵌套除了更容易查询之外,更容易阅读和理解那些目前分散在不同数组中的对象之间的联系。
是的,如果您决定采用这种方法,则必须编辑 mapping 并重新索引整个数据。
映射会是什么样子?像这样:
{
"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²"
}
]
}
现在,使用 Nested Query and Inner Hits 可以更轻松地查询任何特定条件。例如:
{
"_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
}
}
}
]
}
}
}
条件是:Italia
AND max_occupancy > 2
.
内部命中:按价格升序排序,取第一个结果。
希望你会发现它有用