查询嵌套对象以及查询 Elasticsearch

Query nested objects along with the query Elasticsearch

我在查询数据以及 ES 中的嵌套对象时遇到了一些问题。 更像是左加入 SQL,

SELECT
    select_list
FROM
    T1
LEFT JOIN T2 ON
    join_predicate;

这将 return 根据给定的术语和匹配的嵌套对象的所有数据。

请看下面的例子。

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.

所以基本上它应该return低于数据。

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

你能帮帮我吗?

鉴于您只需要具有 "accountId":"10001" 的文档,您只需使用一个简单的 Term Query 来查找您要查找的内容:

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

您的回复将 return 源文档与您摄取它时一样。

{
  "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
        }
      }
    ]
  }
}

希望对您有所帮助!