With its distributed architecture, huge volumes of data can be searched and analyzed in almost real-time. It enables you to begin with a single machine and slowly scale to hundreds. Elasticsearch facilitates the execution of a complete search cluster, although its large-scale execution still requires a considerable amount of expertise. In addition to the use cases of full-text search such as document search, product search, email search, etc. Elasticsearch is commonly used to store data that must be diced and sliced, grouped by different dimensions, and so on. Examples of such analytics use cases include Elasticsearch's use for logs, traces, metrics, and other time-series data. In this blog let us learn about Elasticsearch Filtered query.
Elasticsearch sorts search will result with relevance score By default, that measures the extent to which every document corresponds to a query. Relevance Score is the positive floating number which will return in the search APIs metadata field _score. The greater the _score, the greater the relevance of the document. Although every query type may compute relevance scores in various ways, the calculation of scores also depends on where the query clause is executed, whether in a filter or query context.
Become a ElasticSearch Certified professional by learning ElasticSearch Certification Course from hkrtrainings!
Query Context:
In the context of the query, a query clause responds to the question "To what extent does this document comply with this query clause?". In addition to determining whether the document matches or not, the query clause also computes the relevance score in the metadata field _score. The query context comes into effect when the query clause is passed to the query parameter, like the query parameter within the search API.
ElasticSearch Training
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
Filter Context:
In the context of a filter, a request clause responds to the question, "Does this document comply with the query clause?" The response will be a simple Yes or No - there are no scores calculated. The context of the filters is primarily used to filter structured data. Commonly used filters are automatically cached by Elasticsearch in order to accelerate performance.
The filter context applies each time the query clause passes the filter parameter, like the must_not or filter parameters within bool query, and constant_score query, or filter aggregation.
The following are the query clause examples that are used in the filter or query context in the search API. This query will be for documents that meet all of the following requirements:
The title field includes the word 'search'.
The content field includes the word 'elasticsearch.
The status field includes the word 'published'.
The publish_date field includes the dates from Jan 1st 2015.
GET / _search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
- Here The query parameter specifies the query context.
- Bool and the two-match clauses are used within the query context, meaning they are used to evaluate the extent to which the document matches.
- The filter parameter refers to the filter context. Their term and range clauses are used within the filter context. They will screen out documents which will not match; however, they will not have an impact on the score of the documents that are matching.
Subscribe to our YouTube channel to get new updates..!
Queries and filters:
The difference between filters and queries may initially lead to confusion. Particularly when we attempt to link terminology to the query languages to other data sources like SQL. In ElasticSearch, a query is anything that identifies whether the documents meet a given criterion and, to produce a score from 0 to 1 that indicates the extent to which it achieves this. For example, if we ask ElasticSearch to index "The five boxing wizards", it will index it by default like ["the", "five", "boxing", "Wizards"]. If we search with a query which searches for the term "boxing", the indexed string will be matched; however, the score will not be one because a single word matches.
In addition, a filter ignores scoring. It will only identify whether or not a document will match the filter. With the help of filters, we may restrict the search result to documents which meet the given criterion without having to change the score. Moreover, since ElasticSearch does not have to worry about the rating of the filters, they can be cached and are faster. Hence, one general rule is to use filters always unless you need the results to be sorted by relevance based on the query.
To filter without a query:
When a request is not specified, it will default to the match_all query. It indicates that the filtered query may be used to just wrap around a filter so that it may be used anywhere a query is expected. In the previous example, we restrict the results of a query to a filter. However, what if at all we are looking for is a filter? For this kind of instance, we always use the query property within the body of the query, which waits for a query. However, we cannot simply add a filter; we have to wrap it in some kind of query. A solution to this is to change our existing query by substituting the query_string query with a match_all query in a filtered query that is a query which just answers everything.
[Related Article: Elasticsearch Filters]
Conclusion:
In this blog, we have learned about Elasticsearch Filtered Query. We hope you found this information useful. If you are searching for any topic related to Elastic search, do not hesitate to comment below. We would revert with that topic.
About Author
As a senior Technical Content Writer for HKR Trainings, Gayathri has a good comprehension of the present technical innovations, which incorporates perspectives like Business Intelligence and Analytics. She conveys advanced technical ideas precisely and vividly, as conceivable to the target group, guaranteeing that the content is available to clients. She writes qualitative content in the field of Data Warehousing & ETL, Big Data Analytics, and ERP Tools. Connect me on LinkedIn.
Upcoming ElasticSearch Training Online classes
Batch starts on 21st Nov 2024 |
|
||
Batch starts on 25th Nov 2024 |
|
||
Batch starts on 29th Nov 2024 |
|