<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eda Kaçmaz on Medium]]></title>
        <description><![CDATA[Stories by Eda Kaçmaz on Medium]]></description>
        <link>https://medium.com/@eda.kacmaz?source=rss-a3ed4f7db82f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mv4hpTWfKZZQcYB5</url>
            <title>Stories by Eda Kaçmaz on Medium</title>
            <link>https://medium.com/@eda.kacmaz?source=rss-a3ed4f7db82f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:30:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eda.kacmaz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A cheat sheet for practical ElasticSearch queries]]></title>
            <link>https://medium.com/@eda.kacmaz/a-cheat-sheet-for-practical-elasticsearch-queries-5e180449e20a?source=rss-a3ed4f7db82f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e180449e20a</guid>
            <category><![CDATA[elasticsearch-queries]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[cheatsheet]]></category>
            <dc:creator><![CDATA[Eda Kaçmaz]]></dc:creator>
            <pubDate>Tue, 21 Nov 2023 11:16:05 GMT</pubDate>
            <atom:updated>2023-11-21T11:16:05.229Z</atom:updated>
            <content:encoded><![CDATA[<p>Elasticsearch provides a full <a href="https://logz.io/blog/elasticsearch-queries/">Query DSL (Domain Specific Language)</a> [2] based on JSON to define <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html">queries</a>. In this article, we will have a look at the queries that are commonly in use and can be useful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v1oKkvXCIWkSw1msfBWJig.png" /><figcaption>The similarities between basic terms of relational databases and Elastic Search can be associated with the picture above.</figcaption></figure><p>Assuming that we have a mapping such as can be seen below, we will write a variety of queries to retrieve data from indexes:</p><pre>{<br>  &quot;mappings&quot;: {<br>    &quot;productData&quot;: {<br>      &quot;properties&quot;: {<br>        &quot;brand&quot;: {<br>          &quot;properties&quot;: {<br>            &quot;id&quot;: {<br>              &quot;type&quot;: &quot;integer&quot;<br>            },<br>            &quot;name&quot;: {<br>              &quot;type&quot;: &quot;keyword&quot;<br>            }<br>          }<br>        },<br>        &quot;category&quot;: {<br>          &quot;properties&quot;: {<br>            &quot;id&quot;: {<br>              &quot;type&quot;: &quot;integer&quot;<br>            },<br>            &quot;name&quot;: {<br>              &quot;type&quot;: &quot;keyword&quot;<br>            }<br>          }<br>        },<br>        &quot;documentCreationDate&quot;: {<br>          &quot;type&quot;: &quot;date&quot;<br>        },<br>        &quot;campaigns&quot;: {<br>          &quot;properties&quot;: {<br>            &quot;campaignId&quot;: {<br>              &quot;type&quot;: &quot;long&quot;<br>            },<br>            &quot;campaignEndDate&quot;: {<br>              &quot;type&quot;: &quot;date&quot;<br>            },<br>            &quot;isHolidayMode&quot;: {<br>              &quot;type&quot;: &quot;boolean&quot;<br>            },<br>            &quot;isSellable&quot;: {<br>              &quot;type&quot;: &quot;boolean&quot;<br>            },<br>            &quot;price&quot;: {<br>              &quot;type&quot;: &quot;double&quot;<br>            },<br>            &quot;campaignStartDate&quot;: {<br>              &quot;type&quot;: &quot;keyword&quot;<br>            }<br>          }<br>        }<br>      }<br>    }<br>  }<br>}</pre><p>Table of Contents:</p><ul><li><a href="#def6">Match all</a></li><li><a href="#9458">Match</a></li><li><a href="#6491">Match Phrase Prefix</a></li><li><a href="#05e0">Term</a></li><li><a href="#86e3">Terms</a></li><li><a href="#b423">Must</a></li><li><a href="#01ba">Must Not</a></li><li><a href="#f7e4">Range</a></li><li><a href="#b4e6">Sort</a></li><li><a href="#3688">Size</a></li><li><a href="#56c9">Source</a></li><li><a href="#b60f">Date Math</a></li><li><a href="#4165">Pagination</a></li><li><a href="#b552">Aggregations</a></li></ul><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html">Match all:</a> The most simple query, which matches all documents, used for getting all documents under index.</blockquote><pre><br>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;match_all&quot;: {}<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html">Match:</a> Used for full-text search. Returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;brand.name&quot;: &quot;Koton&quot;<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html">Match phrase prefix:</a> Returns documents that contain the words of a provided text, in the <strong>same order</strong> as provided. The last term of the provided text is treated as a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html">prefix</a>, matching any words that begin with that term. We use this when we want to filter phrases with a prefix. For example, on the below, we want to filter brands that starts with “Kot”.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;match_phrase_prefix&quot;: {<br>      &quot;brand.name&quot;: &quot;Kot&quot;<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html">Term:</a> We use this when filtering fields, similar to “match”. The difference is <strong>term</strong> is a perfect match, that is, an exact query. The <strong>match query</strong> will first classify the search words. After the word segmentation (the process of determining the word boundaries in a sentence or a document by computer algorithms), the word segmentation results will be matched one by one. For example, we want to filter product documents that brand name is “Koton”. *(<a href="https://hashnode.com/post/match-query-vs-term-query-ckptjx9870i28cls147vg7ryr">Term query vs Match query</a>)</blockquote><pre><br>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;term&quot;: {<br>      &quot;brand.name&quot;: {<br>        &quot;value&quot;: &quot;Koton&quot;<br>      }<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html">Terms:</a> We use <strong>terms </strong>when we want to filter several options for one field. For example, we want to gather the results for the brands Koton and İpekyol. This query will return results with product documents that brand name is Koton or İpekyol. It will retrieve both brands.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;terms&quot;: {<br>      &quot;brand.name&quot;: [<br>        &quot;Koton&quot;,<br>        &quot;İpekyol&quot;<br>      ]<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html">must:</a><strong> </strong>— <em>Must </em>is similar to the “and” operator. Using <em>must </em>tells Elasticsearch that document matches need to include all of the queries that fall under the <em>must </em>clause. If you have more than one query, then all of those queries need to match. We are filtering sweaters that belongs to Koton brand below.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;bool&quot;: {<br>      &quot;must&quot;: [<br>        {<br>          &quot;term&quot;: {<br>            &quot;brand.name&quot;: {<br>              &quot;value&quot;: &quot;Koton&quot;<br>            }<br>          }<br>        },<br>        {<br>          &quot;term&quot;: {<br>            &quot;category.name&quot;: {<br>              &quot;value&quot;: &quot;Sweater&quot;<br>            }<br>          }<br>        }<br>      ]<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html">must-not:</a><strong> </strong>— <em>Must_not </em>is similar to the “not” operator. It is the opposite of the <em>must </em>clause. Using <em>must_not </em>tells Elasticsearch that document matches cannot include any of the queries that fall under the <em>must_not </em>clause.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;bool&quot;: {<br>      &quot;must_not&quot;: [<br>        {<br>          &quot;term&quot;: {<br>            &quot;campaigns.isHolidayMode&quot;: {<br>              &quot;value&quot;: true<br>            }<br>          }<br>        },<br>        {<br>          &quot;term&quot;: {<br>            &quot;campaigns.isSellable&quot;: {<br>              &quot;value&quot;: false<br>            }<br>          }<br>        }<br>      ]<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html">range</a>: Returns documents that contain terms within a provided range, is used for filtering a field within a range. On the example, we want to retrieve the product documents that the price is greater than 10 or less than 150.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;bool&quot;: {<br>      &quot;must&quot;: [<br>        {<br>          &quot;range&quot;: {<br>            &quot;campaigns.price&quot;: {<br>              &quot;gte&quot;: 10,<br>              &quot;lte&quot;: 120<br>            }<br>          }<br>        }<br>      ]<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html">Sort:</a> Allows you to add one or more sorts on specific fields. Each sort can be reversed as well. Used to sort documents according to specified fields.</blockquote><pre>GET products/_search<br>{<br>  &quot;sort&quot;: [<br>    {<br>      &quot;documentCreationDate&quot;: {<br>        &quot;order&quot;: &quot;desc&quot;<br>      }<br>    }<br>  ]<br>}</pre><blockquote><a href="https://opster.com/guides/elasticsearch/search-apis/elasticsearch-limit-results-optimizing-query-performance-pagination/">Size:</a> Used to retrieve a specified number of documents.</blockquote><pre>GET products/_search<br>{<br>  &quot;size&quot;: 3,<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;brand.name&quot;: &quot;Koton&quot;<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-fields.html#source-filtering">Source:</a> Used to filter output document’s fields only to see the specified fields. For example we have a document that has 10 fields. We can filter and retrieve only the fields we want.</blockquote><pre>GET products/_search<br>{<br>  &quot;_source&quot;: &quot;id&quot;,<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;brand.name&quot;: &quot;Koton&quot;<br>    }<br>  }<br>}</pre><pre><br>GET products/_search<br>{<br>  &quot;_source&quot;: [&quot;id&quot;, &quot;documentCreationDate&quot;],<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;brand.name&quot;: &quot;Koton&quot;<br>    }<br>  }<br>}</pre><p>An example query combining sort, size, and sources, we want to filter Koton brand products, document creation date should be latest to oldest, and we want to see only document id and creation date fields:</p><pre>GET products/_search<br>{<br>  &quot;size&quot;: 3,<br>  &quot;_source&quot;: [&quot;id&quot;, &quot;documentCreationDate&quot;],<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;brand.name&quot;: &quot;Koton&quot;<br>    }<br>  },<br>  &quot;sort&quot;: [<br>    {<br>      &quot;documentCreationDate&quot;: {<br>        &quot;order&quot;: &quot;desc&quot;<br>      }<br>    }<br>  ]<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math">Date math:</a> The expression starts with an anchor date, which can either be now, or a date string ending with ||. This anchor date can optionally be followed by one or more maths expressions:</blockquote><blockquote>+1h: Add one hour</blockquote><blockquote>-1d: Subtract one day</blockquote><blockquote>/d: Round down to the nearest day</blockquote><blockquote>For example, the following search returns documents where the endDate field contains a date between today and yesterday.</blockquote><pre>GET products/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;bool&quot;: {<br>      &quot;must&quot;: [<br>        {<br>          &quot;range&quot;: {<br>            &quot;campaigns.campaignStartDate&quot;: {<br>              &quot;gte&quot;: &quot;now-1d/d&quot;,<br>              &quot;lte&quot;: &quot;now/d&quot;<br>            }<br>          }<br>        }<br>      ]<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html">Pagination:</a> By default, searches return the top 10 matching hits. To page through a larger set of results, you can use the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">search API</a>’s from and size parameters. The from parameter defines the number of hits to skip, defaulting to 0. The size parameter is the maximum number of hits to return. Together, these two parameters define a page of results.</blockquote><pre>GET products/_search<br>{<br>  &quot;from&quot;: 5,<br>  &quot;size&quot;: 20,<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;brand.name&quot;: &quot;Koton&quot;<br>    }<br>  }<br>}</pre><blockquote><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html">Aggregations:</a> An aggregation summarizes your data as metrics, statistics, or other analytics. Aggregations help you answer questions like:</blockquote><blockquote>What’s the average load time for my website?</blockquote><blockquote>How many products are in each product category?</blockquote><blockquote>You can run aggregations as part of a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html">search</a> by specifying the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html">search API</a>’s aggs parameter. In this example, we want to find out how many products do every brand have.</blockquote><pre>GET products/_search<br>{<br>  &quot;aggs&quot;: {<br>    &quot;my-agg-name&quot;: {<br>      &quot;terms&quot;: {<br>        &quot;field&quot;: &quot;brand.name&quot;<br>      }<br>    }<br>  }<br>}</pre><p><em>Sources:</em></p><p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/elasticsearch-intro.html">What is Elasticsearch?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e180449e20a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>