Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save d68fbe50/ec8341ae82d09edf345cb57e05e55707 to your computer and use it in GitHub Desktop.
Save d68fbe50/ec8341ae82d09edf345cb57e05e55707 to your computer and use it in GitHub Desktop.
ElasticSearch cheat cheat

Elastic search cheat sheet

GET data

GET http://localhost:9200/:index/:type/:id

PUT (or update) data

PUT http://localhost:9200/:index/:type/(:id)

Check data existence

HEAD localhost:9200/:index/:type/:id

DELETE data

# All resources
DELETE http://localhost:9200/_all

# An index
DELETE http://localhost:9200/:index

# A type
DELETE http://localhost:9200/:index/:type

# An item
DELETE http://localhost:9200/:index/:type/:id

Searching

The hits object gives you the top 10 hits that matched the query. The score represents how well the results matched the query.

# Empty searches

# Entire database
GET http://localhost:9200/_search

# One index
GET http://localhost:9200/:index/_search

# Multuple indecies
GET http://localhost:9200/:index,:index/_search

# Wildcards
GET http://localhost:9200/hub*/_search
Pagination
# Page 1
GET http://localhost:9200/_search?size=5&from=0

# Page 2
GET http://localhost:9200/_search?size=5&from=5
On fields

Good for development, not for production. If you do not specify a field, elasticsearch automatically uses the _all field, which searches through all fields.

GET /_search?q:fieldName:query

Datatype mapping

When you insert data into elastic search, it uses dynamic detection to determine what kind of data each field is.

Get mapping
GET http://localhost:9200/:index/:type/_mapping
Changing mapping
  • If you are adding fields, there is no need to reindex.
  • If you need to change a field, you need to reindex your data. An article on the subject
Reindex data

Lookup more detailed information on how to do this

  • Create a new index with the new mapping (see PUT mapping below)
  • Pull in documents from the old index using a scrolled search and index them to the new index using the bulk API. Note: make sure that you include search_type=scan in your search request. This disables sorting and makes "deep paging" efficient.
  • Update index alias.
  • Delete the old index
PUT (or update) mapping
PUT http://localhost:9200/:index/:type -d '{
	"mappings": {
		"tweet": {
			"properties": {
				…
			}
			
		}
	}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment