size-field.asciidoc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. [[mapping-size-field]]
  2. === `_size` field
  3. The `_size` field, when enabled, indexes the size in bytes of the original
  4. <<mapping-source-field,`_source`>>. In order to enable it, set
  5. the mapping as follows:
  6. [source,js]
  7. --------------------------
  8. PUT my_index
  9. {
  10. "mappings": {
  11. "my_type": {
  12. "_size": {
  13. "enabled": true
  14. }
  15. }
  16. }
  17. }
  18. --------------------------
  19. // AUTOSENSE
  20. The value of the `_size` field is accessible in queries, aggregations, scripts,
  21. and when sorting:
  22. [source,js]
  23. --------------------------
  24. # Example documents
  25. PUT my_index/my_type/1
  26. {
  27. "text": "This is a document"
  28. }
  29. PUT my_index/my_type/2
  30. {
  31. "text": "This is another document"
  32. }
  33. GET my_index/_search
  34. {
  35. "query": {
  36. "range": {
  37. "_size": { <1>
  38. "gt": 10
  39. }
  40. }
  41. },
  42. "aggs": {
  43. "Sizes": {
  44. "terms": {
  45. "field": "_size", <2>
  46. "size": 10
  47. }
  48. }
  49. },
  50. "sort": [
  51. {
  52. "_size": { <3>
  53. "order": "desc"
  54. }
  55. }
  56. ],
  57. "script_fields": {
  58. "Size": {
  59. "script": "doc['_size']" <4>
  60. }
  61. }
  62. }
  63. --------------------------
  64. // AUTOSENSE
  65. <1> Querying on the `_size` field
  66. <2> Aggregating on the `_size` field
  67. <3> Sorting on the `_size` field
  68. <4> Accessing the `_size` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)