advanced-scripting.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. [[modules-advanced-scripting]]
  2. === Advanced text scoring in scripts
  3. experimental[The functionality described on this page is considered experimental and may be changed or removed in a future release]
  4. Text features, such as term or document frequency for a specific term can be
  5. accessed in scripts with the `_index` variable. This can be useful if, for
  6. example, you want to implement your own scoring model using for example a
  7. script inside a <<query-dsl-function-score-query,function score query>>.
  8. Statistics over the document collection are computed *per shard*, not per
  9. index.
  10. [float]
  11. === Nomenclature:
  12. [horizontal]
  13. `df`::
  14. document frequency. The number of documents a term appears in. Computed
  15. per field.
  16. `tf`::
  17. term frequency. The number times a term appears in a field in one specific
  18. document.
  19. `ttf`::
  20. total term frequency. The number of times this term appears in all
  21. documents, that is, the sum of `tf` over all documents. Computed per
  22. field.
  23. `df` and `ttf` are computed per shard and therefore these numbers can vary
  24. depending on the shard the current document resides in.
  25. [float]
  26. === Shard statistics:
  27. `_index.numDocs()`::
  28. Number of documents in shard.
  29. `_index.maxDoc()`::
  30. Maximal document number in shard.
  31. `_index.numDeletedDocs()`::
  32. Number of deleted documents in shard.
  33. [float]
  34. === Field statistics:
  35. Field statistics can be accessed with a subscript operator like this:
  36. `_index['FIELD']`.
  37. `_index['FIELD'].docCount()`::
  38. Number of documents containing the field `FIELD`. Does not take deleted documents into account.
  39. `_index['FIELD'].sumttf()`::
  40. Sum of `ttf` over all terms that appear in field `FIELD` in all documents.
  41. `_index['FIELD'].sumdf()`::
  42. The sum of `df` s over all terms that appear in field `FIELD` in all
  43. documents.
  44. Field statistics are computed per shard and therefore these numbers can vary
  45. depending on the shard the current document resides in.
  46. The number of terms in a field cannot be accessed using the `_index` variable. See <<token-count>> for how to do that.
  47. [float]
  48. === Term statistics:
  49. Term statistics for a field can be accessed with a subscript operator like
  50. this: `_index['FIELD']['TERM']`. This will never return null, even if term or field does not exist.
  51. If you do not need the term frequency, call `_index['FIELD'].get('TERM', 0)`
  52. to avoid unnecessary initialization of the frequencies. The flag will have only
  53. affect is your set the <<index-options,`index_options`>> to `docs`.
  54. `_index['FIELD']['TERM'].df()`::
  55. `df` of term `TERM` in field `FIELD`. Will be returned, even if the term
  56. is not present in the current document.
  57. `_index['FIELD']['TERM'].ttf()`::
  58. The sum of term frequencies of term `TERM` in field `FIELD` over all
  59. documents. Will be returned, even if the term is not present in the
  60. current document.
  61. `_index['FIELD']['TERM'].tf()`::
  62. `tf` of term `TERM` in field `FIELD`. Will be 0 if the term is not present
  63. in the current document.
  64. [float]
  65. === Term positions, offsets and payloads:
  66. If you need information on the positions of terms in a field, call
  67. `_index['FIELD'].get('TERM', flag)` where flag can be
  68. [horizontal]
  69. `_POSITIONS`:: if you need the positions of the term
  70. `_OFFSETS`:: if you need the offsets of the term
  71. `_PAYLOADS`:: if you need the payloads of the term
  72. `_CACHE`:: if you need to iterate over all positions several times
  73. The iterator uses the underlying lucene classes to iterate over positions. For efficiency reasons, you can only iterate over positions once. If you need to iterate over the positions several times, set the `_CACHE` flag.
  74. You can combine the operators with a `|` if you need more than one info. For
  75. example, the following will return an object holding the positions and payloads,
  76. as well as all statistics:
  77. `_index['FIELD'].get('TERM', _POSITIONS | _PAYLOADS)`
  78. Positions can be accessed with an iterator that returns an object
  79. (`POS_OBJECT`) holding position, offsets and payload for each term position.
  80. `POS_OBJECT.position`::
  81. The position of the term.
  82. `POS_OBJECT.startOffset`::
  83. The start offset of the term.
  84. `POS_OBJECT.endOffset`::
  85. The end offset of the term.
  86. `POS_OBJECT.payload`::
  87. The payload of the term.
  88. `POS_OBJECT.payloadAsInt(missingValue)`::
  89. The payload of the term converted to integer. If the current position has
  90. no payload, the `missingValue` will be returned. Call this only if you
  91. know that your payloads are integers.
  92. `POS_OBJECT.payloadAsFloat(missingValue)`::
  93. The payload of the term converted to float. If the current position has no
  94. payload, the `missingValue` will be returned. Call this only if you know
  95. that your payloads are floats.
  96. `POS_OBJECT.payloadAsString()`::
  97. The payload of the term converted to string. If the current position has
  98. no payload, `null` will be returned. Call this only if you know that your
  99. payloads are strings.
  100. Example: sums up all payloads for the term `foo`.
  101. [source,groovy]
  102. ---------------------------------------------------------
  103. termInfo = _index['my_field'].get('foo',_PAYLOADS);
  104. score = 0;
  105. for (pos in termInfo) {
  106. score = score + pos.payloadAsInt(0);
  107. }
  108. return score;
  109. ---------------------------------------------------------
  110. [float]
  111. === Term vectors:
  112. The `_index` variable can only be used to gather statistics for single terms. If you want to use information on all terms in a field, you must store the term vectors (see <<term-vector>>). To access them, call
  113. `_index.termVectors()` to get a
  114. https://lucene.apache.org/core/4_0_0/core/org/apache/lucene/index/Fields.html[Fields]
  115. instance. This object can then be used as described in https://lucene.apache.org/core/4_0_0/core/org/apache/lucene/index/Fields.html[lucene doc] to iterate over fields and then for each field iterate over each term in the field.
  116. The method will return null if the term vectors were not stored.