Filtering on facet selections

I am new in ElasticSearch. So far I could implement the search successfully and also aggregations to build my filter as shown below

{
"from": 0,
"size": 5,
"explain": false,
"aggs": {
"Condition_aggs": {
"terms": {
"field": "Condition"
}
},
"Color_aggs": {
"terms": {
"field": "color"
}
}
},
"query": {
"bool": {
"should": [
{
"multi_match": {
"type": "best_fields",
"query": "tn2000",
"fields": [
"Number^8",
"Name.raw^7.5",
"Name^7"
]
}
}
]
}
}
}

I am getting aggregations for filter but I dont know how to do next step when filter is selected on the page. for example If red color is selected. should I use a filter term query instead of multi-match query or should I extend above multi-match query with filters? Not sure how to achieve this. I appreciate if somebody can shed me some lights ? and short example will be lovely.

thanks,

Emil

Add the "filter" clause to your bool query [1], put the term filter there.

"term": {"color": "red"}

[1] https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query

what I am confused about is aggregation filters? are those filters just narrowing down returned aggregations? what is the purpose of those filters? if you can give me a simple example, I appreciate.

Exactly. Aggregation filters should be used when a filter should ONLY apply to aggregations. If a filter should apply to both aggregations and the top hits (or if you do not care about the top hits), then it should be put in the query element. Given what you are trying to do, you should probably be doing something like this:

GET t/_search
{
  "from": 0,
  "size": 5,
  "explain": false,
  "aggs": {
    "Condition_aggs": {
      "terms": {
        "field": "Condition"
      }
    },
    "Color_aggs": {
      "terms": {
        "field": "color"
      }
    }
  },
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "type": "best_fields",
            "query": "tn2000",
            "fields": [
              "Number^8",
              "Name.raw^7.5",
              "Name^7"
            ]
          }
        }
      ]
    },
    "filter": {
      "term": {
        "color": "red"
      }
    }
  }
}

There are not many use-cases for aggregation filters. One example would be that your users are looking for x OR y OR z and you want to tell them how many documents have 2 out of 3 terms. You would put a regular bool query in the query part that will match all documents that contain either term, and then you would have an aggregation filter with a bool query with those 3 terms and a min_should_match of 2 so that elasticsearch will compute how many docs contain 2 of the queried terms or more.