Skip to content

Instantly share code, notes, and snippets.

@ellerycrane
Created May 12, 2011 18:44
Show Gist options
  • Save ellerycrane/969178 to your computer and use it in GitHub Desktop.
Save ellerycrane/969178 to your computer and use it in GitHub Desktop.
elasticsearch dynamic templates issue
////////////////////////////////////////////////////////
// Create index and mapping without dynamic templates //
////////////////////////////////////////////////////////
curl -XDELETE 'http://localhost:9200/someindex'
curl -XPUT 'http://localhost:9200/someindex/?pretty=1' -d '
{
"settings" : {
"analysis" : {
"filter" : {
"edge_ngram" : {
"side" : "front",
"max_gram" : 20,
"min_gram" : 3,
"type" : "edgeNGram"
}
},
"analyzer" : {
"ascii_std" : {
"filter" : [
"standard",
"lowercase",
"asciifolding"
],
"type" : "custom",
"tokenizer" : "standard"
},
"ascii_edge_ngram" : {
"filter" : [
"standard",
"lowercase",
"asciifolding",
"edge_ngram"
],
"type" : "custom",
"tokenizer" : "standard"
}
}
}
}
}
'
curl -XPUT 'http://localhost:9200/someindex/contact/_mapping?pretty=1' -d '
{
"contact" : {
"properties" : {
"name" : {
"properties" : {
"first" : {
"type" : "string",
"include_in_all" : 0,
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std"
},
"last" : {
"type" : "string",
"include_in_all" : 0,
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std"
}
}
}
}
}
}
'
curl -XPOST 'http://localhost:9200/someindex/_refresh'
curl -XPOST 'http://localhost:9200/someindex/contact' -d '
{
"name" : {
"first": "Charles",
"last": "Xavier"
}
}
'
curl -XPOST 'http://localhost:9200/someindex/_refresh'
curl -XGET 'http://localhost:9200/someindex/contact/_mapping?pretty=1'
echo Note the mapping here, to compare with dynamic template mapping later
curl -XGET 'http://localhost:9200/someindex/contact/_search?pretty=1' -d '
{
"explain":true,
"query" : {
"query_string" : {
"query" : "cha",
"fields": ["name.first"]
}
}
}
'
echo Our document is matched, as we expect
///////////////////////////////////////////////////
// Now try the same thing with dynamic templates //
///////////////////////////////////////////////////
curl -XDELETE 'http://localhost:9200/someindex'
curl -XPUT 'http://localhost:9200/someindex/?pretty=1' -d '
{
"settings" : {
"analysis" : {
"filter" : {
"edge_ngram" : {
"side" : "front",
"max_gram" : 20,
"min_gram" : 3,
"type" : "edgeNGram"
}
},
"analyzer" : {
"ascii_std" : {
"filter" : [
"standard",
"lowercase",
"asciifolding"
],
"type" : "custom",
"tokenizer" : "standard"
},
"ascii_edge_ngram" : {
"filter" : [
"standard",
"lowercase",
"asciifolding",
"edge_ngram"
],
"type" : "custom",
"tokenizer" : "standard"
}
}
}
}
}
'
curl -XPUT 'http://localhost:9200/someindex/contact/_mapping?pretty=1' -d '
{
"contact": {
"dynamic_templates": [
{
"nameTemplate": {
"match": "name",
"mapping": {
"properties" : {
"first" : {
"type" : "string",
"include_in_all" : 0,
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std"
},
"last" : {
"type" : "string",
"include_in_all" : 0,
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std"
}
}
}
}
}
]
}
}
'
curl -XPOST 'http://localhost:9200/someindex/_refresh'
curl -XPOST 'http://localhost:9200/someindex/contact' -d '
{
"name" : {
"first": "Charles",
"last": "Xavier"
}
}
'
curl -XPOST 'http://localhost:9200/someindex/_refresh'
curl -XGET 'http://localhost:9200/someindex/contact/_mapping?pretty=1'
echo The mapping is identical to the previous one, except for the presence of the dynamic_templates property
curl -XGET 'http://localhost:9200/someindex/contact/_search?pretty=1' -d '
{
"explain":true,
"query" : {
"query_string" : {
"query" : "cha",
"fields": ["name.first"]
}
}
}
'
echo The document does NOT match despite the identical mapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment