具有不同级别属性的弹性搜索6.7嵌套查询

时间:2019-07-29 04:11:07

标签: elasticsearch elasticsearch-6

映射:

{
    "mappings":{
    "feeds": {
        "properties": {
        "feedId" : {"type":"keyword"},
        "feedDate":{"type": "keyword"},
        "account": {
                "type": "nested",
                "properties": {

                    "investment": {
                        "type": "nested",
                        "properties": {

                        }
                    }
                }
            }   

        }
    }
}}

数据集:

{
{
  "feedId": "feed_1",
  "feedDate" : "20180106",
  "account": [
    {
      "id": "account_201",
      "user_id": "alpha@xyz.com",
      "investment": [
        {
          "investmentId":"001",
          "cost":"8000"
        },
        {
         "investmentId":"012",
          "cost": "3400"
        }
      ]
    },
    {
      "id": "account_230",
      "user_id": "xyz@abc.com",
      "investment": [
        {
        "investmentId":045",
          "cost": "1200"
        },
        {
        "investmentId" : "093"
          "cost": "5320"
        }
      ]
    }
  ]
},
{
  "feedId": "feed_13",
  "feedDate" : "20180716",
  "account": [
    {
      "id": "account_1108",
      "user_id": "pqr@xz.com",
      "investment": [
        {
          "investmentId":"121",
          "cost":"3200"
        },
        {
         "investmentId":"112",
          "cost": "3800"
        }
      ]
    },
    {
      "id": "account_2310",
      "user_id": "xz@ac.com",
      "investment": [
        {
        "investmentId":"097",
          "cost": "3100"
        },
        {
        "investmentId" : "123",
          "cost": "5320"
        }
      ]
    }
  ]
}

}

我想要与feedId,feedDate和investementId匹配的提要。 例如

"feedId": "feed_13",
"feedDate" : "20180716",
"investmentId":"121",

结果应为:

{
  "feedId": "feed_13",
  "feedDate" : "20180716"
  "account": [
    {
      "id": "account_1108",
      "user_id": "pqr@xz.com",
      "investment": [
        {
          "investmentId":"121",
          "cost":"3200"
        }]
    }
    ]
}

我的查询是:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "feedDate": "20180716"
          }
        },
        {
          "match": {
            "feedId": "feed_13"
          }
        },
        {
          "nested": {
            "path": "account.investment",
            "query": {
              "match": {
                "account.investment.investmentId": "121"
              }
            },
            "inner_hits": {}
          }

        }
      ]
    }
  }
}

但它正在提供所有投资,而我只想要匹配的投资。如何查询文档以获得预期结果。

我尝试关注questionquestion,但没有运气。

我正在使用6.7版弹性搜索。

0 个答案:

没有答案