Skip to content

Instantly share code, notes, and snippets.

@sdelrio0
Created May 5, 2017 14:47
Show Gist options
  • Save sdelrio0/acdee7a3cf39c1bef98c59237e6b86f9 to your computer and use it in GitHub Desktop.
Save sdelrio0/acdee7a3cf39c1bef98c59237e6b86f9 to your computer and use it in GitHub Desktop.
Sample code to run in kibana console to understand the basics of Elasticsearch 5
# Check cluster health
GET /_cat/health?v
# Get indicies
GET /_cat/indices?v
# ================ ANALYSIS ================
# Whitespace tokenizer
GET _analyze
{
"tokenizer": {
"type": "standard"
},
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
# Standard tokenizer
GET _analyze
{
"tokenizer": {
"type": "standard"
},
"text": "The 2 QUICK / Brown-Foxes jumped over the lazy dog's bone."
}
# Whitespace tokenizer + lowercase filter
GET _analyze
{
"tokenizer": {
"type": "whitespace"
},
"filter": ["lowercase"],
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
# Email: whitespace
GET _analyze
{
"tokenizer": {
"type": "whitespace"
},
"text": "My awesome email is: SOME.awesome_mail@my-company.com"
}
# Email: standard
GET _analyze
{
"tokenizer": {
"type": "standard"
},
"text": "My awesome email is: SOME.awesome_mail@my-company.com"
}
# Email: UAX URL Email
GET _analyze
{
"tokenizer": {
"type": "uax_url_email"
},
"text": "My awesome http://www.google.com email is: SOME.awesome_mail@my-company.com"
}
# Email: UAX URL Email
GET _analyze
{
"tokenizer": {
"type": "uax_url_email"
},
"filter": ["lowercase"],
"text": "My awesome email is: SOME.awesome_mail@my-company.com"
}
# Ngram tokenizer
GET _analyze
{
"tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 15
},
"filter": ["lowercase"],
"text": "Hello world!"
}
# Edge-Ngram tokenizer
GET _analyze
{
"tokenizer": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 15
},
"filter": ["lowercase"],
"text": "Hello world!"
}
# ================ SETTINGS & MAPPINGS ================
# Set up mappings, analizers and tokenizers
PUT jobs
{
"settings": {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "ngram_tokenizer",
"filter": [
"lowercase"
]
}
},
"tokenizer": {
"ngram_tokenizer": {
"type": "ngram",
"min_gram": "3",
"max_gram": "15"
}
}
}
},
"mappings": {
"job": {
"dynamic": true,
"properties": {
"title": {
"analyzer": "ngram_analyzer",
"fields": {
"sortable": {
"type": "keyword"
}
},
"type": "string"
},
"team": {
"type": "object",
"properties": {
"name": {
"type": "text",
"analyzer": "ngram_analyzer",
"fields": {
"sortable": {
"type": "keyword"
}
}
}
}
},
"active": {
"type": "boolean",
"fields": {
"sortable": {
"type": "boolean"
}
}
}
}
}
}
}
# Get mappings
GET jobs/_mapping
# Inspect analyzers used in our mappings
GET jobs/_analyze
{
"analyzer": "ngram_analyzer",
"text": "Master of the Universe"
}
# Delete index
DELETE jobs
# Input seed data
POST jobs/job/_bulk
{"index": {}}
{"title": "Carpenter", "active": true, "team": {"name": "Handcrafts"}}
{"index": {}}
{"title": "Builder", "active": false, "team": {"name": "Handcrafts"}}
{"index": {}}
{"title": "Artist", "active": true, "team": {"name": "Handcrafts"}}
{"index": {}}
{"title": "Banker", "active": true, "team": {"name": "Corporate"}}
{"index": {}}
{"title": "Cargo logistics", "active": true, "team": {"name": "Corporate"}}
{"index": {}}
{"title": "Flower eater", "active": false, "team": {"name": "Hippie"}}
{"index": {}}
{"title": "Construction worker", "active": true, "team": {"name": "Workers"}}
{"index": {}}
{"title": "Social worker", "active": true, "team": {"name": "Workers"}}
{"index": {}}
{"title": "Terrible worker (New York) And Job with a very long long name that I don't know what to do", "active": false, "team": {"name": "Singer"}}
{"index": {}}
{"title": "SF - Position 1", "active": true, "team": {"name": "San Francisco"}}
{"index": {}}
{"title": "SF - Position 2", "active": true, "team": {"name": "San Francisco"}}
{"index": {}}
{"title": "SF - Position 3", "active": true, "team": {"name": "San Francisco"}}
{"index": {}}
{"title": "NY - Position 1", "active": true, "team": {"name": "New York"}}
{"index": {}}
{"title": "NY - Position 2", "active": true, "team": {"name": "New York"}}
{"index": {}}
{"title": "NY - Position 3", "active": true, "team": {"name": "New York"}}
# ================ SEARCH ! ================
# Get all
GET jobs/_search
{
"query": {
"match_all": {}
}
}
# Get x amount of results
GET jobs/_search
{
"size": 3,
"query": {
"bool": {
"must": {
"match_all": {}
}
}
}
}
# Multi match
GET jobs/_search
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "posi",
"fields": [
"title",
"team.name"
]
}
}
}
}
}
# Fuzzy
GET jobs/_search
{
"query": {
"bool": {
"must": {
"fuzzy": {
"title": "wokrer"
}
}
}
}
}
# Highlight results
GET jobs/_search
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "worker",
"fields": [
"title",
"team.name"
]
}
}
}
},
"highlight": {
"number_of_fragments": 0,
"fields": {
"*": { "no_match_size": 100 }
}
}
}
# Sorting
GET jobs/_search
{
"sort": [
{ "title.sortable": "asc" }
],
"query": {
"bool": {
"must": {
"match_all": {}
}
}
}
}
# Other...
# aggregations
# machine learning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment