Find count of nested objects matching with query


(Jagdish) #1

Is there a way we can find #of nested objects matching with query criteria.

{ "customername": "John",
"order": [
{ "productname":"books", "qty":5, "orderdate": 2015-12-01},
{ "productname":"pens", "qty":15, "orderdate": 2015-12-01},
{ "productname":"papers", "qty":100, "orderdate": 2015-12-01}
]
}

  1. how can we find if we have customer with order where qty > 100
  2. how many items is there in order with qty > 100

Thanks !!!


(Isabel Drost-Fromm) #2

With the help of a range query this is actually fairly simple (for more information see here https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

DELETE index

PUT index/type/1
{
    "customername": "John", 
    "order": [ 
        { "productname":"books", "qty":5, "orderdate": "2015-12-01"},
        { "productname":"pens", "qty":15, "orderdate": "2015-12-01"},
        { "productname":"papers", "qty":100, "orderdate": "2015-12-01"},
        { "productname":"disks", "qty":100, "orderdate": "2015-12-01"}
    ]

}

GET _search
{
    "query": {
        "range" : {
            "order.qty" : 
                {"gte": 100}
        }
    }
}

IIRC for this you need to help Elasticsearch determine the correct mapping. You will probably want to specify that you'd like to use nested objects and execute a count query against those. For more information check out https://www.elastic.co/guide/en/elasticsearch/reference/2.x/nested.html

Hope this helps,
Isabel


(system) #3