Skip to content

Instantly share code, notes, and snippets.

@mikebaldry
Created November 25, 2013 07:18
Show Gist options
  • Save mikebaldry/7637501 to your computer and use it in GitHub Desktop.
Save mikebaldry/7637501 to your computer and use it in GitHub Desktop.
# delete index if exists
curl -XDELETE localhost:9200/test_nesting/
# create index with default settings
curl -XPOST localhost:9200/test_nesting/
# create mapping
curl -XPUT localhost:9200/test_nesting/link/_mapping -d '
{
"link" : {
"properties" : {
"url" : {"type" : "string"},
"sources" : {
"type": "nested",
"properties" : {
"full_name" : {"type" : "string"},
"screen_name" : {"type" : "string"},
"external_id" : {"type" : "string", "index" : "not_analyzed"}
}
}
}
}
}
'
# add some data
curl -XPUT localhost:9200/test_nesting/link/1 -d '
{
"url": "http://www.google.com",
"sources": [
{
"full_name": "Steve",
"screen_name": "Stevo19",
"external_id": 1
},
{
"full_name": "Jim",
"screen_name": "JimmyJimJim",
"external_id": 2
},
{
"full_name": "Brian",
"screen_name": "CoolBrian23",
"external_id": 3
}
]
}
'
curl -XPUT localhost:9200/test_nesting/link/2 -d '
{
"url": "http://www.yahoo.com",
"sources": [
{
"full_name": "Steve",
"screen_name": "Stevo19",
"external_id": 1
},
{
"full_name": "Mark",
"screen_name": "Markooose",
"external_id": 4
}
]
}
'
curl -XPUT localhost:9200/test_nesting/link/3 -d '
{
"url": "http://www.yahoo.com",
"sources": [
{
"full_name": "Mark",
"screen_name": "Markooose",
"external_id": 4
}
]
}
'
curl -XPUT localhost:9200/test_nesting/link/4 -d '
{
"url": "http://www.facebook.com",
"sources": [
{
"full_name": "Jim",
"screen_name": "JimmyJimJim",
"external_id": 2
},
{
"full_name": "Mark",
"screen_name": "Markooose",
"external_id": 4
}
]
}
'
curl -XPOST 'http://127.0.0.1:9200/test_nesting/link/_search?pretty=1' -d '
{
"size": 0,
"query": {
"nested": {
"path": "sources",
"query": {
"match_phrase_prefix": {
"full_name": {
"query": "St"
}
}
}
}
},
"facets": {
"author": {
"terms": {
"script_field": "String result = \"\"; for (int i = 0; i < doc[\"sources\"].values.size(); i++) { result += doc[\"sources\"].values[i].screen_name + Character.toString(0x02) + doc[\"sources\"].values[i].full_name + Character.toString(0x03) } return result;",
"size": 10
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment