matzie

Elastic search Nested Documents and Terms Aggregation

May 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 2.16 KB | None | 0 0
  1. # Given this index and mapping:
  2.  
  3. POST /test
  4. {
  5.     "settings" : {
  6.         "number_of_shards" : 1
  7.     },
  8.     "mappings" : {
  9.         "testdoc" : {
  10.             "properties" : {
  11.               "META": {
  12.                "type": "nested",
  13.                "properties": {
  14.                 "scheme_name": {
  15.                   "type": "string",
  16.                   "index": "no"
  17.                 },
  18.                 "scheme_data": {
  19.                   "type": "nested",
  20.                   "properties": {
  21.                   "column_name": {
  22.                     "type": "string",
  23.                     "index": "no" },
  24.                   "value": {
  25.                     "type": "string"}}}}}}}}}
  26.  
  27.  
  28.  
  29. # And this test data:
  30.  
  31. POST /test/testdoc
  32. {
  33.   "path": "/matt/matt-pics",
  34.   "file": "dog.jpg",
  35.   "mime_type": "image/jpeg",
  36.   "META": [
  37.     {
  38.       "scheme_name": "Pseudo_EXIF_Scheme",
  39.       "scheme_data": [
  40.         {
  41.           "value": "Nikon",
  42.           "column_name": "CAMERA_MANUFACTURER"
  43.         },
  44.         {
  45.           "value": "D80",
  46.           "column_name": "CAMERA_MODEL"
  47.         }
  48.        
  49.       ]
  50.     }
  51.   ]
  52.  
  53. }
  54.  
  55. POST /test/testdoc
  56. {
  57.   "path": "/matt/matt-music",
  58.   "file": "song.mp3",
  59.   "mime_type": "audio/mp3",
  60.   "META": [
  61.     {
  62.       "scheme_name": "Pseudo_MP3_Scheme",
  63.       "scheme_data": [
  64.         {
  65.           "value": "Foo",
  66.           "column_name": "ARTIST"
  67.         },
  68.         {
  69.           "value": "Bar",
  70.           "column_name": "SONGTITLE"
  71.         }
  72.        
  73.       ]
  74.     }
  75.   ]
  76.  
  77. }
  78.  
  79. # I want to be able to do terms / bucket aggregations to get something like this (buckets over META.scheme_name
  80. #<snip>
  81. "buckets": [
  82.         {
  83.           "key": "Pseudo_MP3_Scheme",
  84.           "doc_count": 1
  85.         },
  86.         {
  87.           "key": "Pseudo_EXIF_Scheme",
  88.           "doc_count": 1
  89.         }
  90. ]
  91. #<snip>
  92.  
  93. #and also over specific values in the nested doc eg for 'Artist'
  94.  
  95. "buckets": [
  96.         {
  97.           "key": "Foo",
  98.           "doc_count": 1
  99.         }
  100. ]
  101.  
  102. #But I'm totally stuck on the syntax for aggregating nested documents like this and have tried for several hours without any success.
  103. #Thanks in advance for your help!
Add Comment
Please, Sign In to add comment