<?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 Gent Aliti on Medium]]></title>
        <description><![CDATA[Stories by Gent Aliti on Medium]]></description>
        <link>https://medium.com/@gent.aliti?source=rss-eaf113874c40------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*nNyDSYrYhjm0LVeuGugxqg.jpeg</url>
            <title>Stories by Gent Aliti on Medium</title>
            <link>https://medium.com/@gent.aliti?source=rss-eaf113874c40------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 20:41:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gent.aliti/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[The one with Elasticsearch and Spring Boot]]></title>
            <link>https://medium.com/@gent.aliti/the-one-with-elasticsearch-and-spring-boot-432bf0a6d2ba?source=rss-eaf113874c40------2</link>
            <guid isPermaLink="false">https://medium.com/p/432bf0a6d2ba</guid>
            <category><![CDATA[spring-data]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[spring-data-elasticsearch]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Gent Aliti]]></dc:creator>
            <pubDate>Wed, 02 Oct 2019 18:20:05 GMT</pubDate>
            <atom:updated>2019-10-02T21:51:17.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tRZp0iF2eMHrcSOgupg58g.png" /></figure><p>This article aims to give you an introduction to Elasticsearch.</p><p>First we will go through explanation of basic concepts, then we will build a simple Spring Boot application to see how easily ES can be integrated using Spring Data Elasticsearch.</p><p>As a prerequisite for this post you’ll need Docker, Java and Maven installed.</p><h3><strong>You know, for search…</strong></h3><p>Elasticsearch is an open source distributed search engine and aims to make full-text search easy by hiding the complexities of Apache Lucene behind a simple Rest API. Instead of storing data in rows and columns Elasticsearch stores data in documents as JSON.</p><p><strong>Near real time</strong></p><p>There is a slight latency between the time you index a document until the time it becomes searchable.</p><p><strong>Clusters and Nodes</strong></p><p>Elasticsearch is distributed by nature. It can run on multiple nodes (servers) within a cluster. Indexing and searching is done in parallel by all the nodes.</p><p><strong>Documents</strong></p><p>I mentioned earlier that in ES data is stored in documents. For example, if you’re developing an e-commerce system, you would have a document for product, one for ratings, comments etc.</p><p><strong>Shards and Replicas</strong></p><p>In one index you can store large amount of data which may exceed a node’s disk size. This is why ES subdivides your index into multiple pieces called shards. When you create an index, you can define the number of shards that you want. Each shard is in itself a fully-functional and independent “index” that can be hosted on any node in the cluster. <em>Each Elasticsearch shard is a Lucene index.</em></p><p>As a failover mechanism in case a shard/node goes offline, Elasticsearch provides the opportunity to make one or more copies of your index’s shards into <em>replica shards.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*zwHiqqdjlLeJeJUS6phu-A.gif" /></figure><p>You don’t need to be an expert in sharding, cluster discovery, or dozens of other distributed concepts. Elasticsearch can happily run on a single node living inside your laptop, but if you were to run the tutorial on a cluster containing 100 nodes, everything would work in exactly the same way ;) .</p><h3>Now let’s get our hands dirty</h3><p>With all basic concepts explained now it’s time to run elastic and sample data.</p><p>Run Elasticsearch in a docker container:</p><pre>docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e &quot;discovery.type=single-node&quot; elasticsearch:6.4.3</pre><h4>Load sample data</h4><p>Download data using this <a href="https://download.elastic.co/demos/kibana/gettingstarted/7.x/accounts.zip">link</a>.</p><p>We can use Elasticsearch’s bulk api to load the data:</p><pre>curl -H &#39;Content-Type: application/x-ndjson&#39; -XPOST &#39;localhost:9200/bank/account/_bulk?pretty&#39; --data-binary @accounts.json</pre><p>Verify if everything is ok:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aeb15236684c3c25c510f14dc939a16/href">https://medium.com/media/7aeb15236684c3c25c510f14dc939a16/href</a></iframe><p>Now we have indexed 1000 accounts with the following structure:</p><pre>{<br>    &quot;account_number&quot;: INT,<br>    &quot;balance&quot;: INT,<br>    &quot;firstname&quot;: &quot;String&quot;,<br>    &quot;lastname&quot;: &quot;String&quot;,<br>    &quot;age&quot;: INT,<br>    &quot;gender&quot;: &quot;M or F&quot;,<br>    &quot;address&quot;: &quot;String&quot;,<br>    &quot;employer&quot;: &quot;String&quot;,<br>    &quot;email&quot;: &quot;String&quot;,<br>    &quot;city&quot;: &quot;String&quot;,<br>    &quot;state&quot;: &quot;String&quot;<br>}</pre><h3>Query DSL</h3><p>Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries.</p><p>You can read more about query DSL in Elasticsearch <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl.html">docs</a>.</p><h3>Spring Data Elasticsearch</h3><p>Spring Data Elasticsearch project provides integration with the Elasticsearch search engine.</p><p>You can clone <a href="https://github.com/gentaliti/elasticsearch-spring-boot"><strong>gentaliti/elasticsearch </strong></a>where I implemented a search functionality from the data we just loaded in ES.</p><p>First add Spring Data Elasticsearch dependency:</p><pre>&lt;dependency&gt;            <br>   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;                <br>   &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt;        &lt;/dependency&gt;</pre><p>Then you would need to add <em>@EnableElasticsearchRepositories</em> in one of your configuration classes and edit <em>application.properties</em> with your configuration values.</p><p>There are two type of parameters by which we can perform filtering and search, <em>query</em> and <em>filters.</em></p><p><strong>Querying</strong></p><p>For the query parameter we are using <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html">MultiMatchQuery</a>, which allows us to search for a given string in all of our fields.</p><pre>MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(query);<br>searchQuery.must(multiMatchQuery);</pre><p><strong>Filtering</strong></p><p>We can use filter parameter to define multiple filters for our data, by separating them with “;”. This logic is handled in <em>SearchController:filter</em> method, which converts the <em>filter</em> string into a list of filters.</p><p>Then from that list of filters we build multiple bool queries and add them to the main bool query.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f733987197d96a8273cbbd39667fd5bb/href">https://medium.com/media/f733987197d96a8273cbbd39667fd5bb/href</a></iframe><p>I am using here “should” (equivalent of OR) for filters corresponding to the same field and “must” (equivalent of AND) for combining them with other filters.</p><p><strong>Let’s see give it a try</strong></p><p>I am not going to cover here how to run the project, as that is explained in repo’s readme.</p><p>After you run the Spring Boot application you can send http requests to the only endpoint available, where you can search and filter data.</p><p>For example an http request like this:</p><pre>curl -X GET \<br> &#39;<a href="http://localhost:8080/accounts?q=rockwell&amp;filter=city:nicholson;city:shaft">http://localhost:8080/accounts?q=rockwell&amp;filter=city:nicholson;city:shaft</a>&#39;</pre><p>is the equivalent of writing this query:</p><pre>{  <br>   <strong>&quot;query&quot;</strong>:{  <br>      <strong>&quot;bool&quot;</strong>:{  <br>         <strong>&quot;must&quot;</strong>:[  <br>            {  <br>               <strong>&quot;multi_match&quot;</strong>:{  <br>                  <strong>&quot;query&quot;</strong>:&quot;rockwell&quot;<br>               }<br>            },<br>            {  <br>               <strong>&quot;bool&quot;</strong>:{  <br>                  <strong>&quot;should&quot;</strong>:[  <br>                     {  <br>                        <strong>&quot;match&quot;</strong>:{  <br>                           <strong>&quot;city&quot;</strong>:&quot;nicholson&quot;<br>                        }<br>                     },<br>                     {  <br>                        <strong>&quot;match&quot;</strong>:{  <br>                           <strong>&quot;city&quot;</strong>:&quot;shaft&quot;<br>                        }<br>                     }<br>                  ]<br>               }<br>            }<br>         ]<br>      }<br>   }<br>}</pre><p>This finds all accounts which contain <strong>rockwell </strong>in one of it’s fields and it’s from <strong>Nicholson</strong> or <strong>Shaft</strong>.</p><p>There is only one account matching our query:</p><pre>{<br>    &quot;content&quot;: [<br>        {<br>            &quot;id&quot;: &quot;99&quot;,<br>            &quot;account_number&quot;: 99,<br>            &quot;balance&quot;: 47159,<br>            &quot;first_name&quot;: null,<br>            &quot;last_name&quot;: null,<br>            &quot;address&quot;: &quot;806 Rockwell Place&quot;,<br>            &quot;email&quot;: &quot;<a href="/service/mailto:ratliffheath@zappix.com">ratliffheath@zappix.com</a>&quot;,<br>            &quot;city&quot;: &quot;Shaft&quot;,<br>            &quot;state&quot;: &quot;ND&quot;,<br>            &quot;age&quot;: 39<br>        }<br>    ],<br>    &quot;pageable&quot;: {<br>        &quot;sort&quot;: {<br>            &quot;sorted&quot;: false,<br>            &quot;unsorted&quot;: true,<br>            &quot;empty&quot;: true<br>        },<br>        &quot;offset&quot;: 0,<br>        &quot;pageSize&quot;: 10,<br>        &quot;pageNumber&quot;: 0,<br>        &quot;paged&quot;: true,<br>        &quot;unpaged&quot;: false<br>    },<br>    &quot;facets&quot;: [],<br>    &quot;aggregations&quot;: null,<br>    &quot;scrollId&quot;: null,<br>    &quot;maxScore&quot;: 13.007463,<br>    &quot;totalElements&quot;: 1,<br>    &quot;totalPages&quot;: 1,<br>    &quot;size&quot;: 10,<br>    &quot;first&quot;: true,<br>    &quot;numberOfElements&quot;: 1,<br>    &quot;last&quot;: true,<br>    &quot;number&quot;: 0,<br>    &quot;sort&quot;: {<br>        &quot;sorted&quot;: false,<br>        &quot;unsorted&quot;: true,<br>        &quot;empty&quot;: true<br>    },<br>    &quot;empty&quot;: false<br>}</pre><h3>Wrapping up</h3><p>We have covered here Elasticsearch basic concepts as well as a simple Spring Boot application for querying and filtering the data. This of course can be expanded more, but I think is a good example to using Spring Data Elasticsearch.</p><p>In the next posts I will write more about Query DSL, Mapping and Data Modelling.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=432bf0a6d2ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>