mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-24 23:27:25 -04:00
* [8.x] Mark semantic text as GA in docs (#124670) * Update docs/changelog/125302.yaml * Remove extra changelog
117 lines
2.9 KiB
Text
117 lines
2.9 KiB
Text
[[query-dsl-semantic-query]]
|
|
=== Semantic query
|
|
++++
|
|
<titleabbrev>Semantic</titleabbrev>
|
|
++++
|
|
|
|
The `semantic` query type enables you to perform <<semantic-search,semantic search>> on data stored in a <<semantic-text,`semantic_text`>> field.
|
|
|
|
|
|
[discrete]
|
|
[[semantic-query-example]]
|
|
==== Example request
|
|
|
|
[source,console]
|
|
------------------------------------------------------------
|
|
GET my-index-000001/_search
|
|
{
|
|
"query": {
|
|
"semantic": {
|
|
"field": "inference_field",
|
|
"query": "Best surfing places"
|
|
}
|
|
}
|
|
}
|
|
------------------------------------------------------------
|
|
// TEST[skip: Requires inference endpoints]
|
|
|
|
|
|
[discrete]
|
|
[[semantic-query-params]]
|
|
==== Top-level parameters for `semantic`
|
|
|
|
`field`::
|
|
(Required, string)
|
|
The `semantic_text` field to perform the query on.
|
|
|
|
`query`::
|
|
(Required, string)
|
|
The query text to be searched for on the field.
|
|
|
|
|
|
Refer to <<semantic-search-semantic-text,this tutorial>> to learn more about semantic search using `semantic_text` and `semantic` query.
|
|
|
|
[discrete]
|
|
[[hybrid-search-semantic]]
|
|
==== Hybrid search with the `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.
|
|
|
|
[source,console]
|
|
------------------------------------------------------------
|
|
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
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
------------------------------------------------------------
|
|
// TEST[skip: Requires inference endpoints]
|
|
|
|
You can also use semantic_text as part of <<rrf,Reciprocal Rank Fusion>> to make ranking relevant results easier:
|
|
|
|
[source,console]
|
|
------------------------------------------------------------
|
|
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
|
|
}
|
|
}
|
|
}
|
|
------------------------------------------------------------
|
|
// TEST[skip: Requires inference endpoints]
|