Skip to content

Instantly share code, notes, and snippets.

@rajanm
Last active September 20, 2020 23:55
Show Gist options
  • Save rajanm/3fdbc7999f0120ce5e87 to your computer and use it in GitHub Desktop.
Save rajanm/3fdbc7999f0120ce5e87 to your computer and use it in GitHub Desktop.
Elasticsearch Sample Scripts

This gist contains various example scripts for Elasticsearch. These scripts have been tested with Elasticsearch Server v1.5.2. These scripts can be run from shell/terminal in a Unix or from git bash in a Windows.

For a full fledged Java sample project, refer - ElasticsearchJavaSample

# This script shows an example of an aggregate query. Import the data using bulk.sh before running this script.
curl -XPOST "http://localhost:9200/products/product/_search" -d'{
"size": 10,
"aggregations": {
"agg_brand": {
"terms": {
"field": "brand"
}
}
}
}'
# This script shows an example of an bulk import. Execute this script after creating index using create_index_suggest.sh.
curl -XPOST "http://localhost:9200/products1/_bulk" -d'
{"create":{"_index":"products","_type":"product", "_id" : "300"}}
{"title":"Vanilla Ice Cream 200G", "price":"123", "brand":"Organics", "title_suggest": ["organics","organic"]}
{"create":{"_index":"products","_type":"product", "_id" : "310"}}
{"title":"Vanilla Ice Cream 500G", "price":"22.45", "brand":"Organics", "title_suggest": ["organics","organic"]}
{"create":{"_index":"products","_type":"product", "_id" : "320"}}
{"title":"Vanilla Ice Cream 2000G", "price":"33", "brand":"Organics", "title_suggest": ["organics",""]}
'
# This script shows examples of the cat API.
curl -XGET http://localhost:9200/_cat/master?v
curl -XGET http://localhost:9200/_cat/nodes?v
#This command retrieves the cluster health.
curl -XGET http://localhost:9200/_cluster/health?pretty=true
#This command retrieves the cluster state.
curl -XGET http://localhost:9200/_cluster/state?pretty
#This command retrieves the cluster statistics.
curl -XGET http://localhost:9200/_cluster/stats?pretty
#This command provides details on any pending tasks in a cluster.
curl -XGET http://localhost:9200/_cluster/pending_tasks
#This command updates a setting in a peresistent way (will persist after cluster restart)
curl -XPUT -d '{
"persistent" : {
"marvel.agent.interval" : "60s"
}
}' localhost:9200/_cluster/settings
# Create a product in the products index.
# If document already exists, it will update else create it.
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 1000G",
"id": "123",
"price" : 20.00,
"brand":"Kwality"
}' http://localhost:9200/products/product/123
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 2000G",
"id": "456",
"price" : 50.00,
"brand":"Baskin Robbins"
}' http://localhost:9200/products/product/456
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 1000G",
"id": "789",
"price" : 40.00,
"brand":"Amul"
}' http://localhost:9200/products/product/789
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 250G",
"id": "112",
"price" : 440.00,
"brand":"Ben & Jerry Ice Cream"
}' http://localhost:9200/products/product/112
# Create a product in the products index along with the suggestions for each product.
# If document already exists, it will update else create it.
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 1000G",
"id": "123",
"price" : 20.00,
"brand":"Kwality",
"title_suggest": {
"input" : ["kwality", "kwa", "ice"]
}
}' http://localhost:9200/products/product/123
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 2000G",
"id": "456",
"price" : 50.00,
"brand":"Baskin Robbins",
"title_suggest": {
"input" : ["cho", "chocolate", "ice"]
}
}' http://localhost:9200/products/product/456
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 1000G",
"id": "789",
"price" : 40.00,
"brand":"Amul",
"title_suggest": {
"input" : ["cho", "cream", "ice"]
}
}' http://localhost:9200/products/product/789
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chocolate Ice Cream 250G",
"id": "112",
"price" : 440.00,
"brand":"Ben & Jerry Ice Cream",
"title_suggest": {
"input" : ["ben", "jerry", "ice"]
}
}' http://localhost:9200/products/product/112
# This command creates an index called products with a type product containing the fields - id, title, brand and price.
# Elasticsearch should be running locally on port 9200.
curl -XPUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 0,
"index": {
"analysis": {
"analyzer": {
"eng": {
"type": "english",
"tokenizer": "standard",
"filter": [
"standard",
"asciifolding"
]
}
}
}
}
},
"mappings" : {
"product" : {
"_source" : { "enabled" : true },
"properties" : {
"id" : { "type" : "string", "index" : "not_analyzed" },
"title" : { "type" : "string", "index_analyzer" : "eng", "search_analyzer" : "eng" },
"price" : { "type" : "float", "index" : "analyzed"},
"brand" : { "type" : "string", "index_analyzer" : "eng", "search_analyzer" : "eng" }
}
}
}
}' http://localhost:9200/products
# This command creates an index called products with a type product containing the fields - id, title, brand and price. It also
# creates a suggester for the title field.
# Elasticsearch should be running locally on port 9200.
curl -XPUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 0,
"index": {
"analysis": {
"analyzer": {
"eng": {
"type": "english",
"tokenizer": "standard",
"filter": [
"standard",
"asciifolding"
]
}
}
}
}
},
"mappings" : {
"product" : {
"_source" : { "enabled" : true },
"properties" : {
"id" : { "type" : "string", "index" : "not_analyzed" },
"title" : { "type" : "string", "index_analyzer" : "eng", "search_analyzer" : "eng" },
"price" : { "type" : "float", "index" : "analyzed"},
"brand" : { "type" : "string", "index_analyzer" : "eng", "search_analyzer" : "eng" },
"title_suggest" : {
"type": "completion",
"index_analyzer": "eng",
"search_analyzer": "eng",
"payloads": true
}
}
}
}
}' http://localhost:9200/products
# This script creates a multilingual document.
# If document already exists, it will update else create it.
curl -XPUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"title" : "Chicken Mince",
"title_cz" : "Kuřecí Mince",
"title_th" : "ไก่สับG",
"id": "123456"
}' http://localhost:9200/productsmulti/product/123456
# This script creates an index with multiple analyzers in it.
curl -XPUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0,
"index": {
"analysis": {
"analyzer": {
"eng": {
"type": "english",
"tokenizer": "standard",
"filter": [
"standard",
"asciifolding"
]
},
"thai": {
"type": "thai",
"tokenizer": "standard",
"filter": [
"standard",
"asciifolding"
]
},
"czech": {
"type": "czech",
"tokenizer": "standard",
"filter": [
"standard",
"asciifolding"
]
}
}
}
}
},
"mappings": {
"product": {
"_source": {
"enabled": false
},
"properties": {
"id": {
"type": "string",
"index": "not_analyzed"
},
"title": {
"type": "string",
"index_analyzer": "eng"
},
"title_cz": {
"type": "string",
"index_analyzer": "czech"
},
"title_th": {
"type": "string",
"index_analyzer": "thai"
}
}
}
}
}' http://localhost:9200/productsmulti
# This script creates a warmer query against the products index.
curl -XPUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"query" : {
"bool" : {"must":[{"query_string":{"default_field":"_all", "query":"ice"}}],"must_not":[],"should":[]}
}
}' http://localhost:9200/products/product/_warmer/warmer1
# This script deletes a document in the index. 123456 is the id of the document.
curl -XDELETE -H "Content-Type: application/json" -H "Cache-Control: no-cache" http://localhost:9200/products/product/123456
# This script deletes an index in Elasticsearch.
curl -XDELETE -H "Content-Type: application/json" -H "Cache-Control: no-cache" http://localhost:9200/products
# This script deletes the warmer.
curl -XDELETE -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '' http://localhost:9200/products/_warmer/warmer1
# This scripts checks for the existence of a document.
curl -XGET -H "Content-Type: application/json" -H "Cache-Control: no-cache" http://localhost:9200/products/product/_search/exists?q=id:123456
# This script executes explain for a query and document combination.
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"query":{"bool":{"must":[{"query_string":{"default_field":"_all","query":"ice"}}],"must_not":[],"should":[]}}}' http://localhost:9200/products/product/123456/_explain
# This script retrieves a document by it's id.
curl -XGET -H "Content-Type: application/json" -H "Cache-Control: no-cache" http://localhost:9200/products/product/123456
# Run this script from the elasticsearch/bin folder. Ensure internet connectivity is available.
# Install the HEAD plugin. Access the plugin from http://localhost:9200/_plugin/head
plugin -install mobz/elasticsearch-head
# This script gets multiple id's in a single request.
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"docs" : [
{
"_type" : "product",
"_id" : "123456"
},
{
"_type" : "product",
"_id" : "789012"
}
]
}' http://localhost:9200/products/_mget
# This script gets the node statistics for all nodes or a specific node.
curl -XGET http://localhost:9200/_nodes/stats?pretty
curl -XGET http://localhost:9200/_nodes/stats/os?pretty
curl -XGET http://localhost:9200/_nodes/stats/os,process?pretty
curl -XGET http://localhost:9200/_nodes/stats/indices?pretty
curl -XGET http://localhost:9200/_nodes/stats/fielddata?pretty
# This script adds a perolator for the products index with an id 1.
curl -XPUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"query" : {
"bool" : {"must":[{"query_string":{"default_field":"_all", "query":"ice"}}],"must_not":[],"should":[]}
}
}' http://localhost:9200/products/.percolator/1
# This script executes a search query against the products index. The search term is ice.
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"query" : {
"bool" : {"must":[{"query_string":{"default_field":"_all", "query":"ice"}}],"must_not":[],"should":[]}
}
}' http://localhost:9200/products/product/_search
# This script searches for products and aggregates by the brand field. If size is set to 0, only aggregations will be returned.
curl -XPOST -d'{
"query" : {
"bool" : {"must":[{"query_string":{"default_field":"title", "query":"ice"}}],"must_not":[],"should":[]}
},
"size": 10,
"aggregations": {
"agg_brand": {
"terms": {
"field": "brand"
}
}
}
}' http://localhost:9200/products/product/_search
# This script applies boost in a query to the title field for the query - ice.
curl -XPOST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"query" : {
"query_string" : {
"fields" : ["title^2", "brand"],
"query" : "ice",
"use_dis_max" : true
}
}
}' http://localhost:9200/products/product/_search
# This script queries the product index to get the product suggestions for a given text - kwa.
curl -XPOST -d'
{
"product_suggest":{
"text":"kwa",
"completion": {
"field" : "title_suggest"
}
}
}' http://localhost:9200/products/_suggest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment