In the previous section, we talked about boosting queries. This type of boosting is very handy and powerful and fulfills its role in most situations. However, there is one case where the more convenient way is to use the index-time boosting. This situation is where important documents are a part of input data. We gain a boost independent from a query at the cost of re-indexing, when the boost value is changed. In addition to that, the performance is slightly better because some parts needed in the boosting process are already calculated at index time. ElasticSearch stores information about the boost as a part of normalization information. This is important because if we set omit_norms
to true
, we can't use index-time boosting.
Let's look at the typical document definition:
{ "title" : "The Complete Sherlock Holmes", "author" : "Arthur Conan Doyle", "year" : 1936 }
If we want to boost the author
field for this particular document, the structure should be slightly changed and should look like the following:
{ "title" : "The Complete Sherlock Holmes", "author" : { "_value" : "Arthur Conan Doyle", "_boost" : 10.0 }, "year": 1936 }
As you've seen, field boosting during indexing is simple. In my opinion, the more useful way is to boost a whole document. For example, we want to promote some items in our shop application. Another example is when we desire to improve search relevance. Sometimes, we note that statistically people are more likely to seek popular products, so there is a lot of sense to boost popular products a bit by placing them at a higher level in the result list. How to do it? Let's look at following example:
{
"title" : "The Complete Sherlock Holmes",
"author" : "Arthur Conan Doyle",
"year" : 1936,
"_boost" : 10.0
}
As you can see, this is as simple as it can be. We've just added a new field named _boost
. ElasticSearch will automatically apply its value as a document boost.
It is worth mentioning that it is possible to directly define a field's boost in our mappings. The following example shows that:
{ "mappings" : { "book" : { "properties" : { "title" : { "type" : "string" }, "author" : { "type" : "string", "boost" : 10.0 } } } } }
Thanks to the preceding boost, all queries will favor values found in the field named author
. This also applies to queries using the _all
field.