Skip to content

Commit 6249e4a

Browse files
committed
Add semantic search stuff
1 parent 51df3a5 commit 6249e4a

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

solutions/search/esql-full-text-filter-tutorial.md

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This tutorial presents examples in {{esql}} syntax. Refer to [the Query DSL vers
1010

1111
This is a hands-on introduction to the basics of [full-text search](full-text.md) with Elasticsearch, also known as *lexical search*, and how to filter search results based on exact criteria. In this scenario, we're implementing a search function for a cooking blog. The blog contains recipes with various attributes including textual content, categorical data, and numerical ratings.
1212

13+
This tutorial covers lexical search, with a brief [introductory section on semantic search](#bonus-semantic-search-with-esql) at the end.
14+
1315
## Requirements
1416

1517
You'll need a running {{es}} cluster, together with {{kib}} to use the Dev Tools API Console. Refer to [choose your deployment type](/deploy-manage/deploy.md#choosing-your-deployment-type) for deployment options.
@@ -118,7 +120,7 @@ POST /cooking_blog/_bulk?refresh=wait_for
118120
Full-text search involves executing text-based queries across one or more document fields. These queries calculate a relevance score for each matching document, based on how closely the document's content aligns with the search terms. Elasticsearch offers various query types, each with its own method for matching text and relevance scoring.
119121

120122
:::{tip}
121-
ES|QL provides two ways to perform full-text searches:
123+
{{esql}} provides two ways to perform full-text searches:
122124

123125
1. Full match function syntax: `match(field, "search terms")`
124126
1. Compact syntax using the match operator "`:`": `field:"search terms"`
@@ -141,7 +143,7 @@ POST /_query?format=txt
141143
}
142144
```
143145

144-
By default, like the Query DSL `match` query, ES|QL uses `OR` logic between terms. This means it will match documents that contain either "fluffy" or "pancakes", or both, in the description field.
146+
By default, like the Query DSL `match` query, {{esql}} uses `OR` logic between terms. This means it will match documents that contain either "fluffy" or "pancakes", or both, in the description field.
145147

146148
:::{tip}
147149
You can control which fields to include in the response using the `KEEP` command:
@@ -195,7 +197,7 @@ This query searches the title field to match at least 2 of the 3 terms: "fluffy"
195197

196198
## Step 4: Search across multiple fields at once
197199

198-
When users enter a search query, they often don't know (or care) whether their search terms appear in a specific field. ES|QL provides ways to search across multiple fields simultaneously:
200+
When users enter a search query, they often don't know (or care) whether their search terms appear in a specific field. {{esql}} provides ways to search across multiple fields simultaneously:
199201

200202
```esql
201203
POST /_query?format=txt
@@ -216,19 +218,25 @@ However, in many cases, matches in certain fields (like the title) might be more
216218
POST /_query?format=txt
217219
{
218220
"query": """
219-
FROM cooking_blog METADATA _score # Request score metadata for relevance ranking
221+
FROM cooking_blog METADATA _score # Request _score metadata for relevance-based results
220222
| WHERE match(title, "vegetarian curry", {"boost": 2.0}) # Title matches are twice as important
221223
OR match(description, "vegetarian curry")
222224
OR match(tags, "vegetarian curry")
223-
| KEEP title, description, tags, _score
224-
| SORT _score DESC # Order by relevance score (highest first)
225+
| KEEP title, description, tags, _score # Include relevance score in results
226+
| SORT _score DESC # Including `METADATA _score` doesn't automatically sort your results by relevance, you must explicitly sort when the order of results matters
225227
| LIMIT 1000
226228
"""
227229
}
228230
```
229231

230232
In this example, we're using the `boost` parameter to make matches in the title field twice as important as matches in other fields. We also request the `_score` metadata field to sort results by relevance.
231233

234+
:::{tip}
235+
When working with relevance scoring in {{esql}}, it's important to understand how `_score` works. If you don't include `METADATA _score` in your query, you're only performing filtering operations with no relevance calculation. When you do include it, only full-text search functions like `match()` or the `:` operator will contribute to the relevance score.
236+
237+
Currently, filtering operations like range conditions and exact matches still contribute a constant score, which can affect results. This behavior will change in future versions, where these operations won't affect scoring at all.
238+
:::
239+
232240
## Step 5: Filter and find exact matches
233241

234242
Filtering allows you to narrow down your search results based on exact criteria. Unlike full-text searches, filters are binary (yes/no) and do not affect the relevance score. Filters execute faster than queries because excluded results don't need to be scored.
@@ -325,11 +333,61 @@ POST /_query?format=txt
325333
}
326334
```
327335

336+
## Bonus: Semantic search with ES|QL
337+
338+
ES|QL also supports semantic search when your mappings include fields of the [`semantic_text`](elasticsearch://reference/elasticsearch/mapping-reference/semantic-text.md) type:
339+
340+
```console
341+
PUT /cooking_blog/_mapping
342+
{
343+
"properties": {
344+
"semantic_description": {
345+
"type": "semantic_text"
346+
}
347+
}
348+
}
349+
```
350+
351+
This mapping update adds a new field called `semantic_description` with the type `semantic_text`, which enables vector-based semantic search using the specified embedding model.
352+
353+
Next, index a document with content in the semantic field:
354+
355+
```console
356+
POST /cooking_blog/_doc
357+
{
358+
"title": "Mediterranean Quinoa Bowl",
359+
"semantic_description": "A protein-rich bowl with quinoa, chickpeas, fresh vegetables, and herbs. This nutritious Mediterranean-inspired dish is easy to prepare and perfect for a quick, healthy dinner.",
360+
"author": "Jamie Oliver",
361+
"date": "2023-06-01",
362+
"category": "Main Course",
363+
"tags": ["vegetarian", "healthy", "mediterranean", "quinoa"],
364+
"rating": 4.7
365+
}
366+
```
367+
368+
Then you can perform semantic searches that find results based on meaning, not just keywords:
369+
370+
```console
371+
POST /_query?format=txt
372+
{
373+
"query": """
374+
FROM cooking_blog METADATA _score
375+
| WHERE semantic_description:"What are some easy to prepare but nutritious plant-based meals?"
376+
| SORT _score DESC
377+
| LIMIT 5
378+
"""
379+
}
380+
```
381+
382+
Follow this [tutorial](/solutions/search/semantic-search/semantic-search-semantic-text#semantic-text-semantic-search) to learn how to use semantic search in Elasticsearch, using Query DSL or ES|QL.
328383

329384
## Learn more
330385

331-
This tutorial introduced the basics of full-text search and filtering in ES|QL. Building a real-world search experience requires understanding many more advanced concepts and techniques. Here are some resources once you're ready to dive deeper:
386+
This tutorial introduced the basics of full-text search and filtering in {{esql}}. Building a real-world search experience requires understanding many more advanced concepts and techniques. Here are some resources once you're ready to dive deeper:
332387

333388
- [Full-text search](full-text.md): Learn about the core components of full-text search in Elasticsearch.
334389
- [Text analysis](full-text/text-analysis-during-search.md): Understand how text is processed for full-text search.
335-
- [{{esql}} search functions](elasticsearch://reference/query-languages/esql/esql-functions-operators.md#esql-search-functions): Explore the full list of search functions available in ES|QL.
390+
- [{{esql}} search functions](elasticsearch://reference/query-languages/esql/esql-functions-operators.md#esql-search-functions): Explore the full list of search functions available in {{esql}}.
391+
% semantic search stuff
392+
- [Semantic search](/solutions/search/semantic-search.md): Understand your various options for semantic search in Elasticsearch.
393+
- [The `semantic_text` workflow](/solutions/search/semantic-search#_semantic_text_workflow): Learn how to use the `semantic_text` field type for semantic search. This is the recommended approach for must users looking to perform semantic search in {{es}}, because it abstracts away the complexity of setting up inference endpoints and models.

0 commit comments

Comments
 (0)