Skip to content

Commit a0de185

Browse files
committed
fix query add annotation
1 parent b76bb8b commit a0de185

File tree

1 file changed

+12
-91
lines changed

1 file changed

+12
-91
lines changed

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

Lines changed: 12 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -308,105 +308,26 @@ For more complex relevance scoring with combined criteria, you can use the `EVAL
308308
POST /_query?format=txt
309309
{
310310
"query": """
311-
FROM cooking_blog METADATA _score
312-
| WHERE tags.keyword == "vegetarian" AND rating >= 4.5
313-
| EVAL title_score = SCORE(match(title, "curry spicy")) * 2
314-
| EVAL desc_score = SCORE(match(description, "curry spicy"))
315-
| EVAL combined_score = title_score + desc_score
316-
| EVAL category_boost = IF(category.keyword == "Main Course", 1.0, 0.0)
317-
| EVAL date_boost = IF(date >= "now-1M/d", 0.5, 0.0)
318-
| EVAL final_score = combined_score + category_boost + date_boost
319-
| WHERE NOT category.keyword == "Dessert"
320-
| WHERE final_score > 0
321-
| SORT final_score DESC
322-
| LIMIT 1000
323-
"""
324-
}
325-
```
326-
327-
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
320+
| SORT custom_score DESC # Sort by custom relevance score (highest first)
370321
| LIMIT 1000
371-
"""
322+
"""
372323
}
373324
```
374325

375-
Placing `WHERE` before `KEEP` allows ES|QL to optimize field caps, only requesting the fields needed for filtering and display.
376-
377-
### Optimal query order
378-
379-
For best performance, structure your ES|QL queries in this general order:
380-
381-
1. `FROM` to select your index
382-
2. `WHERE` clauses for filtering
383-
3. `KEEP` to select only needed fields
384-
4. Processing operations (`EVAL`, aggregations, etc.)
385-
5. `SORT` to order results
386-
6. `LIMIT` to restrict result count
387-
388-
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-
```
404326

405327
## Learn more
406328

407329
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:
408330

409331
- [Full-text search](full-text.md): Learn about the core components of full-text search in Elasticsearch.
410332
- [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

Comments
 (0)