In today’s data-driven world, efficient search is critical. Traditional keyword search falls short when understanding context and user intent. Semantic search solves this problem by understanding meanings rather than just matching words.
In this tutorial, I’ll guide you through setting up Elasticsearch with the Elastic Learned Sparse EncodeR (ELSER) model for powerful semantic search capabilities. ELSER is Elastic’s specialized ML model that creates sparse vector representations to efficiently capture semantic meaning while maintaining computational performance.
By the end of this tutorial, you’ll have a functioning semantic search engine that can understand the intent behind queries and return more relevant search results. Let’s get started!
Start with setting up an ElasticSearch in docker.
Prerequisites
- A system that runs Docker
- A minimum of 8GB of system memory
We are going to start by launching Elasticsearch and Kibana on Docker.
Step 1: Set up the system
Setup the docker network
docker network create elastic
Before starting the Elasticsearch container, ensure your system has the correct sysctl configurations:
sudo sysctl -w vm.max_map_count=262144
To make this change permanent, add the following line to your /etc/sysctl.conf
file and activate it with sysctl -p
or by rebooting your system:
vm.max_map_count=262144
Step 2: Setup ElasticSearch and Kibana
Start ElasticSearch
docker run -d --name es01 --net elastic -p 9200:9200 -it -m 8GB docker.elastic.co/elasticsearch/elasticsearch:8.17.3
Before starting up Kibana, we need to generate an enrollment token.
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
Start Kibana
docker run -d --name kib01 --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.17.3
Retrieve the Kibana logs to start setting up the connection between Elasticsearch and Kibana.
docker logs kib01
It will return a link similar to this: http://0.0.0.0:5601/?code=xxxxx
. Open it in your browser. You will be prompted to enter the enrollment token generated earlier. After entering the token, access the Kibana logs again to get the verification code using the previous command.
Now you will see a login screen. You can retrieve the password from the Elasticsearch logs or generate a new one with the following command:
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
After completing this step, you are ready to begin working with Elasticsearch.
Step 3: Activate ElasticSearch
Since semantic search is a licensed feature of Elasticsearch, you need to activate a license. You can use a 30-day trial for testing. Open “Dev Tools” in Kibana and execute the following command:
POST /_license/start_trial?acknowledge=true
Step 4: Create the index mapping
We need an index to store our content.
PUT elser-index
{
"mappings": {
"properties": {
"content_embedding": {
"type": "sparse_vector"
},
"content": {
"type": "text"
}
}
}
}
Step 5: Create ingest pipeline
We need an ingest pipeline to use ELSER against the ingested data.
PUT _ingest/pipeline/elser-v2-test
{
"processors": [
{
"inference": {
"model_id": ".elser_model_2_linux-x86_64",
"input_output": [
{
"input_field": "content",
"output_field": "content_embedding"
}
]
}
}
]
}
Step 6: Set up the ML model
PUT _inference/sparse_embedding/my-elser-model
{
"service": "elser",
"service_settings": {
"adaptive_allocations": {
"enabled": true,
"min_number_of_allocations": 1,
"max_number_of_allocations": 10
},
"num_threads": 1
}
}
If the model times out, go to the Machine Learning tab in Kibana. It will show a message saying:
“ML job and trained model synchronization required.”
Click “Synchronize your jobs and trained models”, then click “Synchronize”.
You should now see your model under Model Management → Trained Models.
Step 7: Load and Index data
First, download the dataset msmarco-passagetest2019-unique.tsv
:
wget https://raw.githubusercontent.com/elastic/stack-docs/refs/heads/main/docs/en/stack/ml/nlp/data/msmarco-passagetest2019-unique.tsv
In the Kibana interface, navigate to Machine Learning → Data Visualizer → File. Upload the msmarco-passagetest2019-unique.tsv
file.
You will see an import overview. Adjust the field names using Override settings:
- The first column should be “id”
- The second column should be “content”
After making these adjustments, import the data into an index, e.g., "temp-index"
. Start the import process.
Now, reindex the data into elser-index
:
POST _reindex?wait_for_completion=false
{
"source": {
"index": "temp-index",
"size": 50
},
"dest": {
"index": "elser-index",
"pipeline": "elser-v2-test"
}
}
This process should be relatively quick. However, if you want to track its progress, use the task_id
from the previous command:
GET _tasks/<task_id>
Step 8: Perform a semantic search
Now that the data is imported, you can test out a semantic search query:
GET elser-index/_search
{
"query":{
"sparse_vector":{
"field": "content_embedding",
"inference_id": ".elser_model_2_linux-x86_64",
"query": "How to avoid muscle soreness after running?"
}
}
}
Conclusion
Congratulations! You’ve successfully set up Elasticsearch with ELSER for powerful semantic search capabilities. In this tutorial, we covered everything from creating a Docker environment for Elasticsearch and Kibana to configuring the ELSER model for natural language processing.
Semantic search represents a significant advancement over traditional keyword-based search, enabling your applications to:
- Understand the contextual meaning of search queries
- Return relevant results based on semantic similarity
- Process natural language queries more effectively
- Deliver better search experiences for users
This is just the beginning! You can expand your index with more data, fine-tune search parameters, or integrate this semantic search capability into your applications using Elasticsearch’s robust API.
Note: The trial license will expire after 30 days. If you wish to continue using these advanced features in a production environment, consider a paid license.
For more advanced use cases, explore additional Elasticsearch features such as relevance tuning, multi-language support, or hybrid search (combining semantic and keyword-based search).
Happy searching! 🚀