{"id":23821,"date":"2026-12-06T22:06:00","date_gmt":"2026-12-06T22:06:00","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23821"},"modified":"2026-12-06T22:06:00","modified_gmt":"2026-12-06T22:06:00","slug":"elasticsearch-lab3-dsl-query-collection-part1","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/elasticsearch-lab3-dsl-query-collection-part1\/","title":{"rendered":"Elasticsearch Lab#3 \u2013 DSL Query Collection Part#1"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This lab covers the two ways to send a search to Elasticsearch&#039;s <code>_search<\/code> API: URI query parameters (<code>?q=<\/code>) and a JSON Query DSL request body. It goes through pagination, sorting, <code>explain<\/code>, <code>term<\/code> queries, multi-index search and <code>_source<\/code> filtering. Below the original commands you will find sample data to run them against, an explanation of each technique and fixes for the commands that no longer work on Elasticsearch 7.x and 8.x.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/488abf9000b8bb2574e0c0ea3600bcb6.js\"><\/script>\n\n\n\n<h1 class=\"wp-block-heading\">Reference<\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>Sample Data &amp; Query &#8211; https:\/\/github.com\/devopsschool-demo-labs-projects\/elasticsearch<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Before you start: cluster access and sample data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The commands above use plain <code>http:\/\/localhost:9200<\/code> with no authentication. That works on an Elasticsearch 7.x lab node that has security turned off. On Elasticsearch 8.x, security and TLS are on by default, so every request needs HTTPS and credentials. Either add the options below to each curl command, or paste the request part (for example <code>GET \/customers\/_search<\/code>) into Kibana Dev Tools, which handles authentication for you.<\/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\"><span class=\"hljs-comment\"># Elasticsearch 8.x with default security<\/span>\nexport ES=<span class=\"hljs-string\">'https:\/\/localhost:9200'<\/span>\ncurl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt <span class=\"hljs-string\">\"$ES\/_cluster\/health?pretty\"<\/span>\n\n<span class=\"hljs-comment\"># Lab only: skip certificate verification<\/span>\ncurl -k -u elastic:$ELASTIC_PASSWORD <span class=\"hljs-string\">\"$ES\/customers\/_search?pretty\"<\/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\">The repository linked above has the full sample data set. If you only want to try the queries, the small <code>customers<\/code> index below is enough. Dynamic mapping turns each string field into <code>text<\/code> with a <code>keyword<\/code> sub-field (for example <code>state.keyword<\/code>) and turns <code>age<\/code> into a <code>long<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">cat &gt; customers.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\">\"name\"<\/span>:<span class=\"hljs-string\">\"Bill Gates\"<\/span>,<span class=\"hljs-string\">\"street\"<\/span>:<span class=\"hljs-string\">\"12 Chestnut Street\"<\/span>,<span class=\"hljs-string\">\"city\"<\/span>:<span class=\"hljs-string\">\"Seattle\"<\/span>,<span class=\"hljs-string\">\"state\"<\/span>:<span class=\"hljs-string\">\"Washington\"<\/span>,<span class=\"hljs-string\">\"age\"<\/span>:<span class=\"hljs-number\">45<\/span>,<span class=\"hljs-string\">\"gender\"<\/span>:<span class=\"hljs-string\">\"male\"<\/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\">\"name\"<\/span>:<span class=\"hljs-string\">\"Anna Brooks\"<\/span>,<span class=\"hljs-string\">\"street\"<\/span>:<span class=\"hljs-string\">\"88 Oak Avenue\"<\/span>,<span class=\"hljs-string\">\"city\"<\/span>:<span class=\"hljs-string\">\"Cheyenne\"<\/span>,<span class=\"hljs-string\">\"state\"<\/span>:<span class=\"hljs-string\">\"Wyoming\"<\/span>,<span class=\"hljs-string\">\"age\"<\/span>:<span class=\"hljs-number\">31<\/span>,<span class=\"hljs-string\">\"gender\"<\/span>:<span class=\"hljs-string\">\"female\"<\/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\">\"name\"<\/span>:<span class=\"hljs-string\">\"Tom Reed\"<\/span>,<span class=\"hljs-string\">\"street\"<\/span>:<span class=\"hljs-string\">\"5 Chestnut Lane\"<\/span>,<span class=\"hljs-string\">\"city\"<\/span>:<span class=\"hljs-string\">\"Louisville\"<\/span>,<span class=\"hljs-string\">\"state\"<\/span>:<span class=\"hljs-string\">\"Kentucky\"<\/span>,<span class=\"hljs-string\">\"age\"<\/span>:<span class=\"hljs-number\">52<\/span>,<span class=\"hljs-string\">\"gender\"<\/span>:<span class=\"hljs-string\">\"male\"<\/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\">\"name\"<\/span>:<span class=\"hljs-string\">\"Maria Lopez\"<\/span>,<span class=\"hljs-string\">\"street\"<\/span>:<span class=\"hljs-string\">\"301 Pine Road\"<\/span>,<span class=\"hljs-string\">\"city\"<\/span>:<span class=\"hljs-string\">\"Lexington\"<\/span>,<span class=\"hljs-string\">\"state\"<\/span>:<span class=\"hljs-string\">\"Kentucky\"<\/span>,<span class=\"hljs-string\">\"age\"<\/span>:<span class=\"hljs-number\">27<\/span>,<span class=\"hljs-string\">\"gender\"<\/span>:<span class=\"hljs-string\">\"female\"<\/span>}\nEOF\n\ncurl -XPOST <span class=\"hljs-string\">'localhost:9200\/customers\/_bulk?refresh&amp;pretty'<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/x-ndjson'<\/span> --data-binary @customers.ndjson<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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\"><strong>Note:<\/strong> Use <code>--data-binary<\/code>, not <code>-d<\/code>, for bulk files. <code>-d<\/code> strips newlines, and the bulk API needs every line, including the last one, to end with a newline. The <code>refresh<\/code> parameter makes the documents searchable straight away.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">URI search versus request body search<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both styles call the same <code>_search<\/code> endpoint. <code>?q=<\/code> uses Lucene query string syntax. With no field prefix it searches every eligible field, because <code>index.query.default_field<\/code> defaults to <code>*<\/code>. The request body uses the full Query DSL, and that is what you will use in real applications.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Aspect<\/th><th>URI search (?q=)<\/th><th>Request body (Query DSL)<\/th><\/tr><\/thead><tbody><tr><td>Example<\/td><td><code>?q=state:kentucky&amp;sort=age:asc<\/code><\/td><td><code>{&quot;query&quot;:{&quot;match&quot;:{&quot;state&quot;:&quot;kentucky&quot;}}}<\/code><\/td><\/tr><tr><td>Best for<\/td><td>Quick checks from a browser or the shell<\/td><td>Applications, complex bool logic, aggregations<\/td><\/tr><tr><td>Features<\/td><td>q, sort, from, size, explain, _source<\/td><td>All query types, aggregations, highlighting, search_after<\/td><\/tr><tr><td>Pitfalls<\/td><td>Special characters need URL encoding; bad syntax returns a parse error<\/td><td>More verbose; needs a <code>Content-Type: application\/json<\/code> header<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GET<\/code> and <code>POST<\/code> behave the same on <code>_search<\/code>. Some HTTP clients and proxies drop the body of a GET request. If that happens, send the same JSON with <code>POST<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reading the search response<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every search returns the same structure. In Elasticsearch 7.0 and later, <code>hits.total<\/code> is an object, not a plain number.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"took\"<\/span> : <span class=\"hljs-number\">3<\/span>,\n  <span class=\"hljs-attr\">\"timed_out\"<\/span> : <span class=\"hljs-literal\">false<\/span>,\n  <span class=\"hljs-attr\">\"_shards\"<\/span> : { <span class=\"hljs-attr\">\"total\"<\/span> : <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-attr\">\"successful\"<\/span> : <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-attr\">\"skipped\"<\/span> : <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-attr\">\"failed\"<\/span> : <span class=\"hljs-number\">0<\/span> },\n  <span class=\"hljs-attr\">\"hits\"<\/span> : {\n    <span class=\"hljs-attr\">\"total\"<\/span> : { <span class=\"hljs-attr\">\"value\"<\/span> : <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-attr\">\"relation\"<\/span> : <span class=\"hljs-string\">\"eq\"<\/span> },\n    <span class=\"hljs-attr\">\"max_score\"<\/span> : <span class=\"hljs-number\">0.87<\/span>,\n    <span class=\"hljs-attr\">\"hits\"<\/span> : &#91;\n      { <span class=\"hljs-attr\">\"_index\"<\/span> : <span class=\"hljs-string\">\"customers\"<\/span>, <span class=\"hljs-attr\">\"_id\"<\/span> : <span class=\"hljs-string\">\"3\"<\/span>, <span class=\"hljs-attr\">\"_score\"<\/span> : <span class=\"hljs-number\">0.87<\/span>,\n        <span class=\"hljs-attr\">\"_source\"<\/span> : { <span class=\"hljs-attr\">\"name\"<\/span> : <span class=\"hljs-string\">\"Tom Reed\"<\/span>, <span class=\"hljs-attr\">\"state\"<\/span> : <span class=\"hljs-string\">\"Kentucky\"<\/span>, <span class=\"hljs-attr\">\"age\"<\/span> : <span class=\"hljs-number\">52<\/span> } }\n    ]\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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<ul class=\"wp-block-list\">\n<li><code>took<\/code>: how long the search took, in milliseconds.<\/li>\n\n\n<li><code>hits.total.value<\/code> with <code>relation: eq<\/code>: the exact number of matches. <code>gte<\/code> means the count was capped, at 10,000 by default.<\/li>\n\n\n<li><code>_score<\/code>: the relevance score. If you sort on a field such as <code>age<\/code>, scores are not calculated and come back as <code>null<\/code>, unless you also sort by <code>_score<\/code> or set <code>track_scores<\/code>.<\/li>\n\n\n<li><code>_source<\/code>: the original JSON document. This is what source filtering trims.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Pagination, sorting and the explain parameter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>size<\/code> sets how many hits come back. The default is 10. <code>from<\/code> sets how many hits to skip, so page N has <code>from = (N - 1) * size<\/code>. By default <code>from + size<\/code> cannot be more than 10,000, the <code>index.max_result_window<\/code> setting. To page deeper, use <code>search_after<\/code> with a point in time instead of raising that limit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>????<\/code> in the example above is next to the <code>explain<\/code> commands. <code>explain=true<\/code> adds an <code>_explanation<\/code> object to every hit, showing how the score was calculated (BM25 term frequency, inverse document frequency and field length). Use it to debug relevance, not in production queries, because it adds overhead.<\/p>\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\">curl -XGET <span class=\"hljs-string\">'localhost:9200\/customers\/_search?q=state:kentucky&amp;explain=true&amp;pretty'<\/span>\n\n<span class=\"hljs-comment\"># Explain one document against a query<\/span>\ncurl -XGET <span class=\"hljs-string\">'localhost:9200\/customers\/_explain\/3?pretty'<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d<span class=\"hljs-string\">'\n{ \"query\": { \"match\": { \"state\": \"kentucky\" } } }'<\/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\"><strong>Note:<\/strong> Sort on numeric, date or <code>keyword<\/code> fields. Sorting on a <code>text<\/code> field such as <code>state<\/code> fails with a fielddata error. Use <code>state.keyword<\/code> instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Term versus match: why some term queries return nothing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>term<\/code> query looks for the exact token you give it and does not analyze the input. String fields are analyzed when they are indexed: the standard analyzer lowercases text and splits it into words. So <code>&quot;term&quot;: {&quot;name&quot;: &quot;gates&quot;}<\/code> matches &quot;Bill Gates&quot;, but <code>&quot;term&quot;: {&quot;name&quot;: &quot;Gates&quot;}<\/code> and <code>&quot;term&quot;: {&quot;name&quot;: &quot;bill gates&quot;}<\/code> match nothing. The <code>term<\/code> examples above only work because their values are single lowercase words.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Full-text search on a text field: input is analyzed, so case does not matter<\/span>\ncurl -XGET <span class=\"hljs-string\">'localhost:9200\/customers\/_search?pretty'<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d<span class=\"hljs-string\">'\n{ \"query\": { \"match\": { \"name\": \"Bill Gates\" } } }'<\/span>\n\n<span class=\"hljs-comment\"># Exact match on the keyword sub-field: case and spacing must match exactly<\/span>\ncurl -XGET <span class=\"hljs-string\">'localhost:9200\/customers\/_search?pretty'<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d<span class=\"hljs-string\">'\n{ \"query\": { \"term\": { \"state.keyword\": \"Washington\" } } }'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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\">Rule of thumb: use <code>match<\/code> for human-entered text, and use <code>term<\/code> or <code>terms<\/code> for IDs, status codes, tags and <code>.keyword<\/code> fields.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How source filtering works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Source filtering only changes which parts of <code>_source<\/code> are returned. It does not change which documents match or how they are scored. Wildcards are matched against field names.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Setting<\/th><th>Result with the sample data<\/th><\/tr><\/thead><tbody><tr><td><code>&quot;_source&quot;: false<\/code><\/td><td>Only metadata (<code>_index<\/code>, <code>_id<\/code>, <code>_score<\/code>) is returned. Useful when you only need IDs or counts.<\/td><\/tr><tr><td><code>&quot;_source&quot;: &quot;st*&quot;<\/code><\/td><td>Returns <code>street<\/code> and <code>state<\/code>.<\/td><\/tr><tr><td><code>&quot;_source&quot;: [&quot;st*&quot;, &quot;*n*&quot;]<\/code><\/td><td>Adds every field whose name contains <code>n<\/code>: <code>name<\/code> and <code>gender<\/code>.<\/td><\/tr><tr><td><code>includes<\/code> + <code>excludes: [&quot;*der&quot;]<\/code><\/td><td>Same as the line above, but <code>gender<\/code> is removed. Excludes win over includes.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To return values from the index instead of from <code>_source<\/code>, for example dates in a particular format, use the <code>fields<\/code> parameter: <code>&quot;fields&quot;: [&quot;state.keyword&quot;, &quot;age&quot;]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected commands for Elasticsearch 7.x and 8.x<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Two commands in the example above no longer work as written on current versions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Searching multiple document types.<\/strong> Mapping types were deprecated in 7.x and removed in 8.x, so <code>products\/shoes,laptops\/_search<\/code> cannot target types. Either store each kind of product in its own index, or keep one index and filter on a field:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Option 1: one index per product kind<\/span>\ncurl -XGET <span class=\"hljs-string\">'localhost:9200\/shoes,laptops\/_search?pretty'<\/span>\n\n<span class=\"hljs-comment\"># Option 2: one index with a category field<\/span>\ncurl -XGET <span class=\"hljs-string\">'localhost:9200\/products\/_search?pretty'<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d<span class=\"hljs-string\">'\n{ \"query\": { \"terms\": { \"category.keyword\": &#91;\"shoes\", \"laptops\"] } } }'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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\"><strong>Empty query object.<\/strong> <code>{&quot;query&quot;: {}}<\/code> is not a valid query clause, and current versions reject it with a parsing error. To return all documents, use <code>match_all<\/code> or leave out <code>query<\/code> entirely:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">curl -XGET <span class=\"hljs-string\">'localhost:9200\/customers\/_search?pretty'<\/span> -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> -d<span class=\"hljs-string\">'\n{ \"query\": { \"match_all\": {} } }'<\/span>\n\n<span class=\"hljs-comment\"># Same result, no body<\/span>\ncurl -XGET <span class=\"hljs-string\">'localhost:9200\/customers\/_search?pretty'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error or symptom<\/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><\/td><td>Body sent without a JSON header<\/td><td>Add <code>-H &#039;Content-Type: application\/json&#039;<\/code><\/td><\/tr><tr><td><code>curl: (52) Empty reply from server<\/code> or <code>missing authentication credentials<\/code><\/td><td>8.x cluster with security enabled<\/td><td>Use <code>https:\/\/<\/code> with <code>-u<\/code> and <code>--cacert<\/code>, or use Kibana Dev Tools<\/td><\/tr><tr><td><code>Text fields are not optimised for operations that require per-document field data<\/code><\/td><td>Sorting on a <code>text<\/code> field<\/td><td>Sort on the <code>.keyword<\/code> sub-field or a numeric field<\/td><\/tr><tr><td><code>Result window is too large<\/code><\/td><td><code>from + size<\/code> is more than 10,000<\/td><td>Use <code>search_after<\/code> with a point in time<\/td><\/tr><tr><td><code>term<\/code> query returns 0 hits<\/td><td>Case or multi-word value against an analyzed field<\/td><td>Use <code>match<\/code>, or <code>term<\/code> on <code>.keyword<\/code><\/td><\/tr><tr><td><code>index_not_found_exception<\/code><\/td><td>Index name typo or data not loaded<\/td><td>Check with <code>GET _cat\/indices?v<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\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 difference between query and filter context in Elasticsearch?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Clauses in query context calculate a relevance score. Clauses in filter context, such as <code>bool.filter<\/code>, only decide whether a document matches, and their results can be cached. Put exact conditions like status, ranges and keyword matches in filters, and put full-text search in the query.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use GET or POST for _search with a request body?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Elasticsearch handles both the same way. Use POST if your HTTP client, proxy or load balancer drops bodies on GET requests.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my term query return no results when the document clearly contains the value?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The field is probably <code>text<\/code>, which was lowercased and split into tokens at index time, while <code>term<\/code> looks for your exact input. Use a <code>match<\/code> query, or run the <code>term<\/code> query against the <code>.keyword<\/code> sub-field with the exact original value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I get more than 10,000 search results?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not page past 10,000 with <code>from<\/code> and <code>size<\/code>. Use <code>search_after<\/code> together with a point in time (PIT) for deep pagination, or the scroll API for one-off exports in older setups.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is the ?q= URI search safe to use in applications?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It suits quick checks, but user input passed into Lucene query string syntax can cause parse errors or expensive wildcard queries. Applications should build Query DSL bodies, or use <code>simple_query_string<\/code>, which handles bad syntax more gracefully.<\/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\/category\/monitoring-observability\/elastic\/\">More Elastic Stack (ELK) tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This lab covers the two ways to send a search to Elasticsearch&#039;s _search API: URI query parameters (?q=) and a JSON Query DSL request body. It goes&#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-23821","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\/23821","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=23821"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23821\/revisions"}],"predecessor-version":[{"id":80706,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23821\/revisions\/80706"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}