Skip to content

Instantly share code, notes, and snippets.

@avar
Created March 17, 2012 13:41
Show Gist options
  • Save avar/2059195 to your computer and use it in GitHub Desktop.
Save avar/2059195 to your computer and use it in GitHub Desktop.
ElasticSearch Exact Name matching
export http_proxy=
export https_proxy=
curl -XDELETE 'http://localhost:9200/test/'
echo "Creating the mapping"
curl -XPUT 'http://localhost:9200/test/?pretty=1' -d '
{
"mappings" : {
"member" : {
"properties" : {
"object_name" : {
"fields" : {
"unmunged" : {
"search_analyzer" : "name_unmunged",
"index_analyzer" : "name_unmunged",
"type" : "string"
},
"standard" : {
"search_analyzer" : "standard",
"index_analyzer" : "standard",
"type" : "string"
}
},
"type" : "multi_field"
},
"object_name_other_language" : {
"fields" : {
"unmunged" : {
"search_analyzer" : "name_unmunged",
"index_analyzer" : "name_unmunged",
"type" : "string"
},
"standard" : {
"search_analyzer" : "standard",
"index_analyzer" : "standard",
"type" : "string"
}
},
"type" : "multi_field"
}
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"name_unmunged" : {
"filter" : [
"lowercase"
],
"type" : "custom",
"tokenizer" : "standard"
}
}
},
"index": {
"number_of_shards": 1
}
}
}
'; echo
echo "Indexing"
curl -XPOST 'http://localhost:9200/_bulk?pretty=1' -d '
{"index" : {"_index" : "test", "_type" : "member"}}
{"object_name" : "Albino Elephant", "COMMENT_object_name_other_language" : "Albino Elephant"}
{"index" : {"_index" : "test", "_type" : "member"}}
{"object_name" : "Cute Albino Elephant", "object_name_other_language" : "Cute Albino Elephant"}
{"index" : {"_index" : "test", "_type" : "member"}}
{"object_name" : "The Cutest Albino Elephant", "object_name_other_language" : "The Cutest Albino Elephant"}
'; echo
# Wait for indexing
echo "Refreshing"
curl -XPOST 'http://localhost:9200/_refresh?pretty=yes'; echo
echo "Doing the search"
curl -XGET 'http://localhost:9200/test/member/_search?pretty=1' -d '
{
"explain" : "false",
"query" : {
"dis_max" : {
"queries" : [
{
"text" : {
"object_name.unmunged" : "Albino Elephant"
}
},
{
"text" : {
"object_name_other_language.unmunged" : "Albino Elephant"
}
}
]
}
}
}
'; echo
# Results with the COMMENT_ in place for document #1:
# "hits" : [ {
# "_index" : "test",
# "_type" : "member",
# "_id" : "GKZDxtHpSKaksQH4reX89g",
# "_score" : 0.2169777, "_source" : {"object_name" : "The Cutest Albino Elephant", "object_name_other_language" : "The Cutest Albino Elephant"}
# }, {
# "_index" : "test",
# "_type" : "member",
# "_id" : "DVbb0HMeSdSkJajpwZ8PjA",
# "_score" : 0.2169777, "_source" : {"object_name" : "Cute Albino Elephant", "object_name_other_language" : "Cute Albino Elephant"}
# }, {
# "_index" : "test",
# "_type" : "member",
# "_id" : "oZxMt0PIQU2f5s7I7jo7fg",
# "_score" : 0.08322528, "_source" : {"object_name" : "Albino Elephant", "COMMENT_object_name_other_language" : "Albino Elephant"}
# } ]
# }
# Results without it:
# "hits" : [ {
# "_index" : "test",
# "_type" : "member",
# "_id" : "QZwB23TgRY-N8hq7iZ43vQ",
# "_score" : 0.2712221, "_source" : {"object_name" : "Albino Elephant", "object_name_other_language" : "Albino Elephant"}
# }, {
# "_index" : "test",
# "_type" : "member",
# "_id" : "cMeLRXlZShyoZsYVI_BQsQ",
# "_score" : 0.2169777, "_source" : {"object_name" : "Cute Albino Elephant", "object_name_other_language" : "Cute Albino Elephant"}
# }, {
# "_index" : "test",
# "_type" : "member",
# "_id" : "eC1j163mTLeoltIjt7SXkg",
# "_score" : 0.2169777, "_source" : {"object_name" : "The Cutest Albino Elephant", "object_name_other_language" : "The Cutest Albino Elephant"}
# } ]
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment