Skip to content

Instantly share code, notes, and snippets.

@kid-cavaquinho
Created March 20, 2023 08:54
Show Gist options
  • Save kid-cavaquinho/c5099781136a425aa3e43ce570bd8c7a to your computer and use it in GitHub Desktop.
Save kid-cavaquinho/c5099781136a425aa3e43ce570bd8c7a to your computer and use it in GitHub Desktop.
elasticsearch normalizer and analyzers
PUT index-test
{
"settings": {
"analysis": {
"normalizer": {
"my_normalizer": {
"type": "custom",
"char_filter": [],
"filter": ["lowercase", "asciifolding"]
}
}
}
},
"mappings": {
"properties": {
"foo": {
"type": "keyword",
"normalizer": "my_normalizer"
}
}
}
}
PUT /test_index
{
"settings": {
"index": {
"analysis": {
"filter": {
"my_graph_synonyms": {
"type": "synonym_graph",
"synonyms": [
"baaar, bar"
]
}
},
"analyzer": {
"custom_index_time_analyzer": {
"tokenizer": "keyword",
"filter": [
"lowercase",
"stemmer",
"asciifolding"
]
},
"search_index_time_analyzer": {
"tokenizer": "keyword",
"filter": [
"lowercase",
"stemmer",
"asciifolding",
"my_graph_synonyms"
]
}
}
}
}
},
"mappings": {
"properties": {
"foo": {
"type": "text",
"analyzer": "custom_index_time_analyzer",
"search_analyzer": "search_index_time_analyzer"
}
}
}
}
POST test_index/_bulk
{"index":{"_id":"1"}}
{"foo":"bar"}
{"index":{"_id":"2"}}
{"foo":"Bar"}
{"index":{"_id":"3"}}
{"foo":"zas"}
{"index":{"_id":"4"}}
{"foo":"pas"}
{"index":{"_id":"5"}}
{"foo":"tas"}
{"index":{"_id":"6"}}
{"foo":"barry"}
{"index":{"_id":"7"}}
{"foo":"bãr"}
POST test_index/_refresh
# outputs 2 results
GET test_index/_search
{
"query": {
"term": {
"foo": "bar"
}
}
}
#outputs 2 results using synonym graph
GET test_index/_search
{
"query": {
"match": {
"foo": "baaar"
}
}
}
#outputs 2 results
GET test_index/_search
{
"query": {
"match_phrase": {
"foo": {
"query": "bar"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment