You know, for search
# Test whether ES started successfully
GET ?pretty
# Count the number of documents in the cluster
GET /_count?pretty
{
"query": {
"match_all": {}
}
}
# Create index
# If there is data, append data, if there is no index, create by default
# index name/type name/id
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : ["sports", "music"]
}
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : ["music"]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : ["forestry"]
}
# Retrieve documents
# _source attribute is the original json document
GET /megacorp/employee/1
GET /megacorp/employee/2
GET /megacorp/employee/3
# Update document
# Just PUT again.
# Note, complete coverage update, including reducing fields
PUT /megacorp/employee/3
{
"interests" : ["forestry", "programming1"]
}
# Delete document
DELETE /megacorp/employee/3
# Check if the document exists
HEAD /megacorp/employee/3
# Search all employee information
# The returned result is in hits
GET /megacorp/employee/_search
# Use query-string for lightweight search
GET /megacorp/employee/_search?q=last_name:Smith
# Use query expression search
# Query type match
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "Smith"
}
}
}
# Use more complex search
# Query Smith employees who are older than 30 years old
GET /megacorp/employee/_search
{
"query": {
"bool": {
"must": {
"match" : {
"last_name" : "Smith"
}
} ,
"filter": {
"range": {
"age": {
"gt": 30
}
}
}
}
}
}
# Full-text search
# match based on word search
GET /megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
}
}
# match_phrase based on phrase search
GET /megacorp/employee/_search
{
"query": {
"match_phrase": {
"about": "rock climbing"
}
}
}
#Highlight search
GET /megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
},
"highlight": {
"fields": {
"about": {}
}
}
}
#Analysis
# Most popular hobbies
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}
# Smith's most popular hobby
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "Smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}
# Summary
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}