簡體   English   中英

按索引查詢 elasticsearch 嵌套字段(插入順序)

[英]Query elasticsearch nested field by index(order of insert)

我有一個 elasticsearch 文檔,其中包含一些嵌套對象(映射為嵌套字段),例如:

{
  "FirstName": "Test",
  "LastName": "Test",
  "Cost": 322.54,
  "Email": "test@test.com",
  "Vehicles": [
    {
      "Year": 2000,
      "Make": "Mazda",
      "Model": "6"
    },
    {
      "Year": 2012,
      "Make": "Ford",
      "Model": "F150"
    }
  ]
}

我正在嘗試對數組的特定索引進行聚合,例如,我想對福特制造但在第一輛車上的文件的成本求和。

有可能嗎? 互聯網上幾乎沒有關於 elasticsearch 嵌套字段的信息,也沒有關於它們的索引/順序的信息

可以實現您想要的,但您還需要將索引順序添加為嵌套文檔中的字段:

{
  "FirstName": "Test",
  "LastName": "Test",
  "Cost": 322.54,
  "Email": "test@test.com",
  "Vehicles": [
    {
      "Year": 2000,
      "Make": "Mazda",
      "Model": "6",
      "Index": 0
    },
    {
      "Year": 2012,
      "Make": "Ford",
      "Model": "F150",
      "Index": 1
    }
  ]
}

然后您可以使用IndexMake上的兩個條件查詢您的索引,如下所示:

{
  "query": {
    "nested": {
      "path": "Vehicles",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "Vehicles.Index": 0
              }
            },
            {
              "match": {
                "Vehicles.Make": "Ford"
              }
            }
          ]
        }
      }
    }
  }
}

在這種特定情況下,查詢不會產生任何結果,如您所料。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM