<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nico Orfanos</title>
    <description>The latest articles on DEV Community by Nico Orfanos (@nicoorfi).</description>
    <link>https://dev.to/nicoorfi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1095228%2F95e43699-5627-439c-b2f6-ef8758e9ea56.jpg</url>
      <title>DEV Community: Nico Orfanos</title>
      <link>https://dev.to/nicoorfi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nicoorfi"/>
    <language>en</language>
    <item>
      <title>Understanding Elasticsearch Analyzers</title>
      <dc:creator>Nico Orfanos</dc:creator>
      <pubDate>Mon, 12 Jun 2023 20:01:29 +0000</pubDate>
      <link>https://dev.to/nicoorfi/understanding-elasticsearch-analyzers-22p8</link>
      <guid>https://dev.to/nicoorfi/understanding-elasticsearch-analyzers-22p8</guid>
      <description>&lt;p&gt;If you want to truly understand the analysis process in Elasticsearch, you need to get familiar with analyzers. It's what sets Elasticsearch apart from NoSQL databases like MongoDB.&lt;/p&gt;

&lt;p&gt;An analyzer in Elasticsearch is a pipeline. You feed it text, and it gives you back a bunch of &lt;strong&gt;tokens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The analyzer pipeline consists of three steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Analyzer
├── 1. Char filters
├── 2. Tokenizer
└── 3. Token filters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of them as different stages through which your text flows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Char filters
&lt;/h2&gt;

&lt;p&gt;First up, we have character filters. These filters preprocess the text before it gets split into tokens by the tokenizer. &lt;/p&gt;

&lt;p&gt;For example, it can transform emojis like &lt;code&gt;:)&lt;/code&gt; into the word &lt;code&gt;_happy&lt;/code&gt;.” &lt;/p&gt;

&lt;h2&gt;
  
  
  Tokenizer
&lt;/h2&gt;

&lt;p&gt;Next, we have the Tokenizer. The Tokenizer &lt;strong&gt;splits the text into smaller units called tokens&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;For instance, if we use the &lt;code&gt;whitespace&lt;/code&gt; tokenizer on the phrase "Hello World," it would intelligently split it into two tokens: &lt;code&gt;Hello&lt;/code&gt; and &lt;code&gt;World&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Token filters
&lt;/h2&gt;

&lt;p&gt;Now that our text is split into tokens, the token filters come to play. They are responsible for applying changes to the generated tokens. &lt;/p&gt;

&lt;p&gt;One popular use case is stemming, where the token &lt;code&gt;went&lt;/code&gt; is stemmed to &lt;code&gt;go&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;Bringing it all together, this entire process is referred to as &lt;strong&gt;analysis&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Note that analyzers can be customized by configuring different combinations of character filters, tokenizers, and token filters based on your requirements.&lt;/p&gt;

&lt;p&gt;Understanding analyzers is like holding the key to the relevancy capabilities in Elasticsearch. It allows you to fine-tune your search queries and ultimately enhance the overall user experience.&lt;/p&gt;

</description>
      <category>elasticsearch</category>
    </item>
    <item>
      <title>Elasticsearch Indices and Documents</title>
      <dc:creator>Nico Orfanos</dc:creator>
      <pubDate>Tue, 06 Jun 2023 07:35:53 +0000</pubDate>
      <link>https://dev.to/nicoorfi/elasticsearch-indices-and-documents-4j8i</link>
      <guid>https://dev.to/nicoorfi/elasticsearch-indices-and-documents-4j8i</guid>
      <description>&lt;p&gt;To use Elasticsearch effectively, it's essential to understand key concepts like indexes and documents. In this blog post, we'll explore what indexes and documents are in the context of Elasticsearch.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an Index?
&lt;/h2&gt;

&lt;p&gt;Imagine a kid's room with a drawer full of toys. In Elasticsearch, an index serves a similar purpose—it acts as a repository for related data. Think of it as a collection of documents that are grouped together for easy searching and retrieval.&lt;/p&gt;

&lt;p&gt;To visualize this, let's consider a simple representation of a movies index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;movies
├─ Document 1
├─ Document 2
├─ Document 3
├─ ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each individual piece of data within an index is called a &lt;strong&gt;Document&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can create an Index using &lt;a href="https://sigmie.com"&gt;Sigmie&lt;/a&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$sigmie&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;newIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'movies'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is a Document?
&lt;/h2&gt;

