advanced-scripting.asciidoc 5.9 KB

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