Skip to content

Instantly share code, notes, and snippets.

@VarunVats9
Created February 26, 2020 18:00
Show Gist options
  • Save VarunVats9/aa06486ce021248b548a6b45dbac214f to your computer and use it in GitHub Desktop.
Save VarunVats9/aa06486ce021248b548a6b45dbac214f to your computer and use it in GitHub Desktop.
Elasticsearch - mapping, index, get, update, delete, bulk, multiple action, search, search boolean
// Elasticsearch API
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html
// Typeless API
https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
// Mapping >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
PUT /product
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "double"
},
"description": {
"type": "text"
},
"status": {
"type": "keyword"
},
"quantity": {
"type": "integer"
},
"categories": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
}
}
},
"tags": {
"type": "keyword"
}
}
}
}
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "product"
}
// Index API >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
POST /product/_doc/1
{
"name": "Zend Framework 2: Beginner",
"price": 30.00,
"description": "Learn Zend Framework",
"status": "active",
"quantity": 1,
"categories": [{"name": "Software"}],
"tags": ["php", "zend framework"]
}
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
// Get API
GET /product/_doc/1
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Zend Framework 2: Beginner",
"price" : 40.0,
"description" : "Learn Zend Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"php",
"zend framework"
]
}
}
// Update API
POST /product/_update/1
{
"doc": {
"price": 90.00
}
}
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
// DELETE API >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DELETE /product/_doc/1
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
// Bulk API >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
POST /product/_bulk
{"index": {"_index": "product", "_id": "2"}}
{"name": "Spring Framework 2: Beginner","price": 79.00,"description":
"Learn Spring Framework","status": "active","quantity": 1,"categories": [{"name": "Software"}],
"tags": ["java", "Spring framework"]}
{"index": {"_index": "product", "_id": "3"}}
{"name": "Django Framework 2: Beginner","price": 59.00,"description":
"Learn Django Framework","status": "active","quantity": 1,"categories": [{"name": "Software"}],
"tags": ["python", "Django framework"]}
{
"took" : 9,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 1,
"status" : 201
}
}
]
}
// Bulk API - multiple actions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
POST /product/_bulk
{"delete" : {"_id": "1"}}
{"update": {"_id": "2"}}
{ "doc": {"price": 109.00}}
{
"took" : 61,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1,
"status" : 200
}
},
{
"update" : {
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1,
"status" : 200
}
}
]
}
// Search API >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search?q=*
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Django Framework 2: Beginner",
"price" : 59.0,
"description" : "Learn Django Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"python",
"Django framework"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Spring Framework 2: Beginner",
"price" : 109.0,
"description" : "Learn Spring Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"java",
"Spring framework"
]
}
}
]
}
}
// Search API - boolean >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GET /product/_search?q=(status:active AND (description:+Framework +Django -Spring))
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.4146938,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.4146938,
"_source" : {
"name" : "Django Framework 2: Beginner",
"price" : 59.0,
"description" : "Learn Django Framework",
"status" : "active",
"quantity" : 1,
"categories" : [
{
"name" : "Software"
}
],
"tags" : [
"python",
"Django framework"
]
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment