Skip to main content
Back to Blog
MagentoFebruary 14, 20269 min read

Magento 2 Elasticsearch Tuning for Fast Search

Tune Elasticsearch for Magento 2 catalog search with optimized JVM settings, index configuration, synonym handling, and cluster sizing for high-traffic stores.

Introduction

Elasticsearch powers catalog search and layered navigation in Magento 2, and its performance directly affects how quickly shoppers find products. A poorly tuned Elasticsearch instance leads to slow search responses, timeouts during reindexing, and degraded storefront experience.

This guide covers JVM heap sizing, index settings, analyzer configuration, and monitoring for Magento 2 Elasticsearch deployments.

JVM Heap Configuration

The single most important Elasticsearch tuning parameter is JVM heap size. Set it to 50 percent of available RAM, but never exceed 31 GB (to stay within compressed oops range):

# /etc/elasticsearch/jvm.options.d/heap.options
-Xms4g
-Xmx4g

For a dedicated Elasticsearch node with 8 GB RAM, 4 GB heap is appropriate. The remaining 4 GB is used by the OS filesystem cache, which Elasticsearch relies on heavily for segment reads.

Index Settings for Magento

Magento creates indices automatically, but default settings are not optimal for large catalogs. Customize them via the Elasticsearch index template:

curl -X PUT "localhost:9200/_template/magento_template" -H 'Content-Type: application/json' -d'
{
  "index_patterns": ["magento2_*"],
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "refresh_interval": "30s",
    "index.max_result_window": 100000,
    "analysis": {
      "analyzer": {
        "magento_keyword": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": ["lowercase", "trim"]
        }
      }
    }
  }
}'

A single shard is sufficient for catalogs under 500,000 products. Increasing shards adds overhead without benefit for smaller datasets. Setting refresh_interval to 30 seconds reduces indexing overhead during reindex operations.

Synonym and Stopword Configuration

Improve search relevance by adding synonyms in Magento Admin under Stores > Synonyms. For custom analyzers at the Elasticsearch level:

curl -X PUT "localhost:9200/magento2_product/_settings" -H 'Content-Type: application/json' -d'
{
  "index": {
    "analysis": {
      "filter": {
        "synonym_filter": {
          "type": "synonym",
          "synonyms": [
            "tshirt, t-shirt, tee",
            "laptop, notebook, portable computer"
          ]
        }
      }
    }
  }
}'

Monitoring and Troubleshooting

Key metrics to watch:

# Cluster health
curl -s "localhost:9200/_cluster/health?pretty"

# Index stats
curl -s "localhost:9200/magento2_product/_stats?pretty" | jq '.indices[].total.search'

# Slow query log
curl -X PUT "localhost:9200/magento2_product/_settings" -H 'Content-Type: application/json' -d'
{
  "index.search.slowlog.threshold.query.warn": "2s",
  "index.search.slowlog.threshold.query.info": "1s"
}'

Monitor JVM garbage collection pauses. If GC pauses exceed 200ms regularly, increase heap size or add nodes.

For complementary performance improvements, see our guide on setting up Redis caching for Magento 2. Our Magento 2 speed optimization service includes full Elasticsearch tuning as part of our performance audit.

Tuning Elasticsearch for Magento 2 involves balancing JVM heap, shard configuration, and refresh intervals against your catalog size and traffic patterns. Regular monitoring of cluster health and slow query logs ensures search remains fast as your catalog grows.

Need help with this?

Our team handles this kind of work daily. Let us take care of your infrastructure.