fielddata.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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-disabled-text-fields]]
  18. ==== Fielddata is disabled on `text` fields by default
  19. Fielddata can consume a *lot* of heap space, especially when loading high
  20. cardinality `text` fields. Once fielddata has been loaded into the heap, it
  21. remains there for the lifetime of the segment. Also, loading fielddata is an
  22. expensive process which can cause users to experience latency hits. This is
  23. why fielddata is disabled by default.
  24. If you try to sort, aggregate, or access values from a script on a `text`
  25. field, you will see this exception:
  26. [literal]
  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. [[before-enabling-fielddata]]
  31. ==== Before enabling fielddata
  32. Before you enable fielddata, consider why you are using a `text` field for
  33. aggregations, sorting, or in a script. It usually doesn't make sense to do
  34. so.
  35. A text field is analyzed before indexing so that a value like
  36. `New York` can be found by searching for `new` or for `york`. A `terms`
  37. aggregation on this field will return a `new` bucket and a `york` bucket, when
  38. you probably want a single bucket called `New York`.
  39. Instead, you should have a `text` field for full text searches, and an
  40. unanalyzed <<keyword,`keyword`>> field with <<doc-values,`doc_values`>>
  41. enabled for aggregations, as follows:
  42. [source,console]
  43. ---------------------------------
  44. PUT my_index
  45. {
  46. "mappings": {
  47. "properties": {
  48. "my_field": { <1>
  49. "type": "text",
  50. "fields": {
  51. "keyword": { <2>
  52. "type": "keyword"
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. ---------------------------------
  60. <1> Use the `my_field` field for searches.
  61. <2> Use the `my_field.keyword` field for aggregations, sorting, or in scripts.
  62. [[enable-fielddata-text-fields]]
  63. ==== Enabling fielddata on `text` fields
  64. You can enable fielddata on an existing `text` field using the
  65. <<indices-put-mapping,PUT mapping API>> as follows:
  66. [source,console]
  67. -----------------------------------
  68. PUT my_index/_mapping
  69. {
  70. "properties": {
  71. "my_field": { <1>
  72. "type": "text",
  73. "fielddata": true
  74. }
  75. }
  76. }
  77. -----------------------------------
  78. // TEST[continued]
  79. <1> The mapping that you specify for `my_field` should consist of the existing
  80. mapping for that field, plus the `fielddata` parameter.
  81. [[field-data-filtering]]
  82. ==== `fielddata_frequency_filter`
  83. Fielddata filtering can be used to reduce the number of terms loaded into
  84. memory, and thus reduce memory usage. Terms can be filtered by _frequency_:
  85. The frequency filter allows you to only load terms whose document frequency falls
  86. between a `min` and `max` value, which can be expressed an absolute
  87. number (when the number is bigger than 1.0) or as a percentage
  88. (eg `0.01` is `1%` and `1.0` is `100%`). Frequency is calculated
  89. *per segment*. Percentages are based on the number of docs which have a
  90. value for the field, as opposed to all docs in the segment.
  91. Small segments can be excluded completely by specifying the minimum
  92. number of docs that the segment should contain with `min_segment_size`:
  93. [source,console]
  94. --------------------------------------------------
  95. PUT my_index
  96. {
  97. "mappings": {
  98. "properties": {
  99. "tag": {
  100. "type": "text",
  101. "fielddata": true,
  102. "fielddata_frequency_filter": {
  103. "min": 0.001,
  104. "max": 0.1,
  105. "min_segment_size": 500
  106. }
  107. }
  108. }
  109. }
  110. }
  111. --------------------------------------------------