Skip to content

Instantly share code, notes, and snippets.

@dadoonet
Last active December 10, 2018 13:42
Show Gist options
  • Save dadoonet/1a599c3e68420aa95ade73fe58b9e0a1 to your computer and use it in GitHub Desktop.
Save dadoonet/1a599c3e68420aa95ade73fe58b9e0a1 to your computer and use it in GitHub Desktop.
Jussieu workshop
DELETE user
PUT user
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text"
}
}
}
}
}
POST user/_doc
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST user/_doc
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST user/_doc
{
"name": "David Gageot",
"comments": "Engineer at Google"
}
POST user/_doc
{
"name": "David David",
"comments": "Who is that guy?"
}
DELETE user
PUT user
{
"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": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text",
"analyzer": "for_synonyms"
}
}
}
}
}
POST user/_doc
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST user/_doc
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST user/_doc
{
"name": "David Gageot",
"comments": "Engineer at Google"
}
POST user/_doc
{
"name": "David David",
"comments": "Who is that guy?"
}
DELETE user
PUT user
{
"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": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text",
"fields": {
"synonyms": {
"type": "text",
"analyzer": "for_synonyms"
}
}
}
}
}
}
}
POST user/_doc
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST user/_doc
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST user/_doc
{
"name": "David Gageot",
"comments": "Engineer at Google"
}
POST user/_doc
{
"name": "David David",
"comments": "Who is that guy?"
}
### Control the relevancy (score)
DELETE user
PUT user
{
"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": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"comments": {
"type": "text",
"fields": {
"synonyms": {
"type": "text",
"analyzer": "for_synonyms"
}
}
}
}
}
}
}
POST user/_doc
{
"name": "David Pilato",
"comments": "Developer at elastic"
}
POST user/_doc
{
"name": "Malloum Laya",
"comments": "Developer, Worked with David at french customs service"
}
POST user/_doc
{
"name": "David Gageot",
"comments": "Engineer at Google"
}
POST user/_doc
{
"name": "David David",
"comments": "Who is that guy?"
}
# search without synonyms
GET user/_search
{
"query": {
"match": {
"comments": "engineer"
}
}
}
# search using synonyms
GET user/_search
{
"query": {
"match": {
"comments.synonyms": "engineer"
}
}
}
# remember our fuzzy search
GET user/_search
{
"query": {
"multi_match" : {
"query": "gougle",
"fields": [ "name", "comments^0.3" ],
"fuzziness": 2
}
}
}
# remember our phrase search
GET user/_search
{
"query": {
"match_phrase": {
"comments": "at elastic"
}
}
}
## Let's combine all that
GET user/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"name": {
"query" : "david pilato",
"_name": "phrase on name",
"boost": 8.0
}
}
},
{
"match": {
"name": {
"query": "david pilato",
"operator": "and",
"_name": "all terms on name",
"boost": 2.0
}
}
},
{
"match": {
"name": {
"query": "david pilato",
"operator": "or",
"_name": "at least one term on name",
"boost": 1.8
}
}
},
{
"match": {
"name": {
"query": "david pilato",
"fuzziness": 2,
"_name": "fuzzy on name",
"boost": 1.0
}
}
},
{
"match_phrase": {
"comments": {
"query" : "david pilato",
"_name": "phrase on comments",
"boost": 0.9
}
}
},
{
"match": {
"comments": {
"query": "david pilato",
"operator": "and",
"_name": "all terms on comments",
"boost": 0.8
}
}
},
{
"match": {
"comments": {
"query": "david pilato",
"operator": "or",
"_name": "at least one term on comments",
"boost": 0.7
}
}
},
{
"match": {
"comments.synonyms": {
"query": "david pilato",
"_name": "synonyms on comments",
"boost": 0.6
}
}
},
{
"match": {
"comments": {
"query": "david pilato",
"fuzziness": 2,
"_name": "fuzzy on comments",
"boost": 0.2
}
}
}
]
}
}
}
# Changing the user input can be hard...
GET user/_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 _scripts/user
{
"script": {
"lang": "mustache",
"source": {
"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": "user",
"params": {
"query_string": "david"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "david pilato"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "dovad polito"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "david polito"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "david engineer at google"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "david developer at google"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "at elastic"
}
}
GET /_search/template
{
"id": "user",
"params": {
"query_string": "elastic customs david"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment