Query Elasticsearch
Create schema for index
PUT employees/_mapping
{
"properties": {
"date_of_birth": {
"type": "date",
"format": "dd/MM/yyyy"
}
}
}
Insert Batch
POST employees/_bulk?refresh=true
{ "index" : { "_id" : 1 } }
{ "name":"Huntlee Dargavel","email":"hdargavel0@japanpost.jp","gender":"male","ip_address":"58.11.89.193","date_of_birth":"11/09/1990","company":"Talane","position":"Research Associate","experience":7,"country":"China","phrase":"Multi-channelled coherent leverage","salary":180025}
{ "index" : { "_id" : 2 } }
{ "name":"Othilia Cathel","email":"ocathel1@senate.gov","gender":"female","ip_address":"3.164.153.228","date_of_birth":"22/07/1987","company":"Edgepulse","position":"Structural Engineer","experience":11,"country":"China","phrase":"Grass-roots heuristic help-desk","salary":193530}
{ "index" : { "_id" : 3 } }
{"name":"Winston Waren","email":"wwaren2@4shared.com","gender":"male","ip_address":"202.37.210.94","date_of_birth":"10/11/1985","company":"Yozio","position":"Human Resources Manager","experience":12,"country":"China","phrase":"Versatile object-oriented emulation","salary":50616}
{ "index" : { "_id" : 4 } }
{"name" : "Alan Thomas","email" : "athomas2@example.com","gender" : "male","ip_address" : "200.47.210.95","date_of_birth" : "11/12/1985","company" : "Yamaha","position" : "Resources Manager","experience" : 12,"country" : "China","phrase" : "Emulation of roots heuristic coherent systems","salary" : 300000}
Query
Select All
GET employees/_search?filter_path=hits.hits
{
"query": {
"match_all": {}
}
}
Select One
GET employees/_doc/1
Select Match
Select fulltext
POST employees/_search?filter_path=hits.hits
{
"query": {
"match": {
"phrase": {
"query" : "heuristic"
}
}
}
}
POST employees/_search?filter_path=hits.hits
{
"query": {
"match": {
"phrase": "heuristic"
}
}
}
Select By phrase
GET employees/_search?filter_path=hits.hits
{
"query": {
"match": {
"phrase": {
"query" : "heuristic roots help"
}
}
}
}
Select Phrase and
GET employees/_search?filter_path=hits.hits
{
"_source": ["name", "email"],
"query": {
"match": {
"phrase": {
"query" : "heuristic roots help",
"operator" : "AND"
}
}
}
}
Select Phrase minimum
POST employees/_search?filter_path=hits.hits
{
"query": {
"match": {
"phrase": {
"query" : "heuristic roots help",
"minimum_should_match": 3
}
}
}
}
Select multi_match
GET employees/_search?filter_path=hits.hits
{
"query": {
"multi_match": {
"query" : "research help",
"fields": ["position", "phrase"]
}
}
}
Select Match Phrase
GET employees/_search?filter_path=hits.hits
{
"query": {
"match_phrase": {
"phrase": {
"query": "roots heuristic coherent"
}
}
}
}
Select Match slop 1
GET employees/_search
{
"query": {
"match_phrase": {
"phrase": {
"query": "roots coherent",
"slop": 1
}
}
}
}
Add document
PUT employees/_doc/5
{
"name": "Jennifer Lawrence",
"email": "jlaw@example.com",
"gender": "female",
"ip_address": "100.37.110.59",
"date_of_birth": "17/05/1995",
"company": "Monsnto",
"position": "Resources Manager",
"experience": 10,
"country": "Germany",
"phrase": "Emulation of roots heuristic complete systems",
"salary": 300000
}
Select by Prefix
GET employees/_search
{
"_source": [ "phrase" ],
"query": {
"match_phrase_prefix": {
"phrase": {
"query": "roots heuristic co"
}
}
}
}
Select by Term
Select Term
GET employees/_search
{
"query": {
"term": {
"gender": "male"
}
}
}
Select Term by case sensitive
GET employees/_search
{
"query": {
"term": {
"gender": "Male"
}
}
}
Select by Terms
GET employees/_search
{
"query": {
"terms": {
"gender": ["male", "female"]
}
}
}
Insert document
PUT employees/_doc/5
{
"name": "Michael Bordon",
"email": "mbordon@example.com",
"gender": "male",
"ip_address": "10.47.210.65",
"date_of_birth": "12/12/1995",
"position": "Resources Manager",
"experience": 12,
"country": null,
"phrase": "Emulation of roots heuristic coherent systems",
"salary": 300000
}
Select Exists
GET employees/_search
{
"query": {
"exists": {
"field": "company"
}
}
}
Select Not Exists
GET employees/_search
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "company"
}
}
]
}
}
}
Select by Range
POST employees/_search
{
"query": {
"range" : {
"experience" : {
"gte" : 5,
"lte" : 10
}
}
}
}
Select Greater Than or Equal
GET employees/_search
{
"query": {
"range" : {
"date_of_birth" : {
"gte" : "01/01/1986"
}
}
}
}
Select id In
POST employees/_search
{
"query": {
"ids" : {
"values" : ["1", "4"]
}
}
}
Select by Prefix
GET employees/_search
{
"query": {
"prefix": {
"name": "al"
}
}
}
Select by wildcard
GET employees/_search
{
"query": {
"wildcard": {
"country": {
"value": "c*a"
}
}
}
}
Select by Regular Expression
GET employees/_search
{
"query": {
"regexp": {
"position": "res[a-z]*"
}
}
}
Select fuzziness
GET employees/_search
{
"query": {
"fuzzy": {
"country": {
"value": "Chnia",
"fuzziness": "2"
}
}
}
}
Select must(and)
GET employees/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"position": "manager"
}
},
{
"range": {
"experience": {
"gte": 12
}
}
}
]
}
}
}
Select Much vs Should
GET employees/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"position": "manager"
}
},
{
"range": {
"experience": {
"gte": 12
}
}
}
],
"should": [
{
"match": {
"phrase": "versatile"
}
}
]
}
}
}
Select (company = 'Talane' or company = 'Yamaha') and (position = 'manager' or position = 'Associate')
GET employees/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [{
"match": {
"company": "Talane"
}
}, {
"match": {
"company": "Yamaha"
}
}]
}
},
{
"bool": {
"should": [
{
"match": {
"position": "manager"
}
}, {
"match": {
"position": "Associate"
}
}
]
}
}, {
"bool": {
"must": [
{
"range": {
"salary": {
"gte": 100000
}
}
}
]
}
}]
}
}
}