similarity.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. [[index-modules-similarity]]
  2. == Similarity module
  3. A similarity (scoring / ranking model) defines how matching documents
  4. are scored. Similarity is per field, meaning that via the mapping one
  5. can define a different similarity per field.
  6. Configuring a custom similarity is considered an expert feature and the
  7. builtin similarities are most likely sufficient as is described in
  8. <<similarity>>.
  9. [float]
  10. [[configuration]]
  11. === Configuring a similarity
  12. Most existing or custom Similarities have configuration options which
  13. can be configured via the index settings as shown below. The index
  14. options can be provided when creating an index or updating index
  15. settings.
  16. [source,js]
  17. --------------------------------------------------
  18. PUT /index
  19. {
  20. "settings" : {
  21. "index" : {
  22. "similarity" : {
  23. "my_similarity" : {
  24. "type" : "DFR",
  25. "basic_model" : "g",
  26. "after_effect" : "l",
  27. "normalization" : "h2",
  28. "normalization.h2.c" : "3.0"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. --------------------------------------------------
  35. // CONSOLE
  36. Here we configure the DFRSimilarity so it can be referenced as
  37. `my_similarity` in mappings as is illustrate in the below example:
  38. [source,js]
  39. --------------------------------------------------
  40. PUT /index/_mapping/_doc
  41. {
  42. "properties" : {
  43. "title" : { "type" : "text", "similarity" : "my_similarity" }
  44. }
  45. }
  46. --------------------------------------------------
  47. // CONSOLE
  48. // TEST[continued]
  49. [float]
  50. === Available similarities
  51. [float]
  52. [[bm25]]
  53. ==== BM25 similarity (*default*)
  54. TF/IDF based similarity that has built-in tf normalization and
  55. is supposed to work better for short fields (like names). See
  56. http://en.wikipedia.org/wiki/Okapi_BM25[Okapi_BM25] for more details.
  57. This similarity has the following options:
  58. [horizontal]
  59. `k1`::
  60. Controls non-linear term frequency normalization
  61. (saturation). The default value is `1.2`.
  62. `b`::
  63. Controls to what degree document length normalizes tf values.
  64. The default value is `0.75`.
  65. `discount_overlaps`::
  66. Determines whether overlap tokens (Tokens with
  67. 0 position increment) are ignored when computing norm. By default this
  68. is true, meaning overlap tokens do not count when computing norms.
  69. Type name: `BM25`
  70. [float]
  71. [[dfr]]
  72. ==== DFR similarity
  73. Similarity that implements the
  74. {lucene-core-javadoc}/org/apache/lucene/search/similarities/DFRSimilarity.html[divergence
  75. from randomness] framework. This similarity has the following options:
  76. [horizontal]
  77. `basic_model`::
  78. Possible values: {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelG.html[`be`],
  79. {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelD.html[`d`],
  80. {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelG.html[`g`],
  81. {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelIF.html[`if`],
  82. {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelIn.html[`in`],
  83. {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelIne.html[`ine`] and
  84. {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelP.html[`p`].
  85. `be`, `d` and `p` should be avoided in practice as they might return scores that
  86. are equal to 0 or infinite with terms that do not meet the expected random
  87. distribution.
  88. `after_effect`::
  89. Possible values: {lucene-core-javadoc}/org/apache/lucene/search/similarities/AfterEffect.NoAfterEffect.html[`no`],
  90. {lucene-core-javadoc}/org/apache/lucene/search/similarities/AfterEffectB.html[`b`] and
  91. {lucene-core-javadoc}/org/apache/lucene/search/similarities/AfterEffectL.html[`l`].
  92. `normalization`::
  93. Possible values: {lucene-core-javadoc}/org/apache/lucene/search/similarities/Normalization.NoNormalization.html[`no`],
  94. {lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationH1.html[`h1`],
  95. {lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationH2.html[`h2`],
  96. {lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationH1.html[`h3`] and
  97. {lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationZ.html[`z`].
  98. All options but the first option need a normalization value.
  99. Type name: `DFR`
  100. [float]
  101. [[dfi]]
  102. ==== DFI similarity
  103. Similarity that implements the http://trec.nist.gov/pubs/trec21/papers/irra.web.nb.pdf[divergence from independence]
  104. model.
  105. This similarity has the following options:
  106. [horizontal]
  107. `independence_measure`:: Possible values
  108. {lucene-core-javadoc}/org/apache/lucene/search/similarities/IndependenceStandardized.html[`standardized`],
  109. {lucene-core-javadoc}/org/apache/lucene/search/similarities/IndependenceSaturated.html[`saturated`],
  110. {lucene-core-javadoc}/org/apache/lucene/search/similarities/IndependenceChiSquared.html[`chisquared`].
  111. When using this similarity, it is highly recommended to remove stop words to get
  112. good relevance. Also beware that terms whose frequency is less than the expected
  113. frequency will get a score equal to 0.
  114. Type name: `DFI`
  115. [float]
  116. [[ib]]
  117. ==== IB similarity.
  118. {lucene-core-javadoc}/org/apache/lucene/search/similarities/IBSimilarity.html[Information
  119. based model] . The algorithm is based on the concept that the information content in any symbolic 'distribution'
  120. sequence is primarily determined by the repetitive usage of its basic elements.
  121. For written texts this challenge would correspond to comparing the writing styles of different authors.
  122. This similarity has the following options:
  123. [horizontal]
  124. `distribution`:: Possible values:
  125. {lucene-core-javadoc}/org/apache/lucene/search/similarities/DistributionLL.html[`ll`] and
  126. {lucene-core-javadoc}/org/apache/lucene/search/similarities/DistributionSPL.html[`spl`].
  127. `lambda`:: Possible values:
  128. {lucene-core-javadoc}/org/apache/lucene/search/similarities/LambdaDF.html[`df`] and
  129. {lucene-core-javadoc}/org/apache/lucene/search/similarities/LambdaTTF.html[`ttf`].
  130. `normalization`:: Same as in `DFR` similarity.
  131. Type name: `IB`
  132. [float]
  133. [[lm_dirichlet]]
  134. ==== LM Dirichlet similarity.
  135. {lucene-core-javadoc}/org/apache/lucene/search/similarities/LMDirichletSimilarity.html[LM
  136. Dirichlet similarity] . This similarity has the following options:
  137. [horizontal]
  138. `mu`:: Default to `2000`.
  139. The scoring formula in the paper assigns negative scores to terms that have
  140. fewer occurrences than predicted by the language model, which is illegal to
  141. Lucene, so such terms get a score of 0.
  142. Type name: `LMDirichlet`
  143. [float]
  144. [[lm_jelinek_mercer]]
  145. ==== LM Jelinek Mercer similarity.
  146. {lucene-core-javadoc}/core/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.html[LM
  147. Jelinek Mercer similarity] . The algorithm attempts to capture important patterns in the text, while leaving out noise. This similarity has the following options:
  148. [horizontal]
  149. `lambda`:: The optimal value depends on both the collection and the query. The optimal value is around `0.1`
  150. for title queries and `0.7` for long queries. Default to `0.1`. When value approaches `0`, documents that match more query terms will be ranked higher than those that match fewer terms.
  151. Type name: `LMJelinekMercer`
  152. [float]
  153. [[scripted_similarity]]
  154. ==== Scripted similarity
  155. A similarity that allows you to use a script in order to specify how scores
  156. should be computed. For instance, the below example shows how to reimplement
  157. TF-IDF:
  158. [source,js]
  159. --------------------------------------------------
  160. PUT /index
  161. {
  162. "settings": {
  163. "number_of_shards": 1,
  164. "similarity": {
  165. "scripted_tfidf": {
  166. "type": "scripted",
  167. "script": {
  168. "source": "double tf = Math.sqrt(doc.freq); double idf = Math.log((field.docCount+1.0)/(term.docFreq+1.0)) + 1.0; double norm = 1/Math.sqrt(doc.length); return query.boost * tf * idf * norm;"
  169. }
  170. }
  171. }
  172. },
  173. "mappings": {
  174. "_doc": {
  175. "properties": {
  176. "field": {
  177. "type": "text",
  178. "similarity": "scripted_tfidf"
  179. }
  180. }
  181. }
  182. }
  183. }
  184. PUT /index/_doc/1
  185. {
  186. "field": "foo bar foo"
  187. }
  188. PUT /index/_doc/2
  189. {
  190. "field": "bar baz"
  191. }
  192. POST /index/_refresh
  193. GET /index/_search?explain=true
  194. {
  195. "query": {
  196. "query_string": {
  197. "query": "foo^1.7",
  198. "default_field": "field"
  199. }
  200. }
  201. }
  202. --------------------------------------------------
  203. // CONSOLE
  204. Which yields:
  205. [source,js]
  206. --------------------------------------------------
  207. {
  208. "took": 12,
  209. "timed_out": false,
  210. "_shards": {
  211. "total": 1,
  212. "successful": 1,
  213. "skipped": 0,
  214. "failed": 0
  215. },
  216. "hits": {
  217. "total": 1,
  218. "max_score": 1.9508477,
  219. "hits": [
  220. {
  221. "_shard": "[index][0]",
  222. "_node": "OzrdjxNtQGaqs4DmioFw9A",
  223. "_index": "index",
  224. "_type": "_doc",
  225. "_id": "1",
  226. "_score": 1.9508477,
  227. "_source": {
  228. "field": "foo bar foo"
  229. },
  230. "_explanation": {
  231. "value": 1.9508477,
  232. "description": "weight(field:foo in 0) [PerFieldSimilarity], result of:",
  233. "details": [
  234. {
  235. "value": 1.9508477,
  236. "description": "score from ScriptedSimilarity(weightScript=[null], script=[Script{type=inline, lang='painless', idOrCode='double tf = Math.sqrt(doc.freq); double idf = Math.log((field.docCount+1.0)/(term.docFreq+1.0)) + 1.0; double norm = 1/Math.sqrt(doc.length); return query.boost * tf * idf * norm;', options={}, params={}}]) computed from:",
  237. "details": [
  238. {
  239. "value": 1.0,
  240. "description": "weight",
  241. "details": []
  242. },
  243. {
  244. "value": 1.7,
  245. "description": "query.boost",
  246. "details": []
  247. },
  248. {
  249. "value": 2,
  250. "description": "field.docCount",
  251. "details": []
  252. },
  253. {
  254. "value": 4,
  255. "description": "field.sumDocFreq",
  256. "details": []
  257. },
  258. {
  259. "value": 5,
  260. "description": "field.sumTotalTermFreq",
  261. "details": []
  262. },
  263. {
  264. "value": 1,
  265. "description": "term.docFreq",
  266. "details": []
  267. },
  268. {
  269. "value": 2,
  270. "description": "term.totalTermFreq",
  271. "details": []
  272. },
  273. {
  274. "value": 2.0,
  275. "description": "doc.freq",
  276. "details": []
  277. },
  278. {
  279. "value": 3,
  280. "description": "doc.length",
  281. "details": []
  282. }
  283. ]
  284. }
  285. ]
  286. }
  287. }
  288. ]
  289. }
  290. }
  291. --------------------------------------------------
  292. // TESTRESPONSE[s/"took": 12/"took" : $body.took/]
  293. // TESTRESPONSE[s/OzrdjxNtQGaqs4DmioFw9A/$body.hits.hits.0._node/]
  294. WARNING: While scripted similarities provide a lot of flexibility, there is
  295. a set of rules that they need to satisfy. Failing to do so could make
  296. Elasticsearch silently return wrong top hits or fail with internal errors at
  297. search time:
  298. - Returned scores must be positive.
  299. - All other variables remaining equal, scores must not decrease when
  300. `doc.freq` increases.
  301. - All other variables remaining equal, scores must not increase when
  302. `doc.length` increases.
  303. You might have noticed that a significant part of the above script depends on
  304. statistics that are the same for every document. It is possible to make the
  305. above slightly more efficient by providing an `weight_script` which will
  306. compute the document-independent part of the score and will be available
  307. under the `weight` variable. When no `weight_script` is provided, `weight`
  308. is equal to `1`. The `weight_script` has access to the same variables as
  309. the `script` except `doc` since it is supposed to compute a
  310. document-independent contribution to the score.
  311. The below configuration will give the same tf-idf scores but is slightly
  312. more efficient:
  313. [source,js]
  314. --------------------------------------------------
  315. PUT /index
  316. {
  317. "settings": {
  318. "number_of_shards": 1,
  319. "similarity": {
  320. "scripted_tfidf": {
  321. "type": "scripted",
  322. "weight_script": {
  323. "source": "double idf = Math.log((field.docCount+1.0)/(term.docFreq+1.0)) + 1.0; return query.boost * idf;"
  324. },
  325. "script": {
  326. "source": "double tf = Math.sqrt(doc.freq); double norm = 1/Math.sqrt(doc.length); return weight * tf * norm;"
  327. }
  328. }
  329. }
  330. },
  331. "mappings": {
  332. "_doc": {
  333. "properties": {
  334. "field": {
  335. "type": "text",
  336. "similarity": "scripted_tfidf"
  337. }
  338. }
  339. }
  340. }
  341. }
  342. --------------------------------------------------
  343. // CONSOLE
  344. ////////////////////
  345. [source,js]
  346. --------------------------------------------------
  347. PUT /index/_doc/1
  348. {
  349. "field": "foo bar foo"
  350. }
  351. PUT /index/_doc/2
  352. {
  353. "field": "bar baz"
  354. }
  355. POST /index/_refresh
  356. GET /index/_search?explain=true
  357. {
  358. "query": {
  359. "query_string": {
  360. "query": "foo^1.7",
  361. "default_field": "field"
  362. }
  363. }
  364. }
  365. --------------------------------------------------
  366. // CONSOLE
  367. // TEST[continued]
  368. [source,js]
  369. --------------------------------------------------
  370. {
  371. "took": 1,
  372. "timed_out": false,
  373. "_shards": {
  374. "total": 1,
  375. "successful": 1,
  376. "skipped": 0,
  377. "failed": 0
  378. },
  379. "hits": {
  380. "total": 1,
  381. "max_score": 1.9508477,
  382. "hits": [
  383. {
  384. "_shard": "[index][0]",
  385. "_node": "OzrdjxNtQGaqs4DmioFw9A",
  386. "_index": "index",
  387. "_type": "_doc",
  388. "_id": "1",
  389. "_score": 1.9508477,
  390. "_source": {
  391. "field": "foo bar foo"
  392. },
  393. "_explanation": {
  394. "value": 1.9508477,
  395. "description": "weight(field:foo in 0) [PerFieldSimilarity], result of:",
  396. "details": [
  397. {
  398. "value": 1.9508477,
  399. "description": "score from ScriptedSimilarity(weightScript=[Script{type=inline, lang='painless', idOrCode='double idf = Math.log((field.docCount+1.0)/(term.docFreq+1.0)) + 1.0; return query.boost * idf;', options={}, params={}}], script=[Script{type=inline, lang='painless', idOrCode='double tf = Math.sqrt(doc.freq); double norm = 1/Math.sqrt(doc.length); return weight * tf * norm;', options={}, params={}}]) computed from:",
  400. "details": [
  401. {
  402. "value": 2.3892908,
  403. "description": "weight",
  404. "details": []
  405. },
  406. {
  407. "value": 1.7,
  408. "description": "query.boost",
  409. "details": []
  410. },
  411. {
  412. "value": 2,
  413. "description": "field.docCount",
  414. "details": []
  415. },
  416. {
  417. "value": 4,
  418. "description": "field.sumDocFreq",
  419. "details": []
  420. },
  421. {
  422. "value": 5,
  423. "description": "field.sumTotalTermFreq",
  424. "details": []
  425. },
  426. {
  427. "value": 1,
  428. "description": "term.docFreq",
  429. "details": []
  430. },
  431. {
  432. "value": 2,
  433. "description": "term.totalTermFreq",
  434. "details": []
  435. },
  436. {
  437. "value": 2.0,
  438. "description": "doc.freq",
  439. "details": []
  440. },
  441. {
  442. "value": 3,
  443. "description": "doc.length",
  444. "details": []
  445. }
  446. ]
  447. }
  448. ]
  449. }
  450. }
  451. ]
  452. }
  453. }
  454. --------------------------------------------------
  455. // TESTRESPONSE[s/"took": 1/"took" : $body.took/]
  456. // TESTRESPONSE[s/OzrdjxNtQGaqs4DmioFw9A/$body.hits.hits.0._node/]
  457. ////////////////////
  458. Type name: `scripted`
  459. [float]
  460. [[default-base]]
  461. ==== Default Similarity
  462. By default, Elasticsearch will use whatever similarity is configured as
  463. `default`.
  464. You can change the default similarity for all fields in an index when
  465. it is <<indices-create-index,created>>:
  466. [source,js]
  467. --------------------------------------------------
  468. PUT /index
  469. {
  470. "settings": {
  471. "index": {
  472. "similarity": {
  473. "default": {
  474. "type": "boolean"
  475. }
  476. }
  477. }
  478. }
  479. }
  480. --------------------------------------------------
  481. // CONSOLE
  482. If you want to change the default similarity after creating the index
  483. you must <<indices-open-close,close>> your index, send the following
  484. request and <<indices-open-close,open>> it again afterwards:
  485. [source,js]
  486. --------------------------------------------------
  487. POST /index/_close
  488. PUT /index/_settings
  489. {
  490. "index": {
  491. "similarity": {
  492. "default": {
  493. "type": "boolean"
  494. }
  495. }
  496. }
  497. }
  498. POST /index/_open
  499. --------------------------------------------------
  500. // CONSOLE
  501. // TEST[continued]