2013-08-28 19:24:34 -04:00
[[index-modules-similarity]]
== Similarity module
A similarity (scoring / ranking model) defines how matching documents
are scored. Similarity is per field, meaning that via the mapping one
can define a different similarity per field.
2017-10-12 11:35:25 -04:00
Configuring a custom similarity is considered an expert feature and the
2015-08-06 11:24:29 -04:00
builtin similarities are most likely sufficient as is described in
<<similarity>>.
2013-08-28 19:24:34 -04:00
[float]
2013-09-25 12:17:40 -04:00
[[configuration]]
2013-08-28 19:24:34 -04:00
=== Configuring a similarity
Most existing or custom Similarities have configuration options which
can be configured via the index settings as shown below. The index
options can be provided when creating an index or updating index
settings.
2019-09-06 11:31:13 -04:00
[source,console]
2013-08-28 19:24:34 -04:00
--------------------------------------------------
2019-01-22 09:13:52 -05:00
PUT /index
2017-08-30 03:30:36 -04:00
{
"settings" : {
"index" : {
"similarity" : {
"my_similarity" : {
"type" : "DFR",
"basic_model" : "g",
"after_effect" : "l",
"normalization" : "h2",
"normalization.h2.c" : "3.0"
}
}
}
}
2013-08-28 19:24:34 -04:00
}
--------------------------------------------------
Here we configure the DFRSimilarity so it can be referenced as
`my_similarity` in mappings as is illustrate in the below example:
2019-09-06 11:31:13 -04:00
[source,console]
2013-08-28 19:24:34 -04:00
--------------------------------------------------
2019-01-14 16:08:01 -05:00
PUT /index/_mapping
2013-08-28 19:24:34 -04:00
{
2017-08-30 03:30:36 -04:00
"properties" : {
"title" : { "type" : "text", "similarity" : "my_similarity" }
}
2015-08-06 11:24:29 -04:00
}
2013-08-28 19:24:34 -04:00
--------------------------------------------------
2017-08-30 03:30:36 -04:00
// TEST[continued]
2013-08-28 19:24:34 -04:00
[float]
=== Available similarities
[float]
2013-09-25 12:17:40 -04:00
[[bm25]]
2016-06-17 13:08:40 -04:00
==== BM25 similarity (*default*)
2013-08-28 19:24:34 -04:00
2016-06-17 13:08:40 -04:00
TF/IDF based similarity that has built-in tf normalization and
2013-08-28 19:24:34 -04:00
is supposed to work better for short fields (like names). See
http://en.wikipedia.org/wiki/Okapi_BM25[Okapi_BM25] for more details.
This similarity has the following options:
[horizontal]
2015-08-06 11:24:29 -04:00
`k1`::
2013-08-28 19:24:34 -04:00
Controls non-linear term frequency normalization
2016-06-13 12:57:01 -04:00
(saturation). The default value is `1.2`.
2013-08-28 19:24:34 -04:00
2015-08-06 11:24:29 -04:00
`b`::
Controls to what degree document length normalizes tf values.
2016-06-13 12:57:01 -04:00
The default value is `0.75`.
2013-08-28 19:24:34 -04:00
2015-08-06 11:24:29 -04:00
`discount_overlaps`::
2013-08-28 19:24:34 -04:00
Determines whether overlap tokens (Tokens with
0 position increment) are ignored when computing norm. By default this
is true, meaning overlap tokens do not count when computing norms.
Type name: `BM25`
[float]
2018-03-21 05:41:10 -04:00
[[dfr]]
2014-02-13 10:45:30 -05:00
==== DFR similarity
2013-08-28 19:24:34 -04:00
Similarity that implements the
2018-03-21 05:41:10 -04:00
{lucene-core-javadoc}/org/apache/lucene/search/similarities/DFRSimilarity.html[divergence
2013-08-28 19:24:34 -04:00
from randomness] framework. This similarity has the following options:
[horizontal]
2015-08-06 11:24:29 -04:00
`basic_model`::
2019-03-29 04:34:51 -04:00
Possible values: {lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelG.html[`g`],
2018-03-21 05:41:10 -04:00
{lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelIF.html[`if`],
2019-03-29 04:34:51 -04:00
{lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelIn.html[`in`] and
{lucene-core-javadoc}/org/apache/lucene/search/similarities/BasicModelIne.html[`ine`].
2013-08-28 19:24:34 -04:00
`after_effect`::
2019-03-29 04:34:51 -04:00
Possible values: {lucene-core-javadoc}/org/apache/lucene/search/similarities/AfterEffectB.html[`b`] and
{lucene-core-javadoc}/org/apache/lucene/search/similarities/AfterEffectB.html[`l`].
2013-08-28 19:24:34 -04:00
2015-08-06 11:24:29 -04:00
`normalization`::
2018-03-21 05:41:10 -04:00
Possible values: {lucene-core-javadoc}/org/apache/lucene/search/similarities/Normalization.NoNormalization.html[`no`],
{lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationH1.html[`h1`],
{lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationH2.html[`h2`],
{lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationH1.html[`h3`] and
{lucene-core-javadoc}/org/apache/lucene/search/similarities/NormalizationZ.html[`z`].
2013-08-28 19:24:34 -04:00
All options but the first option need a normalization value.
Type name: `DFR`
2016-01-20 03:32:51 -05:00
[float]
[[dfi]]
==== DFI similarity
Similarity that implements the http://trec.nist.gov/pubs/trec21/papers/irra.web.nb.pdf[divergence from independence]
2016-02-02 22:53:39 -05:00
model.
This similarity has the following options:
[horizontal]
2018-03-21 05:41:10 -04:00
`independence_measure`:: Possible values
{lucene-core-javadoc}/org/apache/lucene/search/similarities/IndependenceStandardized.html[`standardized`],
{lucene-core-javadoc}/org/apache/lucene/search/similarities/IndependenceSaturated.html[`saturated`],
{lucene-core-javadoc}/org/apache/lucene/search/similarities/IndependenceChiSquared.html[`chisquared`].
When using this similarity, it is highly recommended to remove stop words to get
good relevance. Also beware that terms whose frequency is less than the expected
frequency will get a score equal to 0.
2016-01-20 03:32:51 -05:00
2016-05-20 05:01:07 -04:00
Type name: `DFI`
2013-08-28 19:24:34 -04:00
[float]
2013-09-25 12:17:40 -04:00
[[ib]]
2013-08-28 19:24:34 -04:00
==== IB similarity.
2018-03-21 05:41:10 -04:00
{lucene-core-javadoc}/org/apache/lucene/search/similarities/IBSimilarity.html[Information
2015-12-14 08:27:40 -05:00
based model] . The algorithm is based on the concept that the information content in any symbolic 'distribution'
sequence is primarily determined by the repetitive usage of its basic elements.
2016-02-02 22:53:39 -05:00
For written texts this challenge would correspond to comparing the writing styles of different authors.
2015-12-14 08:27:40 -05:00
This similarity has the following options:
2013-08-28 19:24:34 -04:00
[horizontal]
2018-03-21 05:41:10 -04:00
`distribution`:: Possible values:
{lucene-core-javadoc}/org/apache/lucene/search/similarities/DistributionLL.html[`ll`] and
{lucene-core-javadoc}/org/apache/lucene/search/similarities/DistributionSPL.html[`spl`].
`lambda`:: Possible values:
{lucene-core-javadoc}/org/apache/lucene/search/similarities/LambdaDF.html[`df`] and
{lucene-core-javadoc}/org/apache/lucene/search/similarities/LambdaTTF.html[`ttf`].
2013-08-28 19:24:34 -04:00
`normalization`:: Same as in `DFR` similarity.
Type name: `IB`
2014-04-06 22:20:46 -04:00
[float]
[[lm_dirichlet]]
==== LM Dirichlet similarity.
2018-03-21 05:41:10 -04:00
{lucene-core-javadoc}/org/apache/lucene/search/similarities/LMDirichletSimilarity.html[LM
2014-04-06 22:20:46 -04:00
Dirichlet similarity] . This similarity has the following options:
[horizontal]
`mu`:: Default to `2000`.
2018-03-21 05:41:10 -04:00
The scoring formula in the paper assigns negative scores to terms that have
fewer occurrences than predicted by the language model, which is illegal to
Lucene, so such terms get a score of 0.
2014-04-06 22:20:46 -04:00
Type name: `LMDirichlet`
[float]
[[lm_jelinek_mercer]]
==== LM Jelinek Mercer similarity.
2018-03-21 05:41:10 -04:00
{lucene-core-javadoc}/core/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.html[LM
2015-12-14 08:27:40 -05:00
Jelinek Mercer similarity] . The algorithm attempts to capture important patterns in the text, while leaving out noise. This similarity has the following options:
2014-04-06 22:20:46 -04:00
[horizontal]
`lambda`:: The optimal value depends on both the collection and the query. The optimal value is around `0.1`
2015-12-14 08:27:40 -05:00
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.
2014-04-06 22:20:46 -04:00
Type name: `LMJelinekMercer`
2017-08-08 02:55:12 -04:00
[float]
[[scripted_similarity]]
==== Scripted similarity
A similarity that allows you to use a script in order to specify how scores
should be computed. For instance, the below example shows how to reimplement
TF-IDF:
2019-09-06 11:31:13 -04:00
[source,console]
2017-08-08 02:55:12 -04:00
--------------------------------------------------
2019-01-22 09:13:52 -05:00
PUT /index
2017-08-08 02:55:12 -04:00
{
"settings": {
"number_of_shards": 1,
"similarity": {
"scripted_tfidf": {
"type": "scripted",
"script": {
"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;"
}
}
}
},
"mappings": {
2019-01-22 09:13:52 -05:00
"properties": {
"field": {
"type": "text",
"similarity": "scripted_tfidf"
2017-08-08 02:55:12 -04:00
}
}
}
}
2017-12-14 11:47:53 -05:00
PUT /index/_doc/1
2017-08-08 02:55:12 -04:00
{
"field": "foo bar foo"
}
2017-12-14 11:47:53 -05:00
PUT /index/_doc/2
2017-08-08 02:55:12 -04:00
{
"field": "bar baz"
}
2017-08-30 03:30:36 -04:00
POST /index/_refresh
2017-08-08 02:55:12 -04:00
2017-08-30 03:30:36 -04:00
GET /index/_search?explain=true
2017-08-08 02:55:12 -04:00
{
"query": {
"query_string": {
"query": "foo^1.7",
"default_field": "field"
}
}
}
--------------------------------------------------
Which yields:
2019-09-06 16:09:09 -04:00
[source,console-result]
2017-08-08 02:55:12 -04:00
--------------------------------------------------
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
2018-12-05 13:49:06 -05:00
"total": {
"value": 1,
"relation": "eq"
},
2017-08-08 02:55:12 -04:00
"max_score": 1.9508477,
"hits": [
{
"_shard": "[index][0]",
"_node": "OzrdjxNtQGaqs4DmioFw9A",
"_index": "index",
2017-12-14 11:47:53 -05:00
"_type": "_doc",
2017-08-08 02:55:12 -04:00
"_id": "1",
"_score": 1.9508477,
"_source": {
"field": "foo bar foo"
},
"_explanation": {
"value": 1.9508477,
"description": "weight(field:foo in 0) [PerFieldSimilarity], result of:",
"details": [
{
"value": 1.9508477,
"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:",
"details": [
{
"value": 1.0,
"description": "weight",
"details": []
},
{
"value": 1.7,
"description": "query.boost",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 2,
2017-08-08 02:55:12 -04:00
"description": "field.docCount",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 4,
2017-08-08 02:55:12 -04:00
"description": "field.sumDocFreq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 5,
2017-08-08 02:55:12 -04:00
"description": "field.sumTotalTermFreq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 1,
2017-08-08 02:55:12 -04:00
"description": "term.docFreq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 2,
2017-08-08 02:55:12 -04:00
"description": "term.totalTermFreq",
"details": []
},
{
"value": 2.0,
"description": "doc.freq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 3,
2017-08-08 02:55:12 -04:00
"description": "doc.length",
"details": []
}
]
}
]
}
}
]
}
}
--------------------------------------------------
// TESTRESPONSE[s/"took": 12/"took" : $body.took/]
// TESTRESPONSE[s/OzrdjxNtQGaqs4DmioFw9A/$body.hits.hits.0._node/]
2018-06-05 02:51:00 -04:00
WARNING: While scripted similarities provide a lot of flexibility, there is
a set of rules that they need to satisfy. Failing to do so could make
Elasticsearch silently return wrong top hits or fail with internal errors at
search time:
- Returned scores must be positive.
- All other variables remaining equal, scores must not decrease when
`doc.freq` increases.
- All other variables remaining equal, scores must not increase when
`doc.length` increases.
You might have noticed that a significant part of the above script depends on
2017-08-08 02:55:12 -04:00
statistics that are the same for every document. It is possible to make the
above slightly more efficient by providing an `weight_script` which will
compute the document-independent part of the score and will be available
under the `weight` variable. When no `weight_script` is provided, `weight`
is equal to `1`. The `weight_script` has access to the same variables as
the `script` except `doc` since it is supposed to compute a
document-independent contribution to the score.
The below configuration will give the same tf-idf scores but is slightly
more efficient:
2019-09-06 11:31:13 -04:00
[source,console]
2017-08-08 02:55:12 -04:00
--------------------------------------------------
2019-01-22 09:13:52 -05:00
PUT /index
2017-08-08 02:55:12 -04:00
{
"settings": {
"number_of_shards": 1,
"similarity": {
"scripted_tfidf": {
"type": "scripted",
"weight_script": {
"source": "double idf = Math.log((field.docCount+1.0)/(term.docFreq+1.0)) + 1.0; return query.boost * idf;"
},
"script": {
"source": "double tf = Math.sqrt(doc.freq); double norm = 1/Math.sqrt(doc.length); return weight * tf * norm;"
}
}
}
},
"mappings": {
2019-01-22 09:13:52 -05:00
"properties": {
"field": {
"type": "text",
"similarity": "scripted_tfidf"
2017-08-08 02:55:12 -04:00
}
}
}
}
--------------------------------------------------
////////////////////
2019-09-06 11:31:13 -04:00
[source,console]
2017-08-08 02:55:12 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
PUT /index/_doc/1
2017-08-08 02:55:12 -04:00
{
"field": "foo bar foo"
}
2017-12-14 11:47:53 -05:00
PUT /index/_doc/2
2017-08-08 02:55:12 -04:00
{
"field": "bar baz"
}
2017-08-30 03:30:36 -04:00
POST /index/_refresh
2017-08-08 02:55:12 -04:00
2017-08-30 03:30:36 -04:00
GET /index/_search?explain=true
2017-08-08 02:55:12 -04:00
{
"query": {
"query_string": {
"query": "foo^1.7",
"default_field": "field"
}
}
}
--------------------------------------------------
// TEST[continued]
2019-09-06 16:09:09 -04:00
[source,console-result]
2017-08-08 02:55:12 -04:00
--------------------------------------------------
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
2018-12-05 13:49:06 -05:00
"total": {
"value": 1,
"relation": "eq"
},
2017-08-08 02:55:12 -04:00
"max_score": 1.9508477,
"hits": [
{
"_shard": "[index][0]",
"_node": "OzrdjxNtQGaqs4DmioFw9A",
"_index": "index",
2017-12-14 11:47:53 -05:00
"_type": "_doc",
2017-08-08 02:55:12 -04:00
"_id": "1",
"_score": 1.9508477,
"_source": {
"field": "foo bar foo"
},
"_explanation": {
"value": 1.9508477,
"description": "weight(field:foo in 0) [PerFieldSimilarity], result of:",
"details": [
{
"value": 1.9508477,
"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:",
"details": [
{
"value": 2.3892908,
"description": "weight",
"details": []
},
{
"value": 1.7,
"description": "query.boost",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 2,
2017-08-08 02:55:12 -04:00
"description": "field.docCount",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 4,
2017-08-08 02:55:12 -04:00
"description": "field.sumDocFreq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 5,
2017-08-08 02:55:12 -04:00
"description": "field.sumTotalTermFreq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 1,
2017-08-08 02:55:12 -04:00
"description": "term.docFreq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 2,
2017-08-08 02:55:12 -04:00
"description": "term.totalTermFreq",
"details": []
},
{
"value": 2.0,
"description": "doc.freq",
"details": []
},
{
2018-09-06 08:42:06 -04:00
"value": 3,
2017-08-08 02:55:12 -04:00
"description": "doc.length",
"details": []
}
]
}
]
}
}
]
}
}
--------------------------------------------------
// TESTRESPONSE[s/"took": 1/"took" : $body.took/]
// TESTRESPONSE[s/OzrdjxNtQGaqs4DmioFw9A/$body.hits.hits.0._node/]
////////////////////
Type name: `scripted`
2013-08-28 19:24:34 -04:00
[float]
2013-09-30 17:32:00 -04:00
[[default-base]]
2017-04-18 09:17:21 -04:00
==== Default Similarity
2013-08-28 19:24:34 -04:00
By default, Elasticsearch will use whatever similarity is configured as
2017-04-18 09:17:21 -04:00
`default`.
2013-08-28 19:24:34 -04:00
2016-11-01 16:14:20 -04:00
You can change the default similarity for all fields in an index when
it is <<indices-create-index,created>>:
2013-08-28 19:24:34 -04:00
2019-09-06 11:31:13 -04:00
[source,console]
2013-08-28 19:24:34 -04:00
--------------------------------------------------
2019-01-22 09:13:52 -05:00
PUT /index
2016-11-01 16:14:20 -04:00
{
"settings": {
"index": {
"similarity": {
"default": {
2018-04-03 10:45:25 -04:00
"type": "boolean"
2016-11-01 16:14:20 -04:00
}
}
}
}
}
--------------------------------------------------
If you want to change the default similarity after creating the index
2017-08-30 03:30:36 -04:00
you must <<indices-open-close,close>> your index, send the following
2016-11-01 16:14:20 -04:00
request and <<indices-open-close,open>> it again afterwards:
2019-09-06 11:31:13 -04:00
[source,console]
2016-11-01 16:14:20 -04:00
--------------------------------------------------
2017-08-30 03:30:36 -04:00
POST /index/_close
PUT /index/_settings
2016-11-01 16:14:20 -04:00
{
2017-08-30 03:30:36 -04:00
"index": {
"similarity": {
"default": {
2018-04-03 10:45:25 -04:00
"type": "boolean"
2016-11-01 16:14:20 -04:00
}
}
}
}
2017-08-30 03:30:36 -04:00
POST /index/_open
2013-08-28 19:24:34 -04:00
--------------------------------------------------
2017-08-30 03:30:36 -04:00
// TEST[continued]