Skip to content

Instantly share code, notes, and snippets.

@orphaner
Forked from dadoonet/search_kibana_console.txt
Created December 15, 2016 18:04
Show Gist options
  • Select an option

  • Save orphaner/2ca0f81b94fd05d180ed1328f5bab0ca to your computer and use it in GitHub Desktop.

Select an option

Save orphaner/2ca0f81b94fd05d180ed1328f5bab0ca to your computer and use it in GitHub Desktop.
Paris JUG 13/09/2016 - Elasticsearch demo
### REINIT
DELETE oss
PUT oss
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"user": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text"
}
}
}
}
}
POST oss/user
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST oss/user
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST oss/user
{
"name": "David Gageot",
"comments": "Engineer at Docker"
}
POST oss/user
{
"name": "David David",
"comments": "Who is that guy?"
}
### SEARCH
# Why the 1st doc?
GET oss/_search
{
"query": {
"match": {
"name": "David"
}
}
}
# Why the 1st doc?
GET oss/_search
{
"query": {
"multi_match" : {
"query": "David",
"fields": [ "name", "comments" ]
}
}
}
# Name is more important for us (positive boost)
GET oss/_search
{
"query": {
"multi_match" : {
"query": "David",
"fields": [ "name^3", "comments" ]
}
}
}
# Name is more important for us (negative boost)
GET oss/_search
{
"query": {
"multi_match" : {
"query": "David",
"fields": [ "name", "comments^0.3" ]
}
}
}
# Typos
GET oss/_search
{
"query": {
"multi_match" : {
"query": "Dadid",
"fields": [ "name", "comments^0.3" ]
}
}
}
GET oss/_search
{
"query": {
"multi_match" : {
"query": "Dadid",
"fields": [ "name", "comments^0.3" ],
"fuzziness": 1
}
}
}
GET oss/_search
{
"query": {
"multi_match" : {
"query": "Dadod",
"fields": [ "name", "comments^0.3" ],
"fuzziness": 1
}
}
}
GET oss/_search
{
"query": {
"multi_match" : {
"query": "dadod",
"fields": [ "name", "comments^0.3" ],
"fuzziness": 2
}
}
}
# Strange result?
GET oss/_search
{
"query": {
"match": {
"comments": "at elastic"
}
}
}
# Using a phrase
GET oss/_search
{
"query": {
"match_phrase": {
"comments": "at elastic"
}
}
}
## REINIT
DELETE oss
PUT oss
{
"settings": {
"number_of_shards": 1,
"analysis": {
"analyzer": {
"for_synonyms": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter"
]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms" : [
"engineer, developer => engineer"
]
}
}
}
},
"mappings": {
"user": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text",
"analyzer": "for_synonyms"
}
}
}
}
}
GET oss/_analyze?analyzer=for_synonyms&text=engineer
GET oss/_analyze?analyzer=for_synonyms&text=Developer
POST oss/user
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST oss/user
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST oss/user
{
"name": "David Gageot",
"comments": "Engineer at Docker"
}
POST oss/user
{
"name": "David David",
"comments": "Who is that guy?"
}
GET oss/_search
{
"query": {
"match": {
"comments": "developer"
}
}
}
GET oss/_search
{
"query": {
"match": {
"comments": "engineer"
}
}
}
### Find the best match first
DELETE oss
PUT oss
{
"settings": {
"number_of_shards": 1,
"analysis": {
"analyzer": {
"for_synonyms": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter"
]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms" : [
"engineer, developer => engineer"
]
}
}
}
},
"mappings": {
"user": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text",
"fields": {
"synonyms": {
"type": "text",
"analyzer": "for_synonyms"
}
}
}
}
}
}
}
POST oss/user
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST oss/user
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST oss/user
{
"name": "David Gageot",
"comments": "Engineer at Docker"
}
POST oss/user
{
"name": "David David",
"comments": "Who is that guy?"
}
# search for existing term in source
GET oss/_search
{
"query": {
"match": {
"comments": "engineer"
}
}
}
# search using synonyms
GET oss/_search
{
"query": {
"match": {
"comments.synonyms": "engineer"
}
}
}
# remember our fuzzy search
GET oss/_search
{
"query": {
"multi_match" : {
"query": "dockir",
"fields": [ "name", "comments^0.3" ],
"fuzziness": 2
}
}
}
# remember our phrase search
GET oss/_search
{
"query": {
"match_phrase": {
"comments": "at elastic"
}
}
}
## Let's combine all that
GET oss/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"name": {
"query" : "david",
"_name": "phrase on name",
"boost": 8.0
}
}
},
{
"match": {
"name": {
"query": "david",
"operator": "and",
"_name": "all terms on name",
"boost": 2.0
}
}
},
{
"match": {
"name": {
"query": "david",
"operator": "or",
"_name": "at least one term on name",
"boost": 1.8
}
}
},
{
"match": {
"name": {
"query": "david",
"fuzziness": 2,
"_name": "fuzzy on name",
"boost": 1.0
}
}
},
{
"match_phrase": {
"comments": {
"query" : "david",
"_name": "phrase on comments",
"boost": 0.9
}
}
},
{
"match": {
"comments": {
"query": "david",
"operator": "and",
"_name": "all terms on comments",
"boost": 0.8
}
}
},
{
"match": {
"comments": {
"query": "david",
"operator": "or",
"_name": "at least one term on comments",
"boost": 0.7
}
}
},
{
"match": {
"comments.synonyms": {
"query": "david",
"_name": "synonyms on comments",
"boost": 0.6
}
}
},
{
"match": {
"comments": {
"query": "david",
"fuzziness": 2,
"_name": "fuzzy on comments",
"boost": 0.2
}
}
}
]
}
}
}
GET oss/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"name": {
"query" : "david polito",
"_name": "phrase on name",
"boost": 8.0
}
}
},
{
"match": {
"name": {
"query": "david polito",
"operator": "and",
"_name": "all terms on name",
"boost": 2.0
}
}
},
{
"match": {
"name": {
"query": "david polito",
"operator": "or",
"_name": "at least one term on name",
"boost": 1.8
}
}
},
{
"match": {
"name": {
"query": "david polito",
"fuzziness": 2,
"_name": "fuzzy on name",
"boost": 1.0
}
}
},
{
"match_phrase": {
"comments": {
"query" : "david polito",
"_name": "phrase on comments",
"boost": 0.9
}
}
},
{
"match": {
"comments": {
"query": "david polito",
"operator": "and",
"_name": "all terms on comments",
"boost": 0.8
}
}
},
{
"match": {
"comments": {
"query": "david polito",
"operator": "or",
"_name": "at least one term on comments",
"boost": 0.7
}
}
},
{
"match": {
"comments.synonyms": {
"query": "david polito",
"_name": "synonyms on comments",
"boost": 0.6
}
}
},
{
"match": {
"comments": {
"query": "david polito",
"fuzziness": 2,
"_name": "fuzzy on comments",
"boost": 0.2
}
}
}
]
}
}
}
### Register a search template
POST /_search/template/oss
{
"template": {
"query": {
"bool": {
"should": [
{
"match_phrase": {
"name": {
"query": "{{query_string}}",
"_name": "phrase on name - {{query_string}}",
"boost": 8
}
}
},
{
"match": {
"name": {
"query": "{{query_string}}",
"operator": "and",
"_name": "all terms on name - {{query_string}}",
"boost": 2
}
}
},
{
"match": {
"name": {
"query": "{{query_string}}",
"operator": "or",
"_name": "at least one term on name - {{query_string}}",
"boost": 1.8
}
}
},
{
"match": {
"name": {
"query": "{{query_string}}",
"fuzziness": 2,
"_name": "fuzzy on name - {{query_string}}",
"boost": 1
}
}
},
{
"match_phrase": {
"comments": {
"query": "{{query_string}}",
"_name": "phrase on comments - {{query_string}}",
"boost": 0.9
}
}
},
{
"match": {
"comments": {
"query": "{{query_string}}",
"operator": "and",
"_name": "all terms on comments - {{query_string}}",
"boost": 0.8
}
}
},
{
"match": {
"comments": {
"query": "{{query_string}}",
"operator": "or",
"_name": "at least one term on comments - {{query_string}}",
"boost": 0.7
}
}
},
{
"match": {
"comments.synonyms": {
"query": "{{query_string}}",
"_name": "synonyms on comments - {{query_string}}",
"boost": 0.6
}
}
},
{
"match": {
"comments": {
"query": "{{query_string}}",
"fuzziness": 2,
"_name": "fuzzy on comments - {{query_string}}",
"boost": 0.2
}
}
}
]
}
}
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "david"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "david pilato"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "dovad polito"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "david polito"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "david engineer at docker"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "david developer at docker"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "at elastic"
}
}
GET /_search/template
{
"id": "oss",
"params": {
"query_string": "elastic customs david"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment