mlt-query.asciidoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. [[query-dsl-mlt-query]]
  2. === More Like This Query
  3. The More Like This Query (MLT Query) finds documents that are "like" a given
  4. set of documents. In order to do so, MLT selects a set of representative terms
  5. of these input documents, forms a query using these terms, executes the query
  6. and returns the results. The user controls the input documents, how the terms
  7. should be selected and how the query is formed. `more_like_this` can be
  8. shortened to `mlt` deprecated[5.0.0,use `more_like_this` instead).
  9. The simplest use case consists of asking for documents that are similar to a
  10. provided piece of text. Here, we are asking for all movies that have some text
  11. similar to "Once upon a time" in their "title" and in their "description"
  12. fields, limiting the number of selected terms to 12.
  13. [source,js]
  14. --------------------------------------------------
  15. GET /_search
  16. {
  17. "query": {
  18. "more_like_this" : {
  19. "fields" : ["title", "description"],
  20. "like" : "Once upon a time",
  21. "min_term_freq" : 1,
  22. "max_query_terms" : 12
  23. }
  24. }
  25. }
  26. --------------------------------------------------
  27. // CONSOLE
  28. A more complicated use case consists of mixing texts with documents already
  29. existing in the index. In this case, the syntax to specify a document is
  30. similar to the one used in the <<docs-multi-get,Multi GET API>>.
  31. [source,js]
  32. --------------------------------------------------
  33. GET /_search
  34. {
  35. "query": {
  36. "more_like_this" : {
  37. "fields" : ["title", "description"],
  38. "like" : [
  39. {
  40. "_index" : "imdb",
  41. "_type" : "movies",
  42. "_id" : "1"
  43. },
  44. {
  45. "_index" : "imdb",
  46. "_type" : "movies",
  47. "_id" : "2"
  48. },
  49. "and potentially some more text here as well"
  50. ],
  51. "min_term_freq" : 1,
  52. "max_query_terms" : 12
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. // CONSOLE
  58. Finally, users can mix some texts, a chosen set of documents but also provide
  59. documents not necessarily present in the index. To provide documents not
  60. present in the index, the syntax is similar to <<docs-termvectors-artificial-doc,artificial documents>>.
  61. [source,js]
  62. --------------------------------------------------
  63. GET /_search
  64. {
  65. "query": {
  66. "more_like_this" : {
  67. "fields" : ["name.first", "name.last"],
  68. "like" : [
  69. {
  70. "_index" : "marvel",
  71. "_type" : "quotes",
  72. "doc" : {
  73. "name": {
  74. "first": "Ben",
  75. "last": "Grimm"
  76. },
  77. "tweet": "You got no idea what I'd... what I'd give to be invisible."
  78. }
  79. },
  80. {
  81. "_index" : "marvel",
  82. "_type" : "quotes",
  83. "_id" : "2"
  84. }
  85. ],
  86. "min_term_freq" : 1,
  87. "max_query_terms" : 12
  88. }
  89. }
  90. }
  91. --------------------------------------------------
  92. // CONSOLE
  93. ==== How it Works
  94. Suppose we wanted to find all documents similar to a given input document.
  95. Obviously, the input document itself should be its best match for that type of
  96. query. And the reason would be mostly, according to
  97. link:https://lucene.apache.org/core/4_9_0/core/org/apache/lucene/search/similarities/TFIDFSimilarity.html[Lucene scoring formula],
  98. due to the terms with the highest tf-idf. Therefore, the terms of the input
  99. document that have the highest tf-idf are good representatives of that
  100. document, and could be used within a disjunctive query (or `OR`) to retrieve similar
  101. documents. The MLT query simply extracts the text from the input document,
  102. analyzes it, usually using the same analyzer at the field, then selects the
  103. top K terms with highest tf-idf to form a disjunctive query of these terms.
  104. IMPORTANT: The fields on which to perform MLT must be indexed and of type
  105. `string`. Additionally, when using `like` with documents, either `_source`
  106. must be enabled or the fields must be `stored` or store `term_vector`. In
  107. order to speed up analysis, it could help to store term vectors at index time.
  108. For example, if we wish to perform MLT on the "title" and "tags.raw" fields,
  109. we can explicitly store their `term_vector` at index time. We can still
  110. perform MLT on the "description" and "tags" fields, as `_source` is enabled by
  111. default, but there will be no speed up on analysis for these fields.
  112. [source,js]
  113. --------------------------------------------------
  114. PUT /imdb
  115. {
  116. "mappings": {
  117. "movies": {
  118. "properties": {
  119. "title": {
  120. "type": "text",
  121. "term_vector": "yes"
  122. },
  123. "description": {
  124. "type": "text"
  125. },
  126. "tags": {
  127. "type": "text",
  128. "fields" : {
  129. "raw": {
  130. "type" : "text",
  131. "analyzer": "keyword",
  132. "term_vector" : "yes"
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. --------------------------------------------------
  141. // CONSOLE
  142. ==== Parameters
  143. The only required parameter is `like`, all other parameters have sensible
  144. defaults. There are three types of parameters: one to specify the document
  145. input, the other one for term selection and for query formation.
  146. [float]
  147. ==== Document Input Parameters
  148. [horizontal]
  149. `like`::
  150. The only *required* parameter of the MLT query is `like` and follows a
  151. versatile syntax, in which the user can specify free form text and/or a single
  152. or multiple documents (see examples above). The syntax to specify documents is
  153. similar to the one used by the <<docs-multi-get,Multi GET API>>. When
  154. specifying documents, the text is fetched from `fields` unless overridden in
  155. each document request. The text is analyzed by the analyzer at the field, but
  156. could also be overridden. The syntax to override the analyzer at the field
  157. follows a similar syntax to the `per_field_analyzer` parameter of the
  158. <<docs-termvectors-per-field-analyzer,Term Vectors API>>.
  159. Additionally, to provide documents not necessarily present in the index,
  160. <<docs-termvectors-artificial-doc,artificial documents>> are also supported.
  161. `unlike`::
  162. The `unlike` parameter is used in conjunction with `like` in order not to
  163. select terms found in a chosen set of documents. In other words, we could ask
  164. for documents `like: "Apple"`, but `unlike: "cake crumble tree"`. The syntax
  165. is the same as `like`.
  166. `fields`::
  167. A list of fields to fetch and analyze the text from. Defaults to the `_all`
  168. field for free text and to all possible fields for document inputs.
  169. `like_text`::
  170. The text to find documents like it.
  171. `ids` or `docs`::
  172. A list of documents following the same syntax as the <<docs-multi-get,Multi GET API>>.
  173. [float]
  174. [[mlt-query-term-selection]]
  175. ==== Term Selection Parameters
  176. [horizontal]
  177. `max_query_terms`::
  178. The maximum number of query terms that will be selected. Increasing this value
  179. gives greater accuracy at the expense of query execution speed. Defaults to
  180. `25`.
  181. `min_term_freq`::
  182. The minimum term frequency below which the terms will be ignored from the
  183. input document. Defaults to `2`.
  184. `min_doc_freq`::
  185. The minimum document frequency below which the terms will be ignored from the
  186. input document. Defaults to `5`.
  187. `max_doc_freq`::
  188. The maximum document frequency above which the terms will be ignored from the
  189. input document. This could be useful in order to ignore highly frequent words
  190. such as stop words. Defaults to unbounded (`0`).
  191. `min_word_length`::
  192. The minimum word length below which the terms will be ignored. The old name
  193. `min_word_len` is deprecated. Defaults to `0`.
  194. `max_word_length`::
  195. The maximum word length above which the terms will be ignored. The old name
  196. `max_word_len` is deprecated. Defaults to unbounded (`0`).
  197. `stop_words`::
  198. An array of stop words. Any word in this set is considered "uninteresting" and
  199. ignored. If the analyzer allows for stop words, you might want to tell MLT to
  200. explicitly ignore them, as for the purposes of document similarity it seems
  201. reasonable to assume that "a stop word is never interesting".
  202. `analyzer`::
  203. The analyzer that is used to analyze the free form text. Defaults to the
  204. analyzer associated with the first field in `fields`.
  205. [float]
  206. ==== Query Formation Parameters
  207. [horizontal]
  208. `minimum_should_match`::
  209. After the disjunctive query has been formed, this parameter controls the
  210. number of terms that must match.
  211. The syntax is the same as the <<query-dsl-minimum-should-match,minimum should match>>.
  212. (Defaults to `"30%"`).
  213. `boost_terms`::
  214. Each term in the formed query could be further boosted by their tf-idf score.
  215. This sets the boost factor to use when using this feature. Defaults to
  216. deactivated (`0`). Any other positive value activates terms boosting with the
  217. given boost factor.
  218. `include`::
  219. Specifies whether the input documents should also be included in the search
  220. results returned. Defaults to `false`.
  221. `boost`::
  222. Sets the boost value of the whole query. Defaults to `1.0`.