{"id":25812,"date":"2023-06-12T11:59:24","date_gmt":"2023-06-12T09:59:24","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=25812"},"modified":"2023-06-12T11:59:26","modified_gmt":"2023-06-12T09:59:26","slug":"elasticsearch-queries-domain-specific-language-dsl","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/","title":{"rendered":"Elasticsearch queries: Domain Specific Language (DSL)"},"content":{"rendered":"\n<p>There are several query languages to query Elasticsearch (e.g. KQL, EQL, DSL, aso), in this blog I will speak about Domain Specific Language (DSL) which is the most flexible and gives access to all Elasticsearch options!<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Leaf query clauses<\/strong><br>Leaf query clauses look for a particular value in a particular field, such as the match, term or range queries. These queries can be used by themselves.<\/li>\n\n\n\n<li><strong>Compound query clauses<\/strong><br>Compound query clauses wrap other leaf or compound queries and are used to combine multiple queries in a logical fashion (such as the bool or dis_max query), or to alter their behaviour (such as the constant_score query).<\/li>\n<\/ul>\n\n\n\n<p>Query clauses behave differently depending on whether they are used in query context or filter context.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Query and filter context<\/h2>\n\n\n\n<p>Lets understand what are both contexts, but first we need to understand how Elasticsearch sorts matching results.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Relevance scores<\/span><\/strong><br>By default, Elasticsearch sorts matching search results by relevance score, which measures how well each document matches a query.<br>The relevance score is a positive floating point number, returned in the _score metadata field of the search API. The higher the _score, the more relevant the document. While each query type can calculate relevance scores differently, score calculation also depends on whether the query clause is run in a query or filter context.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Query context<\/span><\/strong><br>In the query context, a query clause answers the question \u201cHow well does this document match this query clause?\u201d Besides deciding whether or not the document matches, the query clause also calculates a relevance score in the _score metadata field.<br>Query context is in effect whenever a query clause is passed to a query parameter, such as the query parameter in the search API.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Filter context<\/span><\/strong><br>In a filter context, a query clause answers the question \u201cDoes this document match this query clause?\u201d The answer is a simple Yes or No\u2009-\u2009no scores are calculated. Filter context is mostly used for filtering structured data, e.g. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Does this timestamp fall into the range 2022 to 2023?<\/li>\n\n\n\n<li>Is the status field of a blog set to &#8220;published&#8221;?<\/li>\n<\/ul>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Example of query and filter contexts<\/span><\/strong><br>Below is an example of query clauses being used in query and filter context in the search API. This query will match documents where all of the following conditions are met:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <em>title <\/em>field contains the word blog.<\/li>\n\n\n\n<li>The <em>content <\/em>field contains the word elasticsearch.<\/li>\n\n\n\n<li>The <em>status <\/em>field contains the exact word published.<\/li>\n\n\n\n<li>The <em>publish_date <\/em>field contains a date from 1 June 2023 onwards.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nGET \/_search\n{\n  &quot;query&quot;: { \n    &quot;bool&quot;: { \n      &quot;must&quot;: &#x5B;\n        { &quot;match&quot;: { &quot;title&quot;:   &quot;Blog&quot;        }},\n        { &quot;match&quot;: { &quot;content&quot;: &quot;Elasticsearch&quot; }}\n      ],\n      &quot;filter&quot;: &#x5B; \n        { &quot;term&quot;:  { &quot;status&quot;: &quot;published&quot; }},\n        { &quot;range&quot;: { &quot;publish_date&quot;: { &quot;gte&quot;: &quot;2013-06-01&quot; }}}\n      ]\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>The <em>query <\/em>parameter indicates query context.<br>The <em>bool <\/em>and two <em>match <\/em>clauses are used in query context, which means that they are used to score how well each document matches.<br>The <em>filter <\/em>parameter indicates filter context. Its <em>term <\/em>and <em>range <\/em>clauses are used in filter context. They will filter out documents which do not match, but they will not affect the score for matching documents.<\/p>\n\n\n\n<p>You got it, use query clauses in query context for conditions which should affect the score of matching documents, and use all other query clauses in filter context \ud83d\ude09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Queries groups<\/h2>\n\n\n\n<p>As you can imagine there are a lot of queries, that is why they are grouped by &#8220;type&#8221;, one blog is really not enough to go throw all groups queries, bellow is the list of important groups that will be detailed in next blogs:<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Full Text queries<\/span><\/strong><\/p>\n\n\n\n<p>The full text queries enable you to search analyzed text fields such as the body of an email. The query string is processed using the same analyzer that was applied to the field during indexing.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Compound queries<\/span><\/strong><\/p>\n\n\n\n<p>Compound queries wrap other compound or leaf queries, either to combine their results and scores, to change their behaviour, or to switch from query to filter context.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline\">Geo queries<\/span><\/strong><\/p>\n\n\n\n<p>Elasticsearch supports two types of geo data: geo_point fields which support lat\/lon pairs, and geo_shape fields, which support points, lines, circles, polygons, multi-polygons, etc&#8230;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In next blogs, I will go throw all these groups showing you some examples of each one \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several query languages to query Elasticsearch (e.g. KQL, EQL, DSL, aso), in this blog I will speak about Domain Specific Language (DSL) which is the most flexible and gives access to all Elasticsearch options!<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197,1320],"tags":[86,87,234],"type_dbi":[],"class_list":["post-25812","post","type-post","status-publish","format-standard","hentry","category-application-integration-middleware","category-devops","tag-elasticsearch","tag-elk","tag-queries"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Elasticsearch queries: Domain Specific Language (DSL) - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Elasticsearch queries: Domain Specific Language (DSL)\" \/>\n<meta property=\"og:description\" content=\"There are several query languages to query Elasticsearch (e.g. KQL, EQL, DSL, aso), in this blog I will speak about Domain Specific Language (DSL) which is the most flexible and gives access to all Elasticsearch options!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-12T09:59:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-12T09:59:26+00:00\" \/>\n<meta name=\"author\" content=\"David Diab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Diab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\"},\"author\":{\"name\":\"David Diab\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/deb907c3360cacdc6c7df54b4bac3c86\"},\"headline\":\"Elasticsearch queries: Domain Specific Language (DSL)\",\"datePublished\":\"2023-06-12T09:59:24+00:00\",\"dateModified\":\"2023-06-12T09:59:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\"},\"wordCount\":709,\"commentCount\":0,\"keywords\":[\"Elasticsearch\",\"ELK\",\"queries\"],\"articleSection\":[\"Application integration &amp; Middleware\",\"DevOps\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\",\"name\":\"Elasticsearch queries: Domain Specific Language (DSL) - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2023-06-12T09:59:24+00:00\",\"dateModified\":\"2023-06-12T09:59:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/deb907c3360cacdc6c7df54b4bac3c86\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Elasticsearch queries: Domain Specific Language (DSL)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/deb907c3360cacdc6c7df54b4bac3c86\",\"name\":\"David Diab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/212b1b2e4650bad3116f644ab4fb4663786d94195d7685d0704c8426da088e60?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/212b1b2e4650bad3116f644ab4fb4663786d94195d7685d0704c8426da088e60?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/212b1b2e4650bad3116f644ab4fb4663786d94195d7685d0704c8426da088e60?s=96&d=mm&r=g\",\"caption\":\"David Diab\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/david-diab\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Elasticsearch queries: Domain Specific Language (DSL) - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/","og_locale":"en_US","og_type":"article","og_title":"Elasticsearch queries: Domain Specific Language (DSL)","og_description":"There are several query languages to query Elasticsearch (e.g. KQL, EQL, DSL, aso), in this blog I will speak about Domain Specific Language (DSL) which is the most flexible and gives access to all Elasticsearch options!","og_url":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/","og_site_name":"dbi Blog","article_published_time":"2023-06-12T09:59:24+00:00","article_modified_time":"2023-06-12T09:59:26+00:00","author":"David Diab","twitter_card":"summary_large_image","twitter_misc":{"Written by":"David Diab","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/"},"author":{"name":"David Diab","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/deb907c3360cacdc6c7df54b4bac3c86"},"headline":"Elasticsearch queries: Domain Specific Language (DSL)","datePublished":"2023-06-12T09:59:24+00:00","dateModified":"2023-06-12T09:59:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/"},"wordCount":709,"commentCount":0,"keywords":["Elasticsearch","ELK","queries"],"articleSection":["Application integration &amp; Middleware","DevOps"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/","url":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/","name":"Elasticsearch queries: Domain Specific Language (DSL) - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2023-06-12T09:59:24+00:00","dateModified":"2023-06-12T09:59:26+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/deb907c3360cacdc6c7df54b4bac3c86"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/elasticsearch-queries-domain-specific-language-dsl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Elasticsearch queries: Domain Specific Language (DSL)"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/deb907c3360cacdc6c7df54b4bac3c86","name":"David Diab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/212b1b2e4650bad3116f644ab4fb4663786d94195d7685d0704c8426da088e60?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/212b1b2e4650bad3116f644ab4fb4663786d94195d7685d0704c8426da088e60?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/212b1b2e4650bad3116f644ab4fb4663786d94195d7685d0704c8426da088e60?s=96&d=mm&r=g","caption":"David Diab"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/david-diab\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/25812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=25812"}],"version-history":[{"count":4,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/25812\/revisions"}],"predecessor-version":[{"id":25816,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/25812\/revisions\/25816"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=25812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=25812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=25812"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=25812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}