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
This ES|QL query uses an explicit scoring mechanism:
328
-
1. Requires "vegetarian" tag and rating >= 4.5
329
-
2. Computes separate scores for `title` and `description` matches
330
-
3. Adds boosts for Main Course category and recent dates
331
-
4. Excludes Desserts
332
-
5. Sorts by the final combined score
333
-
334
-
335
-
:::{warning}
336
-
TODO
337
-
338
-
This section shouldn't live in a tutorial, leaving it here for comments/suggestions if it might be useful
339
-
:::
340
-
341
-
## Optimizing your ES|QL queries
342
-
343
-
ES|QL queries can be optimized for better performance and more relevant results. Here are some key optimization strategies:
344
-
345
-
### Field filtering with KEEP
346
-
347
-
Using `KEEP` early in your query pipeline can significantly improve performance by reducing the fields that need to be fetched:
348
-
349
-
```esql
350
-
POST /_query?format=txt
351
-
{
352
-
"query": """
353
-
FROM cooking_blog
354
-
| KEEP title, description, rating
355
-
| WHERE title:"curry"
356
-
| LIMIT 1000
357
-
"""
358
-
}
359
-
```
360
-
361
-
However, there's an important caveat: if you need to filter on fields not included in `KEEP`, you should place your `WHERE` clauses before `KEEP`:
362
-
363
-
```esql
364
-
POST /_query?format=txt
365
-
{
366
-
"query": """
367
-
FROM cooking_blog
368
-
| WHERE category.keyword == "Main Course" AND rating >= 4.0
369
-
| KEEP title, description, rating
311
+
FROM cooking_blog METADATA _score # Request _score metadata for relevance scoring
312
+
| EVAL tags_concat = MV_CONCAT(tags.keyword, ",") # Convert multi-valued tags field to a single string with comma delimiter
313
+
| WHERE tags_concat LIKE "*vegetarian*" AND rating >= 4.5 # Filter for vegetarian recipes with high ratings, using wildcards to find "vegetarian" within the concatenated tags
314
+
| WHERE match(title, "curry spicy", {"boost": 2.0}) OR match(description, "curry spicy") # Full-text search with boosted title relevance (2x importance)
315
+
| EVAL category_boost = CASE(category.keyword == "Main Course", 1.0, 0.0) # Add boost for Main Course category
316
+
| EVAL date_boost = CASE(DATE_DIFF("month", date, NOW()) <= 1, 0.5, 0.0) # Add boost for recipes published in the last month
317
+
| EVAL custom_score = _score + category_boost + date_boost # Combine all scores into final relevance score
318
+
| WHERE NOT category.keyword == "Dessert" # Exclude desserts
319
+
| WHERE custom_score > 0 # Only include results with positive relevance
This order allows Elasticsearch to apply filters early, reducing the dataset before performing more expensive operations.
389
-
390
-
### Use keyword fields for exact matching
391
-
392
-
Always use the `.keyword` suffix for exact matching on text fields. This improves performance and ensures case-sensitive, exact matches:
393
-
394
-
```esql
395
-
POST /_query?format=txt
396
-
{
397
-
"query": """
398
-
FROM cooking_blog
399
-
| WHERE tags.keyword == "vegetarian"
400
-
| LIMIT 1000
401
-
"""
402
-
}
403
-
```
404
326
405
327
## Learn more
406
328
407
329
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:
408
330
409
331
-[Full-text search](full-text.md): Learn about the core components of full-text search in Elasticsearch.
410
332
-[Text analysis](full-text/text-analysis-during-search.md): Understand how text is processed for full-text search.
411
-
-[Query and filter data](/explore-analyze/query-filter.md): Understand all your options for searching and analyzing data in {{es}} in the Explore & Analyze section.
412
-
-[Search your data](../search.md): Learn about more advanced search techniques including semantic search.
333
+
-[{{esql}} search functions](elasticsearch://reference/query-languages/esql/esql-functions-operators.md/esql-functions-operators.html#esql-search-functions): Explore the full list of search functions available in ES|QL.
0 commit comments