Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Elasticsearch Query DSL overview and query and the use of filtering context

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Elasticsearch Query DSL overview and query and the use of filtering context". In daily operation, I believe many people have doubts about the use of Elasticsearch Query DSL overview and query and filtering context. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Elasticsearch Query DSL overview and query and the usage of filtering context". Next, please follow the editor to study!

Elasticsearch provides a complete JSON-based query DSL (Domain specific language) to define queries. Think of the query DSL as the AST (abstract syntax tree) of the query, which consists of two types of clauses:

Leaf query clauses (leaf query sentence)

The leaf query clause refers to looking for a specific value in a specific field, such as a match, a range query, or a term (exact match). These queries can be used separately.

Compound query clauses (compound query sentence)

Compound query words wrap other leaves or compound words that are used to logically combine multiple queries (such as bool, dis_max) or change their behavior (such as constant queries).

The behavior of the query clause depends on whether it is used in the query context or in the filtering context:

Query context

The query clause used in the query context answers, "how well does this document match this query clause (query condition)?" In addition to determining whether the document matches, the query clause calculates a score indicating the relative degree to which the document matches other documents. The query context takes effect whenever a query clause is passed to the query parameter (query), such as searching for the query parameter in API.

Filter context

In the context of filtering, the query clause answers, "does this document match this query clause?" The answer is simply "yes" or "no"-no score is calculated. The filtering context is mainly used to filter structured data (equivalent to the filtering conditions of relational databases). For example, will this timestamp be between 2015 and 2016? Is the status of the article "published"? Wait.

Frequently used filters (filter context) are automatically cached by Elasticsearch to improve performance. The filter context takes effect whenever a query clause is passed to the filter parameter (filter), such as the filter or must_not parameter in the bool query, or the constant query (constant_score) or filter query in the filter query.

Examples are as follows:

GET / _ search {"query": {/ / @ 1 "bool": {/ / @ 2 "must": [{"match": {"title": "Search"}} / / @ 3 {"match": {"content": "Elasticsearch"}} / / @ 4], "filter": [/ / @ 5 {"term": {"status": "published"}} / / @ 6 {"range": {"publish_date": {"gte": "2015-01-01"}} / / @ 7]} / / end bool} / / end query}

The code @ 1:query parameter defines the query context, and the query parameter is the query context of elasticsearch.

Code @ 2: bool query expressions using elasticsearch, which will be described in more detail later.

Code @ 3: query the context, using the keyword match, which means that the title field contains a "Search" character that is considered a match. (can be compared to relational database a.title like'% Search%')

Code @ 4: query context, using the keyword match, which means that the content field contains a "Elasticsearch" character that is considered a match.

Code @ 5: defines the filtering context.

Code @ 6: use term (full match), that is, whether the value of the status field is "published". (equivalent to a.status of a relational database = 'published')

Code @ 7: use range to represent a range match, that is, whether the value of the publish_date field is greater than or equal to 2015-01-01. (equivalent to a.publish_date > = 2015-01-01').

This is the end of this article, and the whole series will enter the study of ES DSL query syntax. Next, the ES DSL query syntax is described in detail from the following aspects:

Full-text search (Full text queries)

Entry query (Term level queries)

Compound query

Nested (Association query)

At this point, the study on "Elasticsearch Query DSL overview and query and the use of filtering context" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report