Semantic query
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
9.0+
Generally available since 9.0
The semantic query type enables you to perform semantic search on data stored in a semantic_text field. This query accepts natural-language text and uses the field’s configured inference endpoint to generate a query embedding and match documents.
For an overview of all query options available for semantic_text fields, see Querying semantic_text fields.
The target field of a semantic query must be mapped as semantic or semantic_text and associated with an inference endpoint. At query time, the inference endpoint is chosen as follows:
- If
search_inference_idis defined, the semantic query uses that endpoint to embed the query. - If no
search_inference_idis defined,inference_idis used for both indexing and search. - For
semantic_textfields, if no endpoint is specified in the mapping, the field uses its default endpoint. Thesemanticfield has no default endpoint and requires an explicitinference_id.
The underlying vector type (dense or sparse) is determined by the endpoint automatically. No extra query parameters are required.
GET my-index-000001/_search{ "query": { "semantic": { "field": "inference_field", "query": "Best surfing places" } } }
field- (Required, string) The
semanticorsemantic_textfield to perform the query on. query- (Required, string) The query text to be searched for on the field.
Refer to this tutorial to learn more about semantic search using semantic_text and semantic query.
The semantic query can be used as a part of a hybrid search where the semantic query is combined with lexical queries. For example, the query below finds documents with the title field matching "mountain lake", and combines them with results from a semantic search on the field title_semantic, that is a semantic_text field. The combined documents are then scored, and the top 3 top scored documents are returned.
POST my-index/_search{ "size" : 3, "query": { "bool": { "should": [ { "match": { "title": { "query": "mountain lake", "boost": 1 } } }, { "semantic": { "field": "title_semantic", "query": "mountain lake", "boost": 2 } } ] } } }
You can also use semantic_text as part of Reciprocal Rank Fusion to make ranking relevant results easier:
GET my-index/_search{ "retriever": { "rrf": { "retrievers": [ { "standard": { "query": { "term": { "text": "shoes" } } } }, { "standard": { "query": { "semantic": { "field": "semantic_field", "query": "shoes" } } } } ], "rank_window_size": 50, "rank_constant": 20 } } }