&lt;p&gt;A Document is just a JSON stored in an Index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document = JSON
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Elasticsearch, a &lt;strong&gt;Document&lt;/strong&gt; is represented as a JSON object and stored within an &lt;strong&gt;Index&lt;/strong&gt;. It contains the actual data that you want to search, analyze, or retrieve.&lt;/p&gt;

&lt;p&gt;Here's an example of what a document might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"The Shawshank Redemption"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The document contains a single field, &lt;code&gt;title&lt;/code&gt;, with the value &lt;code&gt;The Shawshank Redemption&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The JSON structure is allowing us to represent various data types and structures. The Document structure is the most important part of achieving what you want using Elasticsearch.&lt;/p&gt;

&lt;p&gt;Here’s how you can create an Instance of an Elasticsearch Document in &lt;a href="https://sigmie.com"&gt;Sigmie&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Sigmie\Document\Document&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$document&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The Shawshank Redemption'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is Indexing
&lt;/h2&gt;

&lt;p&gt;Indexing is the simple act of adding &lt;strong&gt;Documents&lt;/strong&gt; into an &lt;strong&gt;Index&lt;/strong&gt;. It doesn’t matter what are using Elasticsearch for, indexing is important simply because you can’t do anything without Documents in your index.&lt;/p&gt;

&lt;p&gt;And this is the way to index a &lt;strong&gt;Document&lt;/strong&gt; with &lt;a href="https://sigmie.com"&gt;Sigmie&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$index&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$document&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  An Example
&lt;/h2&gt;

&lt;p&gt;Here's an example demonstrating how to add three movie documents to our "movies" index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$documents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The Shawshank Redemption'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Inception'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Pulp Fiction'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nv"&gt;$index&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$documents&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we create an array of &lt;code&gt;Document&lt;/code&gt; objects, each representing a movie. By invoking the &lt;code&gt;merge&lt;/code&gt; method on the collected index and passing the documents, we add them to the index&lt;/p&gt;

&lt;p&gt;Here is what the Index looks like once we merge the Documents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;movies
├─ "The Shawshank Redemption"
├─ "Inception"
├─ "Pulp Fiction"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>elasticsearch</category>
      <category>sigmie</category>
      <category>php</category>
    </item>
    <item>
      <title>Run Elasticsearch on your local machine for development</title>
      <dc:creator>Nico Orfanos</dc:creator>
      <pubDate>Mon, 05 Jun 2023 07:41:03 +0000</pubDate>
      <link>https://dev.to/nicoorfi/run-elasticsearch-on-your-local-machine-for-development-1ebg</link>
      <guid>https://dev.to/nicoorfi/run-elasticsearch-on-your-local-machine-for-development-1ebg</guid>
      <description>&lt;p&gt;The Open Source version of Elasticsearch is the best solution out there for many things. We can use it in our daily developer lives as a Search Engine to create a fast and relevant search experience for our users or as analytics storage to craft some nice dashboards.&lt;/p&gt;

&lt;p&gt;If you want to try and work with Elasticsearch locally this post is for you. I will show you all you need to know to run and manage Elasticsearch on your local machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;The easiest way to run and manage Elasticsearch locally is Docker. If you haven't installed Docker yet, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run this command to download the Docker installation script.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://get.docker.com &lt;span class="nt"&gt;-o&lt;/span&gt; install-docker.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Once you downloaded the installation script run this command to install docker on your system.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;sh install-docker.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running Elasticsearch
&lt;/h2&gt;

&lt;p&gt;Assuming Docker is up and running, use the following command to run Elasticsearch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-p&lt;/span&gt; 9200:9200 &lt;span class="nt"&gt;--name&lt;/span&gt; elasticsearch &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"discovery.type=single-node"&lt;/span&gt; docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command pulls the Elasticsearch Open Source (OSS) image tagged with version 7.10.2, binds the ports 9200 to your localhost, and sets the discovery type to "single-node."&lt;/p&gt;

&lt;p&gt;Let’s see what all this means.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elasticsearch operates on port &lt;code&gt;9200&lt;/code&gt;  using the flag &lt;code&gt;-p 9200:9200&lt;/code&gt; we are binding this port from the docker container to our local machine. Making Elasticsearch available on &lt;code&gt;http://localhost:9200&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;—name elasticsearch&lt;/code&gt; we name our docker container so that we can reference it easily later on.&lt;/li&gt;
&lt;li&gt;When Elasticsearch is running in a production environment, it’s usually a Cluster of multiple Elasticsearch &lt;strong&gt;nodes&lt;/strong&gt; running. These nodes communicate with each other over the network and are always ready to &lt;strong&gt;discover&lt;/strong&gt; new nodes that want to join the cluster. Since we are running Elasticsearch on our local machine for development purposes it makes sense to disable this behavior. Passing the environmental variable &lt;code&gt;discovery.type=single-node&lt;/code&gt; we are telling Elasticsearch that it shouldn’t look for other Elasticsearch nodes in the network.&lt;/li&gt;
&lt;li&gt;And last we are pulling the &lt;strong&gt;OSS&lt;/strong&gt; image of Elasticsearch (&lt;code&gt;docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-amd64&lt;/code&gt;). OSS stands for “Open Source Software.” and is the open-source version of Elasticsearch that is more lightweight than the full version and includes all necessary functionalities of the search and analytics engine. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The OOS version is Licensed under the Apache 2.0 license giving us the freedom to use, modify, and distribute Elasticsearch without any licensing restrictions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.docker.elastic.co/r/elasticsearch/elasticsearch-oss?"&gt;Here you can find all the official Elasticsearch docker images provided by Elastic the company behind Elasticsearch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After the &lt;code&gt;docker run&lt;/code&gt; command finishes running you can check if Elasticsearch is also running with &lt;code&gt;curl&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; GET http://localhost:9200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is correct you will see this output in your console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0a25fd395139"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"cluster_name"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"docker-cluster"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"cluster_uuid"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nBknoxw0QyG-fHS8j9dW8w"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"number"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"7.10.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build_flavor"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"oss"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build_type"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"docker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build_hash"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"747e1cc71def077253878a59143c1f785afa92b9"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build_date"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-01-13T00:42:12.435326Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build_snapshot"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lucene_version"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"8.7.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"minimum_wire_compatibility_version"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"6.8.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"minimum_index_compatibility_version"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"6.0.0-beta1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"tagline"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"You Know, for Search"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Starting, Stopping, and Restarting Elasticsearch:
&lt;/h2&gt;

&lt;p&gt;Once you have Elasticsearch running as a Docker container, you can control its lifecycle using the following commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To stop Elasticsearch:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker stop elasticsearch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command stops the running Elasticsearch container gracefully.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To start Elasticsearch:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker start elasticsearch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command starts the previously created Elasticsearch container.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To restart Elasticsearch:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker restart elasticsearch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command restarts the Elasticsearch container and applies any configuration changes or recovers from crashes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dealing with Elasticsearch Crashes
&lt;/h2&gt;

&lt;p&gt;If your Elasticsearch crashes and &lt;code&gt;docker restart elasticsearch&lt;/code&gt; will not fix the problem, you can always wipe all Elasticsearch data away and start new.&lt;/p&gt;

&lt;p&gt;Elasticsearch stores its data in the &lt;code&gt;/usr/share/elasticsearch/data/nodes&lt;/code&gt; folder in the docker container. If you delete this folder you will have a fresh Elasticsearch to work with.&lt;/p&gt;

&lt;p&gt;Use the following command to delete the &lt;code&gt;nodes&lt;/code&gt; folder within the docker container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; elasticsearch &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /usr/share/elasticsearch/data/nodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command removes the Elasticsearch data stored in the container’s &lt;code&gt;/usr/share/elasticsearch/data/nodes&lt;/code&gt; directory. However, keep in mind that executing this command will result in &lt;strong&gt;data loss&lt;/strong&gt; and you shouldn’t use it if you have data in Elasticsearch that you don’t want to lose.&lt;/p&gt;

