# How to filter \_search performed with text\_expansion

**URL:** <https://discuss.elastic.co/t/how-to-filter-search-performed-with-text-expansion/344646>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning\
**Created:** [October 9, 2023, 11:20am UTC](https://discuss.elastic.co/t/how-to-filter-search-performed-with-text-expansion/344646 "2023-10-09T11:20:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sivagurlinka](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sivagurlinka](https://discuss.elastic.co/u/sivagurlinka)\
**Post date:** [October 9, 2023, 11:20am UTC](https://discuss.elastic.co/t/how-to-filter-search-performed-with-text-expansion/344646/1 "2023-10-09T11:20:31Z")

</div>

I have an Elasticsearch index ingested with inference pipeline using ELSER. While performing the search I would like to filter and show all the URL's containing "/cn/" in it. ([http://www.elastic.co/giude/cn/](http://www.elastic.co/giude/cn/)\*).

How to add filter section in the below search query to filter out all the URLs containing "/cn/"?

```auto
GET search-index/_search
{
  "size": 5, 
   "query":{
      "text_expansion":{
         "ml.inference.body_content_expanded.predicted_value":{
            "model_id":".elser_model_1",
            "model_text":"Search_query"
         }
      }
   },
   "fields": ["title","url"],
  "_source": false
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 9, 2023, 2:10pm UTC](https://discuss.elastic.co/t/how-to-filter-search-performed-with-text-expansion/344646/2 "2023-10-09T14:10:49Z")

</div>

I guess you could do that using a `bool` with a `filter` clause?

```auto
GET search-index/_search
{
  "size": 5, 
   "query":{
      "bool": {
         must: [{
          "text_expansion":{
             "ml.inference.body_content_expanded.predicted_value":{
                "model_id":".elser_model_1",
                "model_text":"Search_query"
             }
          }
         }],
         filter: [{
           "term": {
               "url.path": "http://www.elastic.co/giude/cn" 
           }
         }]
      }
   },
   "fields": ["title","url"],
  "_source": false
}

```

Of course, it depends on the analyzer you have for the `url` field (and subfields). Here I presumed that you defined a `path` subfield which is using a [path-hierarchy tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html).

---

<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 6, 2023, 2:10pm UTC](https://discuss.elastic.co/t/how-to-filter-search-performed-with-text-expansion/344646/3 "2023-11-06T14:10:57Z")

</div>

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