Skip to content

Instantly share code, notes, and snippets.

@tovbinm
Last active December 10, 2015 23:08
Show Gist options
  • Save tovbinm/4507146 to your computer and use it in GitHub Desktop.
Save tovbinm/4507146 to your computer and use it in GitHub Desktop.
Searching photo caption and filtering by owner's gender
curl -XDELETE 'http://localhost:9200/test/'
curl -XPUT 'http://localhost:9200/test/' -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
},
"mappings" : {
"profile" : {
"properties" : {
"name" : {"type" : "string", "store" : "yes"},
"about" : {"type" : "string", "store" : "yes"},
"gender" : {"type" : "string", "store" : "yes"},
"uid" : {"type" : "string", "store" : "yes"}
}
},
"picture":{
"_parent": {"type": "profile"},
"properties" : {
"caption" : {"type" : "string", "store" : "yes"},
"uid" : {"type" : "string", "store" : "yes"},
"pid" : {"type" : "string", "store" : "yes"}
}
}
}
}'
curl -XPOST 'http://localhost:9200/test/profile/1' -d '{
"profile" : {
"uid" : "1",
"name" : "Matthew Tovbin",
"gender" : "male",
"about" : "curious software developer; desperate pc gamer; sci-fi fan; Internet addict; engineer @mrnumber"
}
}'
curl -XPOST 'http://localhost:9200/test/profile/2' -d '{
"profile" : {
"uid" : "2",
"name" : "Peter Johnson",
"gender" : "male",
"about" : "Just a dude"
}
}'
curl -XPOST 'http://localhost:9200/test/profile/3' -d '{
"profile" : {
"uid" : "3",
"name" : "Jamie Jameson",
"gender" : "female",
"about" : "Just a chick"
}
}'
curl -XPOST http://localhost:9200/test/picture/1?parent=1 -d '{
"caption": "What a nice view - 1",
"pid": "1", "uid" : "1"
}'
curl -XPOST http://localhost:9200/test/picture/2?parent=1 -d '{
"caption": "What a nice car - 2",
"pid": "2", "uid" : "1"
}'
curl -XPOST http://localhost:9200/test/picture/3?parent=2 -d '{
"caption": "Amazing view",
"pid": "3", "uid" : "2"
}'
curl -XPOST http://localhost:9200/test/picture/4?parent=3 -d '{
"caption": "Amazing view....",
"pid": "4", "uid" : "3"
}'
curl -XPOST localhost:9200/test/picture/_search -d '{
"query": {
"bool" : {
"must" : [
{
"has_parent" : {
"parent_type" : "profile",
"query" : {
"term" : {
"gender" : "male"
}
}
}
},
{
"match": {
"caption" : "view"
}
}
]
}
}
}'
curl -XPOST localhost:9200/test/profile,picture/_search -d '{
"query": {
"bool" : {
"should" : [
{"bool" : {
"must" : [
{
"has_parent" : {
"parent_type" : "profile",
"query" : {
"term" : {
"gender" : "male"
}
}
}
},
{
"match": {
"caption" : "view"
}
}
]
}},
{"bool" : {
"must" : {
"term" : { "name" : "peter" }
}
}}
],
"minimum_number_should_match" : 1
}
}
}' | python -m json.tool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment