I have an Elasticsearch index of documents in which there is a field that contains a list of URLs. Aggregating on this field gives me the count of unique URLs, as expected.

```
GET models*/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "links": {
      "terms": {
        "field": "links.keyword",
        "size": 10
      }
    }
  }
}

```

I then want to filter out the buckets whose keys do not contain a certain string. I've tried doing so with the [Bucket Selector Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html).

This attempt:

```
GET models*/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "links": {
      "terms": {
        "field": "links.keyword",
        "size": 10
      }
    },
    "links_key_filter": {
      "bucket_selector": {
        "buckets_path": {
          "key": "links"
        },
        "script": "!key.contains('foo')"
      }
    }
  }
}

```

Fails with:

> 
> Invalid pipeline aggregation named [links\_key\_filter] of type
>  [bucket\_selector]. Only sibling pipeline aggregations are allowed at
>  the top level
> 
> 
> 

Putting the bucket selector inside the links aggregation, like so:

```
GET models*/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "links": {
      "terms": {
        "field": "links.keyword",
        "size": 10
      },
      "bucket_selector": {
        "buckets_path": {
          "key": "links"
        },
        "script": "!key.contains('foo')"
      }
    }
  }
}

```

fails with:

> 
> Found two aggregation type definitions in [links]: [terms] and [bucket\_selector]
> 
> 
> 

I'm going to keep tinkering but am a bit stuck at the moment :(