Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ISBN search tricks with Elasticsearch multi_field and index_name
curl -XDELETE 'localhost:9200/isbn/'
curl -XPUT 'localhost:9200/isbn/' -d '
{
"mappings" : {
"_default_" : {
"_source" : {
"enabled" : true
},
"_all" : {
"analyzer" : "default",
"enabled" : true
},
"properties" : {
"identifier" : {
"dynamic" : false,
"properties" : {
"isbn" : {
"type" : "string",
"include_in_all" : true
},
"ean" : {
"type" : "multi_field",
"fields" : {
"ean" : {
"index_name" : "isbn",
"type" : "string"
},
"eanonly" : {
"type" : "string"
}
}
},
"isbnprintable" : {
"index_name" : "isbn",
"index" : "not_analyzed",
"type" : "string"
}
}
}
}
}
}
}
'
# sample document
curl -XPUT 'localhost:9200/isbn/demo/1' -d '
{
"identifier" : {
"isbn" : "1932394281",
"ean" : "9781932394283",
"isbnprintable" : "1-932394-28-1"
}
}
'
sleep 1
# hit
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"_all" : "1932394281"
}
}
}
'
# hit
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"_all" : "9781932394283"
}
}
}
'
# hit
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"identifier.isbn" : "1932394281"
}
}
}
'
# hit
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"identifier.isbn" : "9781932394283"
}
}
}
'
# hit :-(
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"identifier.ean" : "1932394281"
}
}
}
'
# hit
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"identifier.ean" : "9781932394283"
}
}
}
'
# hit
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"identifier.ean.eanonly" : "9781932394283"
}
}
}
'
# no hit :-)
curl -XGET 'localhost:9200/isbn/demo/_search' -d '
{
"query" : {
"text" : {
"identifier.ean.eanonly" : "1932394281"
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment