Skip to content

Instantly share code, notes, and snippets.

@polyfractal
Last active February 21, 2022 17:28
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save polyfractal/bce8e9ce47a96a24e693 to your computer and use it in GitHub Desktop.
Save polyfractal/bce8e9ce47a96a24e693 to your computer and use it in GitHub Desktop.
ElasticSearch Mapping
{
"mappings":{
"item":{
"properties":{
"productName":{
"fields":{
"partial":{
"search_analyzer":"full_name",
"index_analyzer":"partial_name",
"type":"string"
},
"partial_back":{
"search_analyzer":"full_name",
"index_analyzer":"partial_name_back",
"type":"string"
},
"partial_middle":{
"search_analyzer":"full_name",
"index_analyzer":"partial_middle_name",
"type":"string"
},
"productName":{
"type":"string",
"analyzer":"full_name"
}
},
"type":"multi_field"
},
"productID":{
"type":"string",
"analyzer":"simple"
},
"warehouse":{
"type":"string",
"analyzer":"simple"
},
"vendor":{
"type":"string",
"analyzer":"simple"
},
"productDescription":{
"type":"string",
"analyzer":"full_name"
},
"categories":{
"type":"string",
"analyzer":"simple"
},
"stockLevel":{
"type":"integer",
"index":"not_analyzed"
},
"cost":{
"type":"float",
"index":"not_analyzed"
}
}
}
},
"settings":{
"analysis":{
"filter":{
"name_ngrams":{
"side":"front",
"max_gram":50,
"min_gram":2,
"type":"edgeNGram"
},
"name_ngrams_back":{
"side":"back",
"max_gram":50,
"min_gram":2,
"type":"edgeNGram"
},
"name_middle_ngrams":{
"type":"nGram",
"max_gram":50,
"min_gram":2
}
},
"analyzer":{
"full_name":{
"filter":[
"standard",
"lowercase",
"asciifolding"
],
"type":"custom",
"tokenizer":"standard"
},
"partial_name":{
"filter":[
"standard",
"lowercase",
"asciifolding",
"name_ngrams"
],
"type":"custom",
"tokenizer":"standard"
},
"partial_name_back":{
"filter":[
"standard",
"lowercase",
"asciifolding",
"name_ngrams_back"
],
"type":"custom",
"tokenizer":"standard"
},
"partial_middle_name":{
"filter":[
"standard",
"lowercase",
"asciifolding",
"name_middle_ngrams"
],
"type":"custom",
"tokenizer":"standard"
}
}
}
}
}
/*-----------------Example query:----------------------*/
{
"size":20,
"from":0,
"sort":[
"_score"
],
"query":{
"filtered":{
"query":{
"bool":{
"should":[
{
"text":{
"productName":{
"boost":5,
"query":"test query",
"type":"phrase"
}
}
},
{
"text":{
"productName.partial":{
"boost":1,
"query":"test query"
}
}
},
{
"text":{
"productName.partial_middle":{
"boost":1,
"query":"test query"
}
}
},
{
"text":{
"productName.partial_back":{
"boost":1,
"query":"test query"
}
}
}
]
}
},
"filter":{
"bool":{
"must":[
{
"terms":{
"warehouse":[
"international",
"usa",
"aus",
"de"
]
}
},
{
"range":{
"stockLevel":{
"gt":"0"
}
}
}
]
}
}
}
}
}
Copy link

ghost commented Jun 15, 2013

this is just what I've been looking for...there's such a dearth of useable examples for indexes, analyzers and mapping. nice job!

@freshteapot
Copy link

How do I use this mapping?

Do I save it to config/mappings?

Dazed and confused.

@reiz
Copy link

reiz commented Nov 11, 2013

Thanks a lot. This helped me to improve my search.

@mirhmousavi
Copy link

thanks for sharing
for others: the text is deprecated use match instead

@rabb-bit
Copy link

starting ES 2.0 index_analyzer should be changed to "analyzer"

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