Text expansion query

edit

The text expansion query uses a natural language processing model to convert the query text into a list of token-weight pairs which are then used in a query against a sparse vector or rank features field.

Example request

edit
response = client.search(
  body: {
    query: {
      text_expansion: {
        "<sparse_vector_field>": {
          model_id: 'the model to produce the token weights',
          model_text: 'the query string'
        }
      }
    }
  }
)
puts response
GET _search
{
   "query":{
      "text_expansion":{
         "<sparse_vector_field>":{
            "model_id":"the model to produce the token weights",
            "model_text":"the query string"
         }
      }
   }
}

Top level parameters for text_expansion

edit
<sparse_vector_field>
(Required, object) The name of the field that contains the token-weight pairs the NLP model created based on the input text.

Top level parameters for <sparse_vector_field>

edit
model_id
(Required, string) The ID of the model to use to convert the query text into token-weight pairs. It must be the same model ID that was used to create the tokens from the input text.
model_text
(Required, string) The query text you want to use for search.

Example

edit

The following is an example of the text_expansion query that references the ELSER model to perform semantic search. For a more detailed description of how to perform semantic search by using ELSER and the text_expansion query, refer to this tutorial.

GET my-index/_search
{
   "query":{
      "text_expansion":{
         "ml.tokens":{
            "model_id":".elser_model_1",
            "model_text":"How is the weather in Jamaica?"
         }
      }
   }
}

Depending on your data, the text expansion query may be faster with track_total_hits: false.