【问题标题】:Extract record from multiple arrays based on a filter基于过滤器从多个数组中提取记录
【发布时间】:2018-04-08 21:01:06
【问题描述】:

我在 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 抢 4,...),价格较低,在这种情况下为记录并将结果放入一个新的 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”)。

是否可以从弹性搜索中提取这样的结果?我需要执行什么样的查询?

有人可以建议最好的方法吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    我最好的方法:嵌套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 QueryInner 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

    Inner hits:按价格升序排序,得到第一个结果

    希望对你有用

    【讨论】:

    • 在将数据插入索引之前是否必须设置映射?设置映射后,插入该索引的所有后续数据将相应地自动映射?有没有办法在没有映射的情况下实现它?
    • 1) 是 2) 您必须添加具有新嵌套结构的文档 我将用示例编辑我的答案 3) 否
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2021-08-25
    • 2021-01-12
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多