Skip to content

Instantly share code, notes, and snippets.

@wshirey
Last active August 17, 2017 21:49
Show Gist options
  • Save wshirey/cd1f3faa4505a50b8335ea9b84582c99 to your computer and use it in GitHub Desktop.
Save wshirey/cd1f3faa4505a50b8335ea9b84582c99 to your computer and use it in GitHub Desktop.
elasticsearch ngram analyzer with multifields
DELETE my_index
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"filter": ["english_stop", "lowercase"],
"tokenizer": "my_tokenizer"
}
},
"filter": {
"english_stop": {
"type": "stop",
"stopwords": [
"_english_"
]
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 3,
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"text": {
"type": "text",
"fields": {
"english": {
"type": "text",
"analyzer": "english"
},
"ngrams": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
}
}
POST my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "The Quick Brown Foxes"
}
PUT my_index/my_type/1
{ "text": "quick brown fox" }
PUT my_index/my_type/2
{ "text": "quick brown foxes" }
PUT my_index/my_type/3
{ "text": "some new child care program" }
PUT my_index/my_type/4
{ "text": "some other childcare program" }
PUT my_index/my_type/5
{ "text": "some other child_care program" }
PUT my_index/my_type/
GET my_index/_search
{
"query": {
"multi_match": {
"query": "child care",
"fields": [
"text",
"text.english",
"text.ngrams"
],
"type": "most_fields",
"analyzer": "standard"
}
}
}
@wshirey
Copy link
Author

wshirey commented Aug 9, 2017

try searching for terms

  • childcare
  • child care
  • child_care

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment