Skip to content

Instantly share code, notes, and snippets.

@whoisdevd
Last active August 29, 2020 14:35
Show Gist options
  • Save whoisdevd/4bd51c9e7b4e17cff783bf84c6f10641 to your computer and use it in GitHub Desktop.
Save whoisdevd/4bd51c9e7b4e17cff783bf84c6f10641 to your computer and use it in GitHub Desktop.
ES Kibana
# Create a new index for restaurants
PUT /restaurant
# Delete the index whenever you make schema changes and re-create the index. Search will not work for existing data if the index is deleted and recreated, unless back filled.
DELETE /restaurant
# Set mappings
PUT /restaurant/_mapping/doc
{
"properties": {
"address.location": {
"type": "geo_point"
},
"interactions": {
"type": "nested"
}
}
}
# Get all restaurant mappings
GET /restaurant/_mapping
# Get all restaurant documents
GET /restaurant/doc/_search
{
"size" : 10,
"query": {
"bool" : {
"must" : { "match_all" : {} }
}
}
}
# Add interaction
POST restaurant/doc/f852af13-aecd-4614-848e-6cb57348dbac/_update
{
"script": {
"lang": "painless",
"source": "return ctx._source.interactions == null ? ctx._source.interactions = [params.interaction] : ctx._source.interactions.add(params.interaction);",
"params": {
"interaction": {
"restaurantId": "647f67c6-99b8-462f-8ce0-5b4515c6845d",
"userId": "18b607fb-0900-4a6b-b324-3fb91a81c665",
"type": "Liked",
"interactedAt" : "2020-07-30T13:50:10.760Z"
}
}
}
}
# Search restaurants
GET /restaurant/_search
{
"size" : 10,
"query": {
"bool" : {
"must" : { "match_all" : {} },
"should" : [
{
"nested": {
"path": "interactions",
"query": {
"bool": {
"must": [
{
"match": {
"interactions.type": "Liked"
}
},
{
"match": {
"interactions.userId": "18b607fb-0900-4a6b-b324-3fb91a81c665"
}
}
]
}
},
"boost": -0.3
}
},
{
"nested": {
"path": "interactions",
"query": {
"bool": {
"must": [
{
"match": {
"interactions.type": "Disliked"
}
},
{
"match": {
"interactions.userId": "18b607fb-0900-4a6b-b324-3fb91a81c665"
}
}
]
}
},
"boost": -0.5
}
}
],
"filter" : [
{
"geo_distance" : {
"distance" : "4500m",
"distance_type": "arc",
"address.location" : {
"lat": 22.659254631928945,
"lon": 88.44146490097046
}
}
}
]
}
},
"sort": [
{
"_score": { "order": "desc" }
},
{
"_geo_distance": {
"address.location": {
"lat": 22.659254631928945,
"lon": 88.44146490097046
},
"order": "asc",
"unit": "m",
"distance_type": "arc"
}
}
]
}
# Update restaurant document partially. Overwrites matching fields and adds new ones.
POST restaurant/doc/647f67c6-99b8-462f-8ce0-5b4515c6845d/_update
{
"doc" : {
"address": {
"zip": 700051,
"country": "India",
"city": "Kolkata",
"street": "Nimta",
"locality": "Birati",
"location": {
"lon": 88.4110,
"lat": 22.6659
},
"state": "West Bengal"
}
}
}
# Update the entire document. Replaces existing document with given document.
# NOTE: Testing purpose only. Following action could result in data loss.
POST restaurant/doc/647f67c6-99b8-462f-8ce0-5b4515c6845d
{
"doc" : {
"name" : "Test Restaurant"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment