How to filter _search performed with text_expansion

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/*).

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

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
}

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

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.

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