Redtopia
Redtopia

Reputation: 5247

Elasticsearch - return aggregation to match a specific value?

Using Elasticsearch 2, is it possible to return an aggregation where a document category matches a specific field value? For example, I want to get all the categories where categories.type = "application".

My mapping looks like this:

"mappings": {
    "products": {
        "_all": {
            "enabled": true
        }, 
        "properties": { 
            "title": {
                "type": "string"
            },
            "categories": {
                "type":"nested",
                "properties": {
                    "type": {
                        "type": "string", 
                        "index": "not_analyzed"
                    },
                    "name": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

My query looks like this, which returns all category types, but I want to filter just the ones where categories.type = "application".

{  
    "query":{  
        "multi_match": {  
            "query": "Sound",
            "fields": [  
                "title"
            ]
        }
    },
    "aggs":{
        "Applications": {
            "nested": {
                "path": "categories"
            },
            "aggs": {
                "meta": {
                    "terms": {
                        "field": "categories.type"
                    },
                    "aggs": {
                        "name": {
                            "terms": {
                                "field": "categories.name"
                            }
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 4

Views: 7335

Answers (2)

alpert
alpert

Reputation: 4655

You can use aggregation filter if I understand correctly:

{   
    size : 50,
    "query":{  
        "multi_match": {  
            "query": "Sound",
            "fields": [  
                "title"
            ]
        }
    },
    "aggs":{
        "Applications": {
            "nested": {
                "path": "categories"
            },
            "aggs": {
                "meta": {
                    "filter" : {
                        "term" : {
                            "categories.type" : "application"
                        }
                    },
                    "aggs": {
                        "name": {
                            "terms": {
                                "field": "categories.name"
                            }
                        }
                    }
                }
            }
        }
    }
}

Hope that helpes.

Upvotes: 4

pkhlop
pkhlop

Reputation: 1844

You just need to replace "include": ".*" to "include": "application"

{  
    "query":{  
        "multi_match": {  
            "query": "Sound",
            "fields": [  
                "title"
            ]
        }
    },
    "aggs":{
        "Applications": {
            "nested": {
                "path": "categories"
            },
            "aggs": {
                "meta": {
                    "terms": {
                        "field": "categories.type"
                        , "include": "application"
                    },
                    "aggs": {
                        "name": {
                            "terms": {
                                "field": "categories.name"
                            }
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 1

Related Questions