Elastic Search in 2024

Atakan Demircioğlu
Stackademic
Published in
5 min readJan 6, 2024

--

These are my notes about Elastic search. This note series will start with a full-text search and cover semantic search.

To enhance the best experience for customers, we need to provide a great search experience.

So let's start.

Create an Elastic Account

Open the official Elastic website and create an account. It is free for 14 days.

After the registration process, you quickly fill in the onboarding steps and you will see this screen.

Select the “Build AI-powered search experience” option.

After that create an Index.

You have three options. You can collect data with a web crawler so it works well and also you can connect your database (ex: MySQL) or you can programmatically fill data with the API.

So I decided to use a PostgreSQL connector.

Give an index name and select the language analyzer (you can skip as universal)

Fill out the necessary information about your connector database.

After that, you can schedule or directly sync your data to Elastic.

You can create some pipelines to regularly update your index but in this article, I will not cover it.

At the end of sync, you can see the summary of your data like this in the Indices tab.

Let’s start with ElasticSearch

In this article, as I said I will make my examples with PHP.

Elastic PHP SDK: https://www.elastic.co/guide/en/elasticsearch/client/php-api/8.11/overview.html

I don’t want to talk about the installation part. For my example, I used Composer to add my project.

You will probably need an API key to connect to Elastic so basically you can go Security/ API Keys section and create an API key. (Just can type into the search bar to easily go to this section :D)

Connect with PHP

Let’s look at how to connect to Elastic with PHP. I will follow the official documentation and will show you how to do it.

I created a basic Elastic class that follows the Singleton pattern. Singleton pattern is an antipattern but for just testing it easily I used it in this example.

You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint.

After that, I created a custom class for use. For finding your HOST and API_KEY you can check out this link.

After the connection you can check the information by using;

$response = $client->info();

Also, the response implements PSR-7 ResponseInterface and ArrayAccess.

echo $response->getStatusCode(); // 200, 400 etc.

var_dump($response->asArray());
var_dump($response->asBool()); // true if HTTP response code between 200 and 300

Setting up Retries

By default, the client will retry n times, where n = number of nodes in your cluster.

To change the retry number or disable retries you can use this;

$client = ClientBuilder::create()
->setRetries(5) // here
->build();

Searching for a document

I don’t want to talk about indexing or deleting data from the index. My main purpose for this article is to make a great search :)

  • index: your created index
  • _source: fields to return
  • match: this part is to define in which field will make a search

Note: If you need to send an empty object PHP will convert it to;

"content" : []

This is not valid for Elasticsearch DSL. Please prefer to use this;

You can find more detailed examples in the “Dealing with JSON arrays and objects in PHP” article.

Bool Queries

Bool query is a type of query that allows you to combine multiple query clauses to express complex queries.

In this example, I am searching in two fields and if two fields match with the AND operator the results return.

If you want to search with different fields you can use “should”. Also, we have some different alternatives but I will cover them in the other parts of my article series.

This example works basically like an OR operator. Also if you want to search like this and add a filter you can use this;

In this example, we are filtering the public_linkedin_posts_new_reshared field. The other part is the same as the previous example.

Relevance Tuning

Maybe one of the best cool features of Elastic you can easily set up the relevance of your search.

There are different ways to change the relevance of your results. I will show the basic way for this example;

I added a boost to the URL field. So results coming from the second match query will have a high order.

Boost field takes float. The default is 1.0. Boost values are relative to the default value of 1.0. A boost value between 0 and 1.0 decreases the relevance score. A value greater than 1.0 increases the relevance score.

In the upcoming posts, I will cover the advanced parts of elastic. Stay tuned.

Atakan Demircioğlu is a Full Stack Developer currently working at Jotform, a leading online form-builder platform.

He is passionate about blogging, learning, and creating. He shares his experiences on Medium and GitHub.

Twitter: https://twitter.com/atakde

Github: https://github.com/atakde

Buy me a coffee if you like the content.

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.

--

--

Passionate about blogging and sharing insights on tech, web development, and beyond. Join me on this digital journey! 🚀