Query nested objects along with the query Elasticsearch

Keywords´╝Ü elasticsearch elasticsearch-nested

Question: 

I am having some trouble with querying data along with the nested objects in ES. More like left join in SQL,

SELECT
    select_list
FROM
    T1
LEFT JOIN T2 ON
    join_predicate;

which will return the all the data according to given term and matching nested objects.

Please see the below example.

1. Here is my mapping...

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

2. Data inside 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. I need to get all the data (including nested objects) where accountId is 10001.

So basically it should return below data.

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

Can you help me?

Answers: