{"id":23831,"date":"2026-12-06T22:06:02","date_gmt":"2026-12-06T22:06:02","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23831"},"modified":"2026-12-06T22:06:02","modified_gmt":"2026-12-06T22:06:02","slug":"elasticsearch-lab5-example-of-popular-query","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/elasticsearch-lab5-example-of-popular-query\/","title":{"rendered":"Elasticsearch Lab#5 &#8211; Example of Popular Query"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This lab creates an Elasticsearch index with a set number of primary shards and replicas, then runs the queries you&#039;ll use most often against it. The example above sets <code>number_of_shards<\/code> and <code>number_of_replicas<\/code> when the index is created. Below you&#039;ll find a corrected command that works on current Elasticsearch versions, ways to check where shards ended up, sample data, and common query patterns such as match, term, range, bool and aggregations.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/4f0f4b1ece86f7162d0ccdcc44004b7d.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What the example above does<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example above sends a <code>PUT<\/code> request to create an index named <code>example<\/code>. It sets two index settings: <code>number_of_shards: 2<\/code> splits the data into two primary shards, and <code>number_of_replicas: 1<\/code> keeps one copy of each primary. That makes four shards in total: two primaries and two replicas. Each shard is a separate Lucene index, and Elasticsearch places the shards on nodes across the cluster.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Elasticsearch never puts a replica on the same node as its primary. On a single-node lab cluster, the two replica shards stay unassigned and the index health is <strong>yellow<\/strong>. This is expected behaviour, not an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The post title mentions queries, but the original example only creates the index. The query section further down covers the query part of the lab.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected index creation command<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Since Elasticsearch 6.0, any request with a JSON body must include a <code>Content-Type: application\/json<\/code> header. If you leave it out, as the example above does, the request fails with HTTP 406. Use <code>-X PUT<\/code> with the URL in quotes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">curl -X PUT <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\"<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> \\\n  -d <span class=\"hljs-string\">'{\n  \"settings\": {\n    \"index\": {\n      \"number_of_shards\": 2,\n      \"number_of_replicas\": 1\n    }\n  }\n}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Expected response:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{<span class=\"hljs-attr\">\"acknowledged\"<\/span>:<span class=\"hljs-literal\">true<\/span>,<span class=\"hljs-attr\">\"shards_acknowledged\"<\/span>:<span class=\"hljs-literal\">true<\/span>,<span class=\"hljs-attr\">\"index\"<\/span>:<span class=\"hljs-string\">\"example\"<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Elasticsearch 8.x turns on security by default, so plain HTTP without credentials won&#039;t connect. Use HTTPS, the CA certificate created on first start (for example <code>config\/certs\/http_ca.crt<\/code> in an archive install), and the <code>elastic<\/code> user:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">export<\/span> ES_URL=<span class=\"hljs-string\">\"https:\/\/localhost:9200\"<\/span>\n<span class=\"hljs-keyword\">export<\/span> ES_AUTH=<span class=\"hljs-string\">\"elastic:$ELASTIC_PASSWORD\"<\/span>\n\ncurl --cacert config\/certs\/http_ca.crt -u <span class=\"hljs-string\">\"$ES_AUTH\"<\/span> \\\n  -X PUT <span class=\"hljs-string\">\"$ES_URL\/example\"<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> \\\n  -d <span class=\"hljs-string\">'{\"settings\":{\"index\":{\"number_of_shards\":2,\"number_of_replicas\":1}}}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The rest of this lab uses <code>http:\/\/localhost:9200<\/code> to keep the commands short. On a secured cluster, add the same <code>--cacert<\/code> and <code>-u<\/code> options. In Kibana Dev Tools you can drop the curl wrapper and write <code>PUT \/example<\/code> followed by the JSON body.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify settings and shard allocation<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Show the index settings<\/span>\ncurl <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_settings?pretty\"<\/span>\n\n<span class=\"hljs-comment\"># Show every shard, whether it is a primary (p) or replica (r), and its state<\/span>\ncurl <span class=\"hljs-string\">\"http:\/\/localhost:9200\/_cat\/shards\/example?v\"<\/span>\n\n<span class=\"hljs-comment\"># Show the index health<\/span>\ncurl <span class=\"hljs-string\">\"http:\/\/localhost:9200\/_cat\/indices\/example?v\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">On a single-node cluster, <code>_cat\/shards<\/code> returns output like this (IP, size and node name will differ):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">index<\/span>   <span class=\"hljs-selector-tag\">shard<\/span> <span class=\"hljs-selector-tag\">prirep<\/span> <span class=\"hljs-selector-tag\">state<\/span>      <span class=\"hljs-selector-tag\">docs<\/span> <span class=\"hljs-selector-tag\">store<\/span> <span class=\"hljs-selector-tag\">ip<\/span>         <span class=\"hljs-selector-tag\">node<\/span>\n<span class=\"hljs-selector-tag\">example<\/span> 0     <span class=\"hljs-selector-tag\">p<\/span>      <span class=\"hljs-selector-tag\">STARTED<\/span>       0  249<span class=\"hljs-selector-tag\">b<\/span> 172<span class=\"hljs-selector-class\">.18<\/span><span class=\"hljs-selector-class\">.0<\/span><span class=\"hljs-selector-class\">.2<\/span> <span class=\"hljs-selector-tag\">node-1<\/span>\n<span class=\"hljs-selector-tag\">example<\/span> 0     <span class=\"hljs-selector-tag\">r<\/span>      <span class=\"hljs-selector-tag\">UNASSIGNED<\/span>\n<span class=\"hljs-selector-tag\">example<\/span> 1     <span class=\"hljs-selector-tag\">p<\/span>      <span class=\"hljs-selector-tag\">STARTED<\/span>       0  249<span class=\"hljs-selector-tag\">b<\/span> 172<span class=\"hljs-selector-class\">.18<\/span><span class=\"hljs-selector-class\">.0<\/span><span class=\"hljs-selector-class\">.2<\/span> <span class=\"hljs-selector-tag\">node-1<\/span>\n<span class=\"hljs-selector-tag\">example<\/span> 1     <span class=\"hljs-selector-tag\">r<\/span>      <span class=\"hljs-selector-tag\">UNASSIGNED<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">To find out why a shard isn&#039;t assigned, run <code>curl &quot;http:\/\/localhost:9200\/_cluster\/allocation\/explain?pretty&quot;<\/code>. To get a green index on a single node, set the replica count to zero (see the next section).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Changing shards and replicas after creation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>number_of_replicas<\/code> is a dynamic setting. You can change it on a live index at any time:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">curl -X PUT <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_settings\"<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> \\\n  -d <span class=\"hljs-string\">'{\"index\": {\"number_of_replicas\": 0}}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>number_of_shards<\/code> is fixed once the index exists. To change it, use one of these options:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Split API<\/strong> (<code>POST \/example\/_split\/example-4<\/code>): creates a new index with more primaries. The target count must be a multiple of the source count, and the source index must be made read-only first.<\/li>\n\n\n<li><strong>Shrink API<\/strong> (<code>POST \/example\/_shrink\/example-1<\/code>): creates a new index with fewer primaries. The target count must be a factor of the source count, and a copy of every shard must sit on one node.<\/li>\n\n\n<li><strong>Reindex<\/strong> (<code>POST \/_reindex<\/code>): copies documents into a new index that you create with any settings. It&#039;s the most flexible option and the slowest.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Load sample data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Index a few documents with the bulk API so the queries below return results. The bulk body is newline-delimited JSON. It must end with a newline and be sent with <code>--data-binary<\/code>. If you use <code>-d<\/code>, curl strips the newlines and the request fails.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">cat &gt; docs.ndjson &lt;&lt;<span class=\"hljs-string\">'EOF'<\/span>\n{<span class=\"hljs-string\">\"index\"<\/span>:{<span class=\"hljs-string\">\"_id\"<\/span>:<span class=\"hljs-string\">\"1\"<\/span>}}\n{<span class=\"hljs-string\">\"title\"<\/span>:<span class=\"hljs-string\">\"Install Elasticsearch on Ubuntu\"<\/span>,<span class=\"hljs-string\">\"category\"<\/span>:<span class=\"hljs-string\">\"tutorial\"<\/span>,<span class=\"hljs-string\">\"views\"<\/span>:<span class=\"hljs-number\">1200<\/span>,<span class=\"hljs-string\">\"published\"<\/span>:<span class=\"hljs-string\">\"2024-03-10\"<\/span>}\n{<span class=\"hljs-string\">\"index\"<\/span>:{<span class=\"hljs-string\">\"_id\"<\/span>:<span class=\"hljs-string\">\"2\"<\/span>}}\n{<span class=\"hljs-string\">\"title\"<\/span>:<span class=\"hljs-string\">\"Elasticsearch shard sizing guide\"<\/span>,<span class=\"hljs-string\">\"category\"<\/span>:<span class=\"hljs-string\">\"guide\"<\/span>,<span class=\"hljs-string\">\"views\"<\/span>:<span class=\"hljs-number\">3400<\/span>,<span class=\"hljs-string\">\"published\"<\/span>:<span class=\"hljs-string\">\"2024-06-02\"<\/span>}\n{<span class=\"hljs-string\">\"index\"<\/span>:{<span class=\"hljs-string\">\"_id\"<\/span>:<span class=\"hljs-string\">\"3\"<\/span>}}\n{<span class=\"hljs-string\">\"title\"<\/span>:<span class=\"hljs-string\">\"Kibana dashboards for beginners\"<\/span>,<span class=\"hljs-string\">\"category\"<\/span>:<span class=\"hljs-string\">\"tutorial\"<\/span>,<span class=\"hljs-string\">\"views\"<\/span>:<span class=\"hljs-number\">800<\/span>,<span class=\"hljs-string\">\"published\"<\/span>:<span class=\"hljs-string\">\"2023-11-20\"<\/span>}\n{<span class=\"hljs-string\">\"index\"<\/span>:{<span class=\"hljs-string\">\"_id\"<\/span>:<span class=\"hljs-string\">\"4\"<\/span>}}\n{<span class=\"hljs-string\">\"title\"<\/span>:<span class=\"hljs-string\">\"Logstash pipeline troubleshooting\"<\/span>,<span class=\"hljs-string\">\"category\"<\/span>:<span class=\"hljs-string\">\"guide\"<\/span>,<span class=\"hljs-string\">\"views\"<\/span>:<span class=\"hljs-number\">150<\/span>,<span class=\"hljs-string\">\"published\"<\/span>:<span class=\"hljs-string\">\"2025-01-15\"<\/span>}\nEOF\n\ncurl -X POST <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_bulk?refresh=true\"<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/x-ndjson'<\/span> \\\n  --data-binary @docs.ndjson<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">No mapping was defined, so dynamic mapping decides the field types. <code>title<\/code> and <code>category<\/code> become <code>text<\/code> fields with a <code>.keyword<\/code> sub-field, <code>views<\/code> becomes <code>long<\/code>, and <code>published<\/code> becomes <code>date<\/code>. Check the result with <code>GET \/example\/_mapping<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Popular Elasticsearch queries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">All of these use <code>GET \/example\/_search<\/code> with a JSON body. Add <code>?pretty<\/code> to the URL to get readable output.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 1. match: full-text search on an analyzed field<\/span>\ncurl -X GET <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_search?pretty\"<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d <span class=\"hljs-string\">'{\n  \"query\": { \"match\": { \"title\": \"elasticsearch guide\" } }\n}'<\/span>\n\n<span class=\"hljs-comment\"># 2. term: exact value on a keyword field<\/span>\ncurl -X GET <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_search?pretty\"<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d <span class=\"hljs-string\">'{\n  \"query\": { \"term\": { \"category.keyword\": \"tutorial\" } }\n}'<\/span>\n\n<span class=\"hljs-comment\"># 3. range: numeric or date ranges<\/span>\ncurl -X GET <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_search?pretty\"<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d <span class=\"hljs-string\">'{\n  \"query\": { \"range\": { \"published\": { \"gte\": \"2024-01-01\", \"lt\": \"2025-01-01\" } } }\n}'<\/span>\n\n<span class=\"hljs-comment\"># 4. bool: combine scored (must) and non-scored (filter) clauses<\/span>\ncurl -X GET <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_search?pretty\"<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d <span class=\"hljs-string\">'{\n  \"query\": {\n    \"bool\": {\n      \"must\":   &#91; { \"match\": { \"title\": \"elasticsearch\" } } ],\n      \"filter\": &#91; { \"range\": { \"views\": { \"gte\": 1000 } } } ]\n    }\n  }\n}'<\/span>\n\n<span class=\"hljs-comment\"># 5. Most popular documents: sort by views, return the top 3<\/span>\ncurl -X GET <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_search?pretty\"<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d <span class=\"hljs-string\">'{\n  \"size\": 3,\n  \"sort\": &#91; { \"views\": \"desc\" } ],\n  \"_source\": &#91;\"title\", \"views\"]\n}'<\/span>\n\n<span class=\"hljs-comment\"># 6. terms aggregation: document count and total views per category<\/span>\ncurl -X GET <span class=\"hljs-string\">\"http:\/\/localhost:9200\/example\/_search?pretty\"<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d <span class=\"hljs-string\">'{\n  \"size\": 0,\n  \"aggs\": {\n    \"by_category\": {\n      \"terms\": { \"field\": \"category.keyword\" },\n      \"aggs\": { \"total_views\": { \"sum\": { \"field\": \"views\" } } }\n    }\n  }\n}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Query<\/th><th>Use it for<\/th><th>Scored<\/th><\/tr><\/thead><tbody><tr><td><code>match<\/code><\/td><td>Full-text search on <code>text<\/code> fields<\/td><td>Yes<\/td><\/tr><tr><td><code>term<\/code> \/ <code>terms<\/code><\/td><td>Exact values on <code>keyword<\/code>, numeric or boolean fields<\/td><td>Yes, but usually placed in <code>filter<\/code><\/td><\/tr><tr><td><code>range<\/code><\/td><td>Numbers and dates<\/td><td>No, when used in <code>filter<\/code><\/td><\/tr><tr><td><code>bool<\/code><\/td><td>Combining clauses with <code>must<\/code>, <code>should<\/code>, <code>must_not<\/code>, <code>filter<\/code><\/td><td>Only <code>must<\/code> and <code>should<\/code><\/td><\/tr><tr><td><code>terms<\/code> aggregation<\/td><td>Top values and counts per group<\/td><td>Not applicable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Clauses inside <code>filter<\/code> don&#039;t affect scoring, and Elasticsearch can cache them. Put exact-match and range conditions there, and use <code>must<\/code> only for clauses that should change relevance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>Content-Type header [application\/x-www-form-urlencoded] is not supported<\/code> (406)<\/td><td>No JSON content type on the request<\/td><td>Add <code>-H &#039;Content-Type: application\/json&#039;<\/code><\/td><\/tr><tr><td><code>resource_already_exists_exception<\/code> (400)<\/td><td>The index <code>example<\/code> already exists<\/td><td>Delete it with <code>DELETE \/example<\/code> in a lab, or pick a new name<\/td><\/tr><tr><td><code>curl: (52) Empty reply from server<\/code><\/td><td>Plain HTTP sent to an 8.x cluster with TLS turned on<\/td><td>Use <code>https:\/\/<\/code>, <code>--cacert<\/code> and <code>-u elastic<\/code><\/td><\/tr><tr><td><code>missing authentication credentials<\/code> (401)<\/td><td>Security is on and no user was given<\/td><td>Pass <code>-u user:password<\/code> or an API key header<\/td><\/tr><tr><td><code>term<\/code> query returns no hits on <code>category<\/code><\/td><td><code>category<\/code> is an analyzed <code>text<\/code> field, so its value was lowercased and tokenized<\/td><td>Query <code>category.keyword<\/code>, or map the field as <code>keyword<\/code><\/td><\/tr><tr><td>Index health is yellow<\/td><td>The replica can&#039;t be placed on the same node as its primary<\/td><td>Add a node, or set <code>number_of_replicas<\/code> to 0 on a single-node lab<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Shard sizing best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Since Elasticsearch 7.0 the default is one primary shard per index. Add more only when data size or indexing throughput needs it.<\/li>\n\n\n<li>Elastic&#039;s general guidance is shard sizes of about 10GB to 50GB and no more than about 200 million documents per shard. Many small shards waste heap and cluster-state resources.<\/li>\n\n\n<li>For time-series data such as logs, use data streams or rollover with index lifecycle management (ILM) rather than one large index with many shards.<\/li>\n\n\n<li>Use at least one replica in production for high availability. Replicas can also serve search requests, which increases read throughput.<\/li>\n\n\n<li>Put shard and replica settings in an index template, so that every new index gets the same settings.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is the default number of shards in Elasticsearch?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since Elasticsearch 7.0, a new index gets one primary shard and one replica by default. Older versions used five primary shards by default.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I change number_of_shards on an existing index?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. The primary shard count is fixed when the index is created. To change it, use the split or shrink APIs, or reindex into a new index. The replica count can be changed at any time through <code>_settings<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why is my index yellow after creating it with one replica?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On a single-node cluster, the replica shards can&#039;t be assigned because Elasticsearch never places a replica on the same node as its primary. Add a second node, or set <code>number_of_replicas<\/code> to 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between match and term queries?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>match<\/code> query runs your input through the field&#039;s analyzer and searches full text. A <code>term<\/code> query looks for the exact value without analysis, so it belongs on <code>keyword<\/code>, numeric or date fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my curl command fail with a 406 error?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Elasticsearch 6.0 and later require a <code>Content-Type: application\/json<\/code> header on requests with a JSON body. Add <code>-H &#039;Content-Type: application\/json&#039;<\/code> to the curl command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-elasticsearch-and-use-cases-of-elasticsearch\/\">What is Elasticsearch and use cases of Elasticsearch?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-elasticsearch-interview-questions-and-answers\/\">Top 50 Elasticsearch Interview Questions and Answers<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-elk-stack-and-use-cases-of-elk-stack\/\">What is ELK Stack and use cases of ELK Stack?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/monitoring-observability\/elastic\/\">More Elastic Stack (ELK) tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This lab creates an Elasticsearch index with a set number of primary shards and replicas, then runs the queries you&#039;ll use most often against it. The example&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5936],"tags":[4666],"class_list":["post-23831","post","type-post","status-publish","format-standard","hentry","category-elastic","tag-elasticsearch"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=23831"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23831\/revisions"}],"predecessor-version":[{"id":80708,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23831\/revisions\/80708"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}