Skip to content

Instantly share code, notes, and snippets.

@VarunVats9
Last active February 29, 2020 19:15
Show Gist options
  • Save VarunVats9/336f8525fe501bae88741c16839bf3c5 to your computer and use it in GitHub Desktop.
Save VarunVats9/336f8525fe501bae88741c16839bf3c5 to your computer and use it in GitHub Desktop.
Elasticsearch - filter, _source, yml
// Filter
GET /bank/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"query": {
"bool": {
"must": [
{
"match": {
"lastname": "Smith"
}
}
],
"filter": [
{
"range": {
"balance": {
"lte": 150000
}
}
}
]
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 6.5032897,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "516",
"_score" : 6.5032897,
"_source" : {
"account_number" : 516,
"balance" : 44940,
"firstname" : "Roy",
"lastname" : "Smith",
"age" : 37,
"gender" : "M",
"address" : "770 Cherry Street",
"employer" : "Parleynet",
"email" : "roysmith@parleynet.com",
"city" : "Carrsville",
"state" : "RI"
}
}
]
}
}
// Sort
GET /bank/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"size": 2,
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc",
"mode": "avg"
}
}
]
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "248",
"_score" : null,
"_source" : {
"account_number" : 248,
"balance" : 49989,
"firstname" : "West",
"lastname" : "England",
"age" : 36,
"gender" : "M",
"address" : "717 Hendrickson Place",
"employer" : "Obliq",
"email" : "westengland@obliq.com",
"city" : "Maury",
"state" : "WA"
},
"sort" : [
49989
]
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "854",
"_score" : null,
"_source" : {
"account_number" : 854,
"balance" : 49795,
"firstname" : "Jimenez",
"lastname" : "Barry",
"age" : 25,
"gender" : "F",
"address" : "603 Cooper Street",
"employer" : "Verton",
"email" : "jimenezbarry@verton.com",
"city" : "Moscow",
"state" : "AL"
},
"sort" : [
49795
]
}
]
}
}
// Sort by multiple fields, and filter only required fields
// use _source for that
GET /bank/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"size": 3,
"_source": ["balance", "age", "firstname"] ,
"query": {
"match_all": {}
},
"sort": [
{
"balance": "desc",
"age": "asc"
}
]
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "248",
"_score" : null,
"_source" : {
"firstname" : "West",
"balance" : 49989,
"age" : 36
},
"sort" : [
49989,
36
]
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "854",
"_score" : null,
"_source" : {
"firstname" : "Jimenez",
"balance" : 49795,
"age" : 25
},
"sort" : [
49795,
25
]
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "240",
"_score" : null,
"_source" : {
"firstname" : "Oconnor",
"balance" : 49741,
"age" : 35
},
"sort" : [
49741,
35
]
}
]
}
}
// Include exclude source
GET /bank/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"size": 3,
"_source": {
"includes": ["firstname", "address"],
"excludes": "lastname"
} ,
"query": {
"match_all": {}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"firstname" : "Amber",
"address" : "880 Holmes Lane"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"firstname" : "Hattie",
"address" : "671 Bristol Street"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "13",
"_score" : 1.0,
"_source" : {
"firstname" : "Nanette",
"address" : "789 Madison Street"
}
}
]
}
}
// Offset
GET /bank/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"_source": false,
"size": 3,
"from": 6,
"query": {
"range": {
"balance": {
"gte": 10000
}
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 832,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "44",
"_score" : 1.0
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "49",
"_score" : 1.0
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "51",
"_score" : 1.0
}
]
}
}
// YAML result format Vs Json
GET /bank/_search?format=yaml >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"size": 3,
"from": 6,
"query": {
"range": {
"balance": {
"gte": 10000
}
}
}
}
---
took: 2
timed_out: false
_shards:
total: 1
successful: 1
skipped: 0
failed: 0
hits:
total:
value: 832
relation: "eq"
max_score: 1.0
hits:
- _index: "bank"
_type: "_doc"
_id: "44"
_score: 1.0
_source:
account_number: 44
balance: 34487
firstname: "Aurelia"
lastname: "Harding"
age: 37
gender: "M"
address: "502 Baycliff Terrace"
employer: "Orbalix"
email: "aureliaharding@orbalix.com"
city: "Yardville"
state: "DE"
- _index: "bank"
_type: "_doc"
_id: "49"
_score: 1.0
_source:
account_number: 49
balance: 29104
firstname: "Fulton"
lastname: "Holt"
age: 23
gender: "F"
address: "451 Humboldt Street"
employer: "Anocha"
email: "fultonholt@anocha.com"
city: "Sunriver"
state: "RI"
- _index: "bank"
_type: "_doc"
_id: "51"
_score: 1.0
_source:
account_number: 51
balance: 14097
firstname: "Burton"
lastname: "Meyers"
age: 31
gender: "F"
address: "334 River Street"
employer: "Bezal"
email: "burtonmeyers@bezal.com"
city: "Jacksonburg"
state: "MO"
GET /bank/_search?pretty
{
"size": 3,
"from": 6,
"query": {
"range": {
"balance": {
"gte": 10000
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 832,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "44",
"_score" : 1.0,
"_source" : {
"account_number" : 44,
"balance" : 34487,
"firstname" : "Aurelia",
"lastname" : "Harding",
"age" : 37,
"gender" : "M",
"address" : "502 Baycliff Terrace",
"employer" : "Orbalix",
"email" : "aureliaharding@orbalix.com",
"city" : "Yardville",
"state" : "DE"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "49",
"_score" : 1.0,
"_source" : {
"account_number" : 49,
"balance" : 29104,
"firstname" : "Fulton",
"lastname" : "Holt",
"age" : 23,
"gender" : "F",
"address" : "451 Humboldt Street",
"employer" : "Anocha",
"email" : "fultonholt@anocha.com",
"city" : "Sunriver",
"state" : "RI"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "51",
"_score" : 1.0,
"_source" : {
"account_number" : 51,
"balance" : 14097,
"firstname" : "Burton",
"lastname" : "Meyers",
"age" : 31,
"gender" : "F",
"address" : "334 River Street",
"employer" : "Bezal",
"email" : "burtonmeyers@bezal.com",
"city" : "Jacksonburg",
"state" : "MO"
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment