0

我在查询数据以及 ES 中的嵌套对象时遇到了一些麻烦。更像是 SQL 中的左连接,

SELECT
    select_list
FROM
    T1
LEFT JOIN T2 ON
    join_predicate;

它将根据给定的术语和匹配的嵌套对象返回所有数据。

请看下面的例子。

1.这是我的映射...

{
    mappings: {
        _doc: {
            properties: {
                accountId: { type: 'keyword' },
                history: {
                    type: 'nested',
                    properties: {
                        status: { type: 'keyword' },
                        createdAt: { type: 'date' }
                    }
                }               
            }
        }
    }
}

2. ES内部的数据

[
    {
        accountId: 10001,
        history: {
            status: "NEW",
            createdAt: "2010-01-01"
        }
    },
    {
        accountId: 10002,
        history: {
            status: "NEW",
            createdAt: "2010-01-02"
        }
    },
    {
        accountId: 10001,
        history: {
            status: "FAIL",
            createdAt: "2010-01-03"
        }
    },
    {
        accountId: 10004,
        history: {
            status: "FAIL",
            createdAt: "2010-01-04"
        }
    },  
    {
        accountId: 10001,
        history: {}
    },  
    {
        accountId: 10001
    }
]

3.我需要获取accountId为10001的所有数据(包括嵌套对象) 。

所以基本上它应该返回以下数据。

[
    {
        accountId: 10001,
        history: {
            status: "NEW",
            createdAt: "2010-01-01"
        }
    },
    {
        accountId: 10001,
        history: {
            status: "FAIL",
            createdAt: "2010-01-03"
        }
    },
    {
        accountId: 10001,
        history: {}
    },
    {
        accountId: 10001
    }
]

你能帮助我吗?

4

1 回答 1

2

看到您只需要具有 的文档"accountId":"10001",您只需要使用一个简单的术语查询来查找您要查找的内容:

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "accountId": {
              "value": "10001"
            }
          }
        }
      ]
    }
  }
}

您的响应将在您摄取时返回源文档。

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 0.44183275,
    "hits" : [
      {
        "_index" : "somenested",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.44183275,
        "_source" : {
          "accountId" : "10001",
          "history" : {
            "status" : "NEW",                <--- Doc 1
            "createdAt" : "2010-01-01"
          }
        }
      },
      {
        "_index" : "somenested",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.44183275,
        "_source" : {
          "accountId" : "10001",             <--- Doc 2
          "history" : {
            "status" : "FAIL",
            "createdAt" : "2010-01-03"
          }
        }
      },
      {
        "_index" : "somenested",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.44183275,
        "_source" : {
          "accountId" : "10001",             <--- Doc 3
          "history" : { }
        }
      },
      {
        "_index" : "somenested",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.44183275,
        "_source" : {
          "accountId" : "10001"              <--- Doc 4
        }
      }
    ]
  }
}

希望这可以帮助!

于 2019-06-05T07:32:01.253 回答