Elasticsearch

Most of our projects are Apps that we develop for our customers, usually natively or hybrid based on Ionic and Angular.

elasticsearch-logo.png

It is common that we need a full-text-search engine for contents where the user can type, in addition to being able to parameterize other data such as date, geolocation or filtering fields from selectable option lists.

Most of the times we recommend using Elasticsearch because it has many features that, sooner or later, end up being necessary to incorporate into our applications:

  • Clustering and high availability
  • Horizontal scalability
  • Snapshot and restore
  • Amazing powerfull search API
  • Support for aggregations
  • Ingest and management API

However, great power carries great… configuration. And not always is easy to implement searching in Elasticsearch or having a simple administration tool allowing from day 1 to have a simple environment on which to implement an MVP that can eventually become a killer app.

Until now…

Elastic App Search: Advanced Search Made Easy

It is amazing how easy it can be to implement an Elasticsearch-based environment that has everything you need to start creating a complete search application.

With Elastic App Search, the entire process is streamlined allowing you to concentrate efforts and resources on what matters most: create great applications that implement content search in a scalable manner, with control of results relevance, using well-maintained clients and robust analytics.

Starting a Simple Search Application

What if we create a simple application to search for food recipes? Next we will see the necessary steps to create our first application with Elastic App Search.

If you needed you can download all the contents from GitHub:

https://github.com/okode/appsearch

Creating Sample Data

To do this we will start from a sample data set that we can get from different sources (e.g. from TheMealDB: https://www.themealdb.com).

for first in {a..z}
do
    curl -s "https://www.themealdb.com/api/json/v1/1/search.php?f=${first}" | jq -s '[.[][][]]' >> data.json
done

This will create a data.json file with a data structure similar to the following:

[
    {
        "id": "52768",
        "meal": "Apple Frangipan Tart",
        "category": "Dessert",
        "area": "British",
        "instructions": "...",
        "thumb": "http://..."
    },
    {
        "id": "52872",
        "meal": "Spanish Tortilla",
        "category": "Vegetarian",
        "area": "Spanish",
        "instructions": "...",
        "thumb": "http://..."
    }
    ...
]

Running Elastic App Search With Docker Compose

Create the following docker-compose.yml file:

version: '3.7'

services:

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.3.0
    environment:
    - "node.name=es-node"
    - "discovery.type=single-node"
    - "cluster.name=app-search-docker-cluster"
    - "bootstrap.memory_lock=true"
    - "ES_JAVA_OPTS=-Xms512m -Xmx2048m"
    ulimits:
      memlock:
        soft: -1
        hard: -1

  appsearch:
    image: docker.elastic.co/app-search/app-search:7.3.0
    environment:
    - "elasticsearch.host=http://elasticsearch:9200"
    - "allow_es_settings_modification=true"
    - "JAVA_OPTS=-Xmx2048m"
    ports:
    - 3002:3002

Notice that it is important to increase the maximum heap memory values for the elasticsearch and appsearch services from 512m to 2048m.

Once created it is necessary to start the services in order so that elasticsearch has enough time to start before appsearch starts:

$ docker-compose up -d elasticsearch
$ docker-compose up -d appsearch

If you need to monitor the status of the processes, you can do so with the following command:

$ docker-compose logs -f

This will show the following:

app-search-logs

Data Ingestion Using Elastic App Search Dashboard

Open the following address and you will see the Elastic App Search Dashboard:

http://localhost:3002

app-search-dashboard-home

Some of the concepts we know in Elasticsearch have a different name in Elastic App Search. Specifically, an index is renamed to engine in App Search.

Proceed to create a new engine called meals and load the previously created data.json file:

app-search-ingest

Simple Searching

From here it is possible to carry out simple searching using the Elastic App Search Dashboard UI:

app-search-query-tester.png

Internally, searches are executed by Elasticsearch, so it is always possible to visualize the actual search performed by Elasticsearch from behind:

app-search-query-tester-details.png

Configuring Synonims

For some searches the term tortilla (omelette in Spanish) is not suitable since it would be desirable for searches to return Spanish Tortilla when searching for omelette.

For this it is possible to configure the synonyms in a simple and intuitive way:

app-search-synonims.png

This allows subsequent searches such as the following obtaining the expected results:

Configuring Curations

In case you want to promote a particular search result in a special way, it can be done from the Curations option.

A curation helps people discover what you would most like them to discover. Or, what you would not like them to discover.

There are two curation parameters:

  • Promoted: Promote specific documents to have them appear in a prominent way. You can promote multiple documents per query, the array order representing the order in which they will appear.
  • Hidden: Hide specific documents so that they do not appear. You can hide multiple documents per query, the array order being unimportant because … they are hidden.

Configuring Relevance Tuning

One of the most powerful features of Elasticsearch is its ability to refine the relevance of some results over others. It is possible to enhance the fact that a result depends more on the search on a specific field than on others, by configuring the final score that Elasticsearch gives to each search result.

With Elastic App Search this configuration of relevance in the results is extremely simple to configure.

app-search-relevance-tuning.png

Analytics

Standard Elastic App Search includes a simple but powerful analytics dashboard where it is possible to visualize, filtering by dates:

  • Total number of queries
  • Total number of queries without results
  • Tags
  • Detailed analytics by searches performed by end users
  • Recent Searches

app-search-recent-queries.png

About the Author Okode

www.okode.com