查询嵌套对象以及查询Elasticsearch

时间:2019-06-05 06:10:13

标签: elasticsearch elasticsearch-nested

我在查询数据以及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
    }
]

你能帮我吗?

1 个答案:

答案 0 :(得分:2)

看到您只需要包含"accountId":"10001"的文档,您只需要使用一个简单的Term Query就可以了:

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

希望这会有所帮助!

相关问题