fielddata.asciidoc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [[fielddata]]
  2. === `fielddata`
  3. Most fields are <<mapping-index,indexed>> by default, which makes them
  4. searchable. Sorting, aggregations, and accessing field values in scripts,
  5. however, requires a different access pattern from search.
  6. Search needs to answer the question _"Which documents contain this term?"_,
  7. while sorting and aggregations need to answer a different question: _"What is
  8. the value of this field for **this** document?"_.
  9. Most fields can use index-time, on-disk <<doc-values,`doc_values`>> for this
  10. data access pattern, but <<text,`text`>> fields do not support `doc_values`.
  11. Instead, `text` fields use a query-time *in-memory* data structure called
  12. `fielddata`. This data structure is built on demand the first time that a
  13. field is used for aggregations, sorting, or in a script. It is built by
  14. reading the entire inverted index for each segment from disk, inverting the
  15. term ↔︎ document relationship, and storing the result in memory, in the JVM
  16. heap.
  17. ==== Fielddata is disabled on `text` fields by default
  18. Fielddata can consume a *lot* of heap space, especially when loading high
  19. cardinality `text` fields. Once fielddata has been loaded into the heap, it
  20. remains there for the lifetime of the segment. Also, loading fielddata is an
  21. expensive process which can cause users to experience latency hits. This is
  22. why fielddata is disabled by default.
  23. If you try to sort, aggregate, or access values from a script on a `text`
  24. field, you will see this exception:
  25. [quote]
  26. --
  27. Fielddata is disabled on text fields by default. Set `fielddata=true` on
  28. [`your_field_name`] in order to load fielddata in memory by uninverting the
  29. inverted index. Note that this can however use significant memory.
  30. --
  31. [[before-enabling-fielddata]]
  32. ==== Before enabling fielddata
  33. Before you enable fielddata, consider why you are using a `text` field for
  34. aggregations, sorting, or in a script. It usually doesn't make sense to do
  35. so.
  36. A text field is analyzed before indexing so that a value like
  37. `New York` can be found by searching for `new` or for `york`. A `terms`
  38. aggregation on this field will return a `new` bucket and a `york` bucket, when
  39. you probably want a single bucket called `New York`.
  40. Instead, you should have a `text` field for full text searches, and an
  41. unanalyzed <<keyword,`keyword`>> field with <<doc-values,`doc_values`>>
  42. enabled for aggregations, as follows:
  43. [source,js]
  44. ---------------------------------
  45. PUT my_index
  46. {
  47. "mappings": {
  48. "properties": {
  49. "my_field": { <1>
  50. "type": "text",
  51. "fields": {
  52. "keyword": { <2>
  53. "type": "keyword"
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. ---------------------------------
  61. // CONSOLE
  62. <1> Use the `my_field` field for searches.
  63. <2> Use the `my_field.keyword` field for aggregations, sorting, or in scripts.
  64. ==== Enabling fielddata on `text` fields
  65. You can enable fielddata on an existing `text` field using the
  66. <<indices-put-mapping,PUT mapping API>> as follows:
  67. [source,js]
  68. -----------------------------------
  69. PUT my_index/_mapping
  70. {
  71. "properties": {
  72. "my_field": { <1>
  73. "type": "text",
  74. "fielddata": true
  75. }
  76. }
  77. }
  78. -----------------------------------
  79. // CONSOLE
  80. // TEST[continued]
  81. <1> The mapping that you specify for `my_field` should consist of the existing
  82. mapping for that field, plus the `fielddata` parameter.
  83. [[field-data-filtering]]
  84. ==== `fielddata_frequency_filter`
  85. Fielddata filtering can be used to reduce the number of terms loaded into
  86. memory, and thus reduce memory usage. Terms can be filtered by _frequency_:
  87. The frequency filter allows you to only load terms whose document frequency falls
  88. between a `min` and `max` value, which can be expressed an absolute
  89. number (when the number is bigger than 1.0) or as a percentage
  90. (eg `0.01` is `1%` and `1.0` is `100%`). Frequency is calculated
  91. *per segment*. Percentages are based on the number of docs which have a
  92. value for the field, as opposed to all docs in the segment.
  93. Small segments can be excluded completely by specifying the minimum
  94. number of docs that the segment should contain with `min_segment_size`:
  95. [source,js]
  96. --------------------------------------------------
  97. PUT my_index
  98. {
  99. "mappings": {
  100. "properties": {
  101. "tag": {
  102. "type": "text",
  103. "fielddata": true,
  104. "fielddata_frequency_filter": {
  105. "min": 0.001,
  106. "max": 0.1,
  107. "min_segment_size": 500
  108. }
  109. }
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. // CONSOLE