Elasticsearch 9 cheatsheet

Elasticsearch 9: Searching

7 common Elasticsearch 9 tasks.

Return everything

Return the first 10 documents.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_search?pretty"

Full-text search

Find documents whose name matches the text, ranked by relevance.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_search?pretty" -H "Content-Type: application/json" -d '{"query":{"match":{"name":"laptop"}}}'

Exact match on a keyword

Match a keyword field exactly.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_search?pretty" -H "Content-Type: application/json" -d '{"query":{"term":{"sku":"LT-100"}}}'

Combine conditions

Require a text match and filter by price without affecting the score.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_search?pretty" -H "Content-Type: application/json" -d '{"query":{"bool":{"must":[{"match":{"name":"laptop"}}],"filter":[{"range":{"price":{"lte":1000}}}]}}}'

Sort and paginate

Return 5 results starting at the tenth, cheapest first.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_search?pretty" -H "Content-Type: application/json" -d '{"from":10,"size":5,"sort":[{"price":"asc"}]}'

Aggregate

Compute the average price without returning documents.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_search?pretty" -H "Content-Type: application/json" -d '{"size":0,"aggs":{"avg_price":{"avg":{"field":"price"}}}}'

See how text is analyzed

Show the tokens the standard analyzer produces.

curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/_analyze?pretty" -H "Content-Type: application/json" -d '{"analyzer":"standard","text":"Quick Brown Foxes"}'

Was this page helpful?