使用elasticsearch查询hashmap结构

发布于 2025-01-15 03:31:45 字数 1254 浏览 2 评论 0原文

我有两个关于在elasticsearch 中映射和查询java hashmap 的问题。

这种映射在elasticsearch中是否有意义(这是映射哈希图的正确方法)?:

{
  "properties": {
    "itemsMap": {
      "type": "nested",
      "properties": {
        "key": {
          "type": "date",
          "format": "yyyy-MM-dd"
        },
        "value": {
          "type": "nested",
          "properties": {
            "itemVal1": {
              "type": "double"
            },
            "itemVal2": {
              "type": "double"
            }
          }
        }
      }
    }
  }
}

这是一些示例数据:

{
  "itemsMap": {
    "2021-12-31": {
      "itemVal1": 100.0,
      "itemVal2": 150.0,
    },
    "2021-11-30": {
      "itemVal1": 200.0,
      "itemVal2": 50.0,
    }
  }
}

我的查询似乎不起作用。例如:

{
  "query": {
    "nested": {
      "path": "itemsMap",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "itemsMap.key": "2021-11-30"
              }
            }
          ]
        }
      }
    }
  }
}

我做错了什么吗?我如何查询这样的结构?如果有必要,我可以更改映射。

谢谢

I have two questions regarding mapping and querying a java hashmap in elasticsearch.

Does this mapping make sense in elasticsearch (is it the correct way to map a hashmap)?:

{
  "properties": {
    "itemsMap": {
      "type": "nested",
      "properties": {
        "key": {
          "type": "date",
          "format": "yyyy-MM-dd"
        },
        "value": {
          "type": "nested",
          "properties": {
            "itemVal1": {
              "type": "double"
            },
            "itemVal2": {
              "type": "double"
            }
          }
        }
      }
    }
  }
}

Here is some example data:

{
  "itemsMap": {
    "2021-12-31": {
      "itemVal1": 100.0,
      "itemVal2": 150.0,
    },
    "2021-11-30": {
      "itemVal1": 200.0,
      "itemVal2": 50.0,
    }
  }
}

My queries don't seem to work. For example:

{
  "query": {
    "nested": {
      "path": "itemsMap",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "itemsMap.key": "2021-11-30"
              }
            }
          ]
        }
      }
    }
  }
}

Am I doing something wrong? How can I query such a structure? I have the possibility to change the mapping if it's necessary.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热鲨 2025-01-22 03:31:46

太长了;

按照您上传数据的方式,key 中不会存储任何内容。
您将拥有名为 2021-11-30 的字段...并且 key 将为空。

要么您的“日期”数量有限并且这是一个可行的选项(少于 1000),否则从长远来看您的格式不可行。

如果您不想更改文档,请使用以下查询

GET /71525899/_search
{
  "query": {
    "nested": {
      "path": "itemsMap",
      "query": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "itemsMap.2021-12-31"
              }
            }
          ]
        }
      }
    }
  }
}

来了解

如果您通过查询索引来检查映射,

GET /<index_name>/_mapping

您将看到日期之后的字段名称数量将会增长。

在您的所有文档中,itemsMap.key 将为空。 (这解释了为什么我之前的答案不起作用。

一个更可行的选择

保留您的映射,更新文档的形状。

它们看起来像

{
  "itemsMap": [
    { 
      "key": "2021-12-31", 
      "value": { "itemVal1": 100, "itemVal2": 150 }
    },
    { 
      "key": "2021-11-30",
      "value": { "itemVal1": 200, "itemVal2": 50 }
    }
  ]
}

DELETE /71525899


PUT /71525899/
{
  "mappings": {
    "properties": {
      "itemsMap": {
        "type": "nested",
        "properties": {
          "key": {
            "type": "date",
            "format": "yyyy-MM-dd"
          },
          "value": {
            "type": "nested",
            "properties": {
              "itemVal1": {
                "type": "double"
              },
              "itemVal2": {
                "type": "double"
              }
            }
          }
        }
      }
    }
  }
}

POST /_bulk
{"index":{"_index":"71525899"}}
{"itemsMap":[{"key":"2021-12-31", "value": {"itemVal1":100,"itemVal2":150}},{"key":"2021-11-30", "value":{"itemVal1":200,"itemVal2":50}}]}
{"index":{"_index":"71525899"}}
{"itemsMap":[{"key":"2022-12-31", "value": {"itemVal1":100,"itemVal2":150}},{"key":"2021-11-30", "value":{"itemVal1":200,"itemVal2":50}}]}
{"index":{"_index":"71525899"}}
{"itemsMap":[{"key":"2021-11-31", "value": {"itemVal1":100,"itemVal2":150}},{"key":"2021-11-30", "value":{"itemVal1":200,"itemVal2":50}}]}



GET /71525899/_search
{
  "query": {
    "nested": {
      "path": "itemsMap",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "itemsMap.key": "2021-12-31"
              }
            }
          ]
        }
      }
    }
  }
}

TLDR;

The way you are uploading your data, nothing is stored in key.
You will have fields named 2021-11-30 ... and key is going to be empty.

Either you have a limited amount of "dates" and this is a viable options (less than 1000) else your format is not viable on the long run.

If you don't want to change your doc, here is the query

GET /71525899/_search
{
  "query": {
    "nested": {
      "path": "itemsMap",
      "query": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "itemsMap.2021-12-31"
              }
            }
          ]
        }
      }
    }
  }
}

To understand

If you inspect the mapping by querying the index

GET /<index_name>/_mapping

You will see that the number of fields name after your date is going to grow.

And in all your doc, itemsMap.key is going to be empty. (this explain why my previous answer did not work.

A more viable option

Keep your mapping, update the shape of your docs.

They will look like

{
  "itemsMap": [
    { 
      "key": "2021-12-31", 
      "value": { "itemVal1": 100, "itemVal2": 150 }
    },
    { 
      "key": "2021-11-30",
      "value": { "itemVal1": 200, "itemVal2": 50 }
    }
  ]
}

DELETE /71525899


PUT /71525899/
{
  "mappings": {
    "properties": {
      "itemsMap": {
        "type": "nested",
        "properties": {
          "key": {
            "type": "date",
            "format": "yyyy-MM-dd"
          },
          "value": {
            "type": "nested",
            "properties": {
              "itemVal1": {
                "type": "double"
              },
              "itemVal2": {
                "type": "double"
              }
            }
          }
        }
      }
    }
  }
}

POST /_bulk
{"index":{"_index":"71525899"}}
{"itemsMap":[{"key":"2021-12-31", "value": {"itemVal1":100,"itemVal2":150}},{"key":"2021-11-30", "value":{"itemVal1":200,"itemVal2":50}}]}
{"index":{"_index":"71525899"}}
{"itemsMap":[{"key":"2022-12-31", "value": {"itemVal1":100,"itemVal2":150}},{"key":"2021-11-30", "value":{"itemVal1":200,"itemVal2":50}}]}
{"index":{"_index":"71525899"}}
{"itemsMap":[{"key":"2021-11-31", "value": {"itemVal1":100,"itemVal2":150}},{"key":"2021-11-30", "value":{"itemVal1":200,"itemVal2":50}}]}



GET /71525899/_search
{
  "query": {
    "nested": {
      "path": "itemsMap",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "itemsMap.key": "2021-12-31"
              }
            }
          ]
        }
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文