PUT hotels
DELETE hotels
curl -XPUT 'localhost:9200/hotels?pretty' -H 'Content-Type: application/json' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
'
PUT hotels?pretty
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
GET /_cat/indices?v
POST hotels/default/1
{
"type" : "hotel",
"name" : "Motif Seattle",
"city" : "Seattle",
"countryCode" : "US",
"hotelRating" : 4,
"location" : {
"lat" : 47.60985,
"lon" : -122.33475
}
}
GET hotels/default/1
PUT hotels/default/1
{
"type" : "hotel",
"name" : "Motif Seattle",
"city" : "Seattle",
"countryCode" : "US",
"hotelRating" : 4,
"location" : {
"lat" : 47.60985,
"lon" : -122.33475
}
}
POST hotels/default/_search
GET /hotels/default/_search
{
"query": {
"match" : {
"name" : "Motif"
}
}
}
POST /hotels/default/1/_update
{
"doc" : {
"name" : "Motif",
"hotelRating": 3
}
}
POST /hotels/default/1/_update
{
"script" : "ctx._source.hotelRating += 1"
}
POST /hotels/default/2/_update
{
"script" : {
"source": "ctx._source.hotelRating = params.rating",
"lang": "painless",
"params" : {
"rating" : 4
}
},
"upsert" : {
"hotelRating" : 1
}
}
GET hotels/default/2
DELETE /hotels/default/2
## Delete by query
POST hotels/_delete_by_query
{
"query": {
"match": {
"name": "Motif"
}
}
}
See: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/hotels/default/_bulk?pretty" --data-binary "@hotels.json"
Notes: If you have existing JSON file that needs converting to NDJSON, there are few solutions:
GET _cluster/health
# Displays all commands
GET /_cat
# Health
GET /_cat/health
# Use format=json if preffered
GET /_cat/health?format=json
# Each command accepts help
GET /_cat/health?help
# Display results with column headers (verbose)
GET /_cat/nodes?v
# List indices
GET /_cat/indices?v
# Shards allocation
GET /_cat/allocation?v
## Assignment
- Create index
products
with 2 replicas and 5 shards - Bulk import 3-5 hotels with following fields: type, name, city, countryCode, hotelRating
- Delete 1 hotel
- Upsert 1 hotel (change name)
# Check existing mapping and compare it with the new one
GET hotels/default/_mapping
# Delete and recreate the mapping
DELETE /hotels
PUT hotels/
{
"mappings" : {
"default" : {
"properties" : {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"stars" : { "type" : "byte" },
"rooms" : { "type" : "short" },
"location" : { "type" : "geo_point" },
"city" : { "type" : "keyword" },
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"internet" : { "type" : "boolean" },
"service" : { "type" : "keyword" },
"checkin": { "type" : "date", "format" : "dateOptionalTime"}
}
}
}
}
# Reimport the bulk data
PUT /hotels/_mapping/default
{
"dynamic": "strict"
}
# test it
POST hotels/default/1
{
"type3" : "hotel"
}
Find all hotels in 2 km radius from the point: 37.556035, 127.005232 (latitude, longitude)
GET /hotels/default/_search
{
"sort" : [
{ "price" : {"order" : "asc" } },
"_score"
],
"query" : {
"term" : { "service" : "spa" }
}
}
GET hotels/default/_search
{
"size": 0,
"aggs" : {
"messages" : {
"filters" : {
"filters" : {
"spas" : { "match" : { "service" : "spa" }},
"weddings" : { "match" : { "service" : "wedding" }}
}
}
}
}
}
GET hotels/default/_search
{
"size": 0,
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "price",
"ranges" : [
{ "to" : 150 },
{ "from" : 150, "to" : 200 },
{ "from" : 250, "to" : 300 },
{ "from" : 300 }
]
}
}
}
}
References:
- Data source: https://github.com/wikibook/elasticsearch