&lt;p&gt;After deleting the &lt;code&gt;nodes&lt;/code&gt; folder, it is essential to restart Elasticsearch to recreate the necessary data structures. Use the &lt;code&gt;docker restart elasticsearch&lt;/code&gt; command to do this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Running Elasticsearch with Docker provides a convenient way to manage Elasticsearch. By following the steps outlined you can quickly run Elasticsearch using Docker and start playing with it.&lt;/p&gt;

&lt;p&gt;Now that you have Elasticsearch running, in a future post, I will show you how you can use Elasticsearch to create a powerful search for your application.&lt;/p&gt;

</description>
      <category>php</category>
      <category>elasticsearch</category>
      <category>docker</category>
      <category>development</category>
    </item>
    <item>
      <title>Building and Searching an Elasticsearch Index with Sigmie in PHP</title>
      <dc:creator>Nico Orfanos</dc:creator>
      <pubDate>Sun, 04 Jun 2023 11:12:08 +0000</pubDate>
      <link>https://dev.to/nicoorfi/building-and-searching-an-elasticsearch-index-with-sigmie-in-php-djm</link>
      <guid>https://dev.to/nicoorfi/building-and-searching-an-elasticsearch-index-with-sigmie-in-php-djm</guid>
      <description>&lt;p&gt;Elasticsearch is a powerful search engine that allows you to index and search through large volumes of data quickly and efficiently. In this blog post, we will explore how to create an Elasticsearch index, populate it with documents, and perform searches using the &lt;a href="https://sigmie.com"&gt;Sigmie Library&lt;/a&gt; in PHP.&lt;/p&gt;

&lt;p&gt;FInd the instruction to install &lt;a href="https://sigmie.com/docs/v0/installation"&gt;Sigmie here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connection
&lt;/h3&gt;

&lt;p&gt;Before we can interact with Elasticsearch using Sigmie, we need to set up the Sigmie client. We need to connect to Elasticsearch and create an instance of the Sigmie class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Sigmie\Base\Http\ElasticsearchConnection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Sigmie\Http\JSONClient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Sigmie\Sigmie&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JSONClient&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'localhost:9200'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ElasticsearchConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$http&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$sigmie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Sigmie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating an Index
&lt;/h2&gt;

&lt;p&gt;To begin, we need to set up an Elasticsearch, let’s use the &lt;code&gt;movies&lt;/code&gt; Index name.  We also need to define the index properties. To do this we create a new instance of the &lt;code&gt;Sigmie\Mappings\NewProperties&lt;/code&gt; builder class and specifying two text field, the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt; fields. Also we enable &lt;strong&gt;lowercasing&lt;/strong&gt; that will improve our Search relevance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Sigmie\Mappings\NewProperties&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$properties&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NewProperties&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$properties&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$properties&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'description'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$sigmie&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;newIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'movies'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;properties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$properties&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Populating the Index
&lt;/h3&gt;

&lt;p&gt;Once the index is created, we can populate it with documents. We create an array of the &lt;code&gt;Sigmie\Document\Document&lt;/code&gt;  class and fill the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt; keys with values.&lt;/p&gt;

&lt;p&gt;In this example, we add three documents representing fictional movies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$index&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Mickey'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'description'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Adventure in the woods'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Goofy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'description'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Mickey and his friends'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Donald'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'description'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Chasing Goofy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]),&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Performing a Search
&lt;/h3&gt;

&lt;p&gt;After populating the index, we can search it. We create a new search instance, specifying the index name and passing the same properties that we defined earlier. We set the search query string to &lt;code&gt;mickey&lt;/code&gt; and specify the fields we want to search and to retrieve. In our example we search only the &lt;code&gt;name&lt;/code&gt; field, but we retrieve both &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt;. Finally, we execute the search and retrieve the hits as a JSON response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$sigmie&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;newSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'movies'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;properties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;queryString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'mickey'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;retrieve&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'description'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nv"&gt;$hits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$search&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hits'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion&lt;br&gt;
We have covered a simple approach to searching in Elasticsearch using Sigmie. We have seen how to set up an index, populate it with documents, and perform basic search. &lt;/p&gt;

&lt;p&gt;However, it is important to note that search is more than just finding a single word in a sea of documents. In future posts, we will explore dive deeper and explore various techniques to enhance your search.&lt;/p&gt;

</description>
      <category>php</category>
      <category>elasticsearch</category>
      <category>sigmie</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
