You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solutions/search/esql-full-text-filter-tutorial.md
+66-8Lines changed: 66 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ This tutorial presents examples in {{esql}} syntax. Refer to [the Query DSL vers
10
10
11
11
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.
12
12
13
+
This tutorial covers lexical search, with a brief [introductory section on semantic search](#bonus-semantic-search-with-esql) at the end.
14
+
13
15
## Requirements
14
16
15
17
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
118
120
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.
119
121
120
122
:::{tip}
121
-
ES|QL provides two ways to perform full-text searches:
123
+
{{esql}} provides two ways to perform full-text searches:
122
124
123
125
1. Full match function syntax: `match(field, "search terms")`
124
126
1. Compact syntax using the match operator "`:`": `field:"search terms"`
@@ -141,7 +143,7 @@ POST /_query?format=txt
141
143
}
142
144
```
143
145
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.
145
147
146
148
:::{tip}
147
149
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"
195
197
196
198
## Step 4: Search across multiple fields at once
197
199
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:
199
201
200
202
```esql
201
203
POST /_query?format=txt
@@ -216,19 +218,25 @@ However, in many cases, matches in certain fields (like the title) might be more
216
218
POST /_query?format=txt
217
219
{
218
220
"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
220
222
| WHERE match(title, "vegetarian curry", {"boost": 2.0}) # Title matches are twice as important
221
223
OR match(description, "vegetarian curry")
222
224
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
225
227
| LIMIT 1000
226
228
"""
227
229
}
228
230
```
229
231
230
232
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.
231
233
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
+
232
240
## Step 5: Filter and find exact matches
233
241
234
242
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
325
333
}
326
334
```
327
335
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.",
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.
328
383
329
384
## Learn more
330
385
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:
332
387
333
388
-[Full-text search](full-text.md): Learn about the core components of full-text search in Elasticsearch.
334
389
-[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