-1

I'm trying to build a facets system using Elasticsearch to display the number of documents which match a query.

I'm currently doing this query on /_search?search_type=count:

{
    "query": {
        "query_string": {
            "query": "status:(1|2) AND categories:A"
        }
    },
    "aggs": {
        "all_products": {
            "global": {},
            "aggs": {
                "countries": {
                    "aggs": {
                        "counter": {
                            "terms": ["min_doc_count": 0, "field": "country"],
                            "aggs": ["unique": ["cardinality": ["field": "id"]]]
                        }
                    }
                },
                "categories": {
                    "aggs": {
                        "counter": {
                            "terms": ["min_doc_count": 0, "field": "category"],
                            "aggs": ["unique": ["cardinality": ["field": "id"]]]
                        }
                    }
                },
                "statuses": {
                    "aggs": {
                        "counter": {
                            "terms": ["min_doc_count": 0, "field": "status"],
                            "aggs": ["unique": ["cardinality": ["field": "id"]]]
                        }
                    }
                }
            }
        }
    }
}

the documents have the following structure:

{
    "id": 123,
    "name": "Title",
    "categories": ["A", "B", "C"],
    "country": "United Kingdom",
    "status": 1
}

so the output I'm looking for should be:

Country

  • UK: 123
  • USA: 1000

Category

  • Motors: 23
  • Fashion: 1100

Status

  • Active: 1120
  • Not Active: 3

I don't know how to filter properly the aggregations, because right now they are counting all the document in the specified field, without considering the query status:(1|2) AND categories:A.

The elastic version is 1.7.2.

2 Answers 2

0

You simply need to remove global aggregation since it is not influenced by the query, just move your countries, categories and statuses aggregations at the top level like this:

{
    "query": {
        "query_string": {
            "query": "status:(1|2) AND categories:A"
        }
    },
    "aggs": {
            "countries": {
                "aggs": {
                    "counter": {
                        "terms": ["min_doc_count": 0, "field": "country"],
                        "aggs": ["unique": ["cardinality": ["field": "id"]]]
                    }
                }
            },
            "categories": {
                "aggs": {
                    "counter": {
                        "terms": ["min_doc_count": 0, "field": "category"],
                        "aggs": ["unique": ["cardinality": ["field": "id"]]]
                    }
                }
            },
            "statuses": {
                "aggs": {
                    "counter": {
                        "terms": ["min_doc_count": 0, "field": "status"],
                        "aggs": ["unique": ["cardinality": ["field": "id"]]]
                    }
                }
            }
        }

}
0
0

Fabio. Ill see Your post on upwork, i have worked example for ES 2.4, may be it help You.

    "index": "{{YOUR ELASTIC INDEX}}",
    "type": "{{YOUR ELASTIC TYPE}}",
    "body": {
        "aggs": {
            "trademarks": { // aggs NAME
                "terms": {
                    "field": "id",   // field name in ELASTIC base
                    "size": 100  // count of results YOU need
                }
            },
            "materials": { //another aggs NAME
                "terms": {  
                    "field": "materials.name", // field name in ELASTIC base
                    "size": 100 / count of results YOU need
                }
            },
            "certificate": { 
                "terms": {
                    "field": "certificate_type_id",
                    "size": 100
                }
            },
            "country": {
                "terms": {
                    "field": "country.id",
                    "size": 100
                }
            },
            "price": {
                "stats": {
                    "field": "price"
                }
            }
        },
        "from": 0,  // start from
        "size": 20, // results count
        "query": {
            "constant_score": {
                "filter": {  //apply filter
                    "bool": {
                        "should": [{   // all categories You need to show
                            "term": {
                                "categories": "10142" 
                            }
                        }, {
                            "term": {
                                "categories": "10143"
                            }
                        }, {
                            "term": {
                                "categories": "10144"
                            }
                        }, {
                            "term": {
                                "categories": "10145"
                            }
                        }, {
                            "term": {
                                "categories": "12957"
                            }
                        }, {
                            "term": {
                                "categories": "13968"
                            }
                        }, {
                            "term": {
                                "categories": "14353"
                            }
                        }, {
                            "term": {
                                "categories": "16954"
                            }
                        }, {
                            "term": {
                                "categories": "18243"
                            }
                        }, {
                            "term": {
                                "categories": "10141"
                            }
                        }],
                        "must": [{  // if you want another filed to filter for example filter BY field trademark_id
                            "bool": {
                                "should": [{
                                    "term": {
                                        "trademark_id": "2872"
                                    }
                                }, {
                                    "term": {
                                        "trademark_id": "2879"
                                    }
                                }, {
                                    "term": {
                                        "trademark_id": "2914"
                                    }
                                }]
                            }
                        }, {
                            "bool": {  // filter by PRICE
                                "must": [{
                                    "range": {
                                        "price": {
                                            "from": 5.97,
                                            "to": 15752.69
                                        }
                                    }
                                }]
                            }
                        }]
                    }
                }
            }
        },
        "sort": {  //here SORT BY desc or asc
            "updated_at": "desc" //updated_at -  field from ES base
        }
    }
1
  • i use it for aggregations catalog of goods of my internet shop
    – Valery
    Oct 2, 2016 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.