# Exclude Significant Term Aggregation With Different Field

**URL:** <https://discuss.elastic.co/t/exclude-significant-term-aggregation-with-different-field/204759>\
**Category:** Elasticsearch\
**Created:** [October 23, 2019, 4:58am UTC](https://discuss.elastic.co/t/exclude-significant-term-aggregation-with-different-field/204759 "2019-10-23T04:58:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakaziwi](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@rakaziwi](https://discuss.elastic.co/u/rakaziwi)\
**Post date:** [October 23, 2019, 4:58am UTC](https://discuss.elastic.co/t/exclude-significant-term-aggregation-with-different-field/204759/1 "2019-10-23T04:58:11Z")

</div>

Hello,

Is it possible to filter the bucket list result of significant term aggregations using multiple fields to be filtered?  
I am trying to create a recommendation feature using ES based on this article at medium  
[https://towardsdatascience.com/how-to-build-a-recommendation-engine-quick-and-simple-aec8c71a823e](https://towardsdatascience.com/how-to-build-a-recommendation-engine-quick-and-simple-aec8c71a823e).

I store the search data as array of objects instead of array of strings as the given example, because i need other fields to be filtered to get correct bucket list result. Here is the index mapping:

```
{
  "mapping": {
    "properties": {
      "user": {
        "type": "keyword",
        "ignore_above": 256
      },
      "comic_subscribes": {
        "properties": {
          "genres": {
            "type": "keyword",
            "ignore_above": 256
          },
          "id": {
            "type": "keyword",
            "ignore_above": 256
          },
          "type": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

```

I have 2 conditions to be filtered:

1. comic\_subscribes.type must be "serial"
2. comic\_subscribes.genre must not in "hentai" or "echii"

Current aggregation query is like this:

```auto
{
    "size": 0,
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "comic_subscribes.id": "1"
                    }
                }
            ],
            "minimum_should_match": 1,
            "filter": {
                "term": {
                    "comic_subscribes.type": "serial"
                }
            },
            "must_not": [
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "comic_subscribes.genres": "hentai"
                                }
                            },
                            {
                                "term": {
                                    "comic_subscribes.genres": "echii"
                                }
                            }
                        ],
                        "minimum_should_match": 1
                    }
                }
            ]
        }
    },
    "aggs": {
        "recommendations": {
            "significant_terms": {
                "field": "comic_subscribes.id",
                "exclude": ["1"],
                "min_doc_count": 1,
                "size": 10
            }
        }
    }
}

```

I have tried to use filter aggregation but nothing seems to be worked. So is it possible using exclude filtering or is there any other way to achieve my requirements? Thanks

---

<div class="post-metadata">

**Author:** ![rakaziwi](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@rakaziwi](https://discuss.elastic.co/u/rakaziwi)\
**Post date:** [November 1, 2019, 3:58am UTC](https://discuss.elastic.co/t/exclude-significant-term-aggregation-with-different-field/204759/2 "2019-11-01T03:58:41Z")

</div>

Ok, bros. I think there is no option method to filter aggregation significant terms bucket list result using different field.

Based on elasticsearch documentation [Significant Terms Aggregation Parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-significantterms-aggregation.html#_filtering_values_2), which refers to [Terms Aggregation Filtering Value](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_4). There is no other option than **filter using partition expression** and **filter values with exact values** (which i have been using as above, **"exclude"** param).

So i create other way around by getting the comic ids which i want to exclude and store it as excludeComics variable in array. Then use the excludeComics var in **exclude** param. And boom, there you go. Filtered significant terms aggregation bucket list result.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 29, 2019, 3:58am UTC](https://discuss.elastic.co/t/exclude-significant-term-aggregation-with-different-field/204759/3 "2019-11-29T03:58:41Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
