Skip to content

Instantly share code, notes, and snippets.

@mauricioalarcon
Created August 28, 2012 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioalarcon/3498562 to your computer and use it in GitHub Desktop.
Save mauricioalarcon/3498562 to your computer and use it in GitHub Desktop.
working version of multi_field facet
# Delete the index
curl -XDELETE localhost:9200/facettests/facettest/
# create it
curl -XPUT localhost:9200/facettests/
# set mapping (multi field for facets)
# NOTE THAT THIS HAS KEYWORD ANALYZER OVER NAME, and not nameFacet
# this DOES work
curl -XPUT localhost:9200/facettests/facettest/_mapping -d '{
"facettest" : {
"properties" : {
"employmentHistory" : {
"properties" : {
"name" : {
"type" : "multi_field",
"fields" : {
"name" : {"type" : "string", "index" : "analyzed", "analyzer" : "keyword"},
"nameFacet" : { "type" : "string", "index" : "analyzed" }
}
}
}
}
}
}
}'
# Insert a couple of test
curl -XPOST localhost:9200/facettests/facettest/1 -d '{ "id" : 1,
"name" : "Bob",
"lastName" : "Marley",
"city" : "Boston",
"country" : "United States",
"education" : [ {
"degreeType" : "doctorate",
"major" : "Mathematics",
"schoolName" : "Cornell University"
} ],
"employmentHistory" : [ { "category" : "Senior (more than 5 years experience)",
"name" : "A.M. (honorary), Harvard University",
"title" : "Assistant and Associate Professor, Cornell University"
},
{ "category" : "Senior (more than 5 years experience)",
"name" : "Massachusetts Institute of Technology",
"title" : "Professor"
},
{ "category" : "Senior (more than 5 years experience)",
"name" : "Harvard University",
"title" : "Professor"
},
{ "category" : "Senior (more than 5 years experience)",
"name" : "California Institute of Technology",
"title" : "Fellow"
},
{ "category" : "Senior (more than 5 years experience)",
"name" : "University of Chicago",
"title" : "Visiting Professor"
},
{ "category" : "Senior (more than 5 years experience)",
"name" : "Nate Ackerman, M.I.T. Ph.D",
"title" : "1"
},
{ "category" : "Senior (more than 5 years experience)",
"name" : "Alice Chan, M.I.T",
"title" : "student"
},
{ "category" : "Experienced (non-manager)",
"name" : "Congress of Mathematicians",
"title" : "2"
},
{ "category" : "Experienced (non-manager)",
"name" : "AMS Summer Institute in Recursion Theory",
"title" : "Member"
},
{ "category" : "Senior Executive (President, C-level)",
"name" : "Association for Symbolic Logic",
"title" : "Chairman, awards and prizes"
}
]
}'
curl -XPOST localhost:9200/facettests/facettest/2 -d '{ "id" : 2,
"name" : "Isaac",
"lastName" : "Smith",
"city" : "Lyons",
"country" : "United States",
"educationHistory" : [ {
"degreeType" : "masters",
"major" : "Teaching English as a Second Language",
"schoolName" : "University of Illinois"
},
{
"degreeType" : "bachelors",
"major" : "English",
"schoolName" : "University of Colorado"
}
],
"employmentHistory" : [ { "category" : "Manager",
"name" : "Pillar Data Systems",
"title" : "Senior Technical Writer"
},
{ "category" : "Manager",
"name" : "Monitoring (BAM)",
"title" : "Senior Technical Writer"
},
{ "category" : "Manager",
"name" : "HP Photosmart",
"title" : "Senior Technical Writer"
},
{ "category" : "Experienced (non-manager)",
"name" : "Utility Data Center",
"title" : "Write system administrator documentation for Utility Data Center"
},
{ "category" : "Entry Level",
"name" : "Research Systems",
"title" : null
},
{ "category" : "Entry Level",
"name" : "Univ. of Colorado at Denver",
"title" : "Adjunct Instructor"
}
]
}'
# search and count to be sure they're there
curl -XGET localhost:9200/facettests/facettest/_search?pretty=true
curl -XGET localhost:9200/facettests/facettest/_count?pretty=true
# search using facet over "name" this DOES work fine, not like https://gist.github.com/3498390
# where the keyword analyzer is over nameFacet
curl -XGET localhost:9200/facettests/facettest/_search?pretty=true -d '{
"fields": [
"_id",
"employmentHistory.name"
],
"query" : {
"match_all" : {}
},
"facets" : {
"nameFacet" : { "terms" : { "field" : "employmentHistory.name"}}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment