# How to Boost two fields

**URL:** <https://discuss.elastic.co/t/how-to-boost-two-fields/178994>\
**Category:** Elasticsearch\
**Created:** [April 29, 2019, 10:04pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994 "2019-04-29T22:04:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jilusan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilusan/32/41515_2.png) [@jilusan](https://discuss.elastic.co/u/jilusan)\
**Post date:** [April 29, 2019, 10:04pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/1 "2019-04-29T22:04:50Z")

</div>

Hi,

I would like to boost 'title' and 'description' fields on query search, and leave all the other fields (I have many) with boost 1. So I did:

```
GET index1/_doc/_search
{
  "query": {
    "query_string": {
      "query": "web",
      "fields": [
        "title^4",
        "description^2"
      ]
    }    
  }
}

```

Suppose 'web' is in another field than 'title' and 'description', when I do a search , these documents are not listed.

Did I do something wrong?

Thanks.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 1, 2019, 11:51am UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/2 "2019-05-01T11:51:37Z")

</div>

One option would be to use the [copy\_to](https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html) mapping option to copy all those other fields to a single field called "everything\_else" and just add that to your list of fields in the above example.

This has other benefits - searching across many fields using their own independent indexes often produces counterintuitive results. Lucene likes to pick the bizarrest context for a search term because it likes rarity. Searching for books on "web development" for example would rank the document "author:gary web" first because of the typo for the common name `Webb`. You can see that in this example:

```
DELETE test
PUT test
{
  "settings": {
	"number_of_replicas": 0,
	"number_of_shards": 1
  },
  "mappings": {
	"_doc": {
	  "properties": {
		"title": {
		  "type": "text"
		},
		"description": {
		  "type": "text"
		},
		"everything_else": {
		  "type": "text"
		},
		"author": {
		  "type": "text",
		  "copy_to":"everything_else"
		},
		"tags": {
		  "type": "keyword",
		  "copy_to":"everything_else"
		}
	  }
	}
  }
}
POST test/_doc/_bulk
{ "index":{} }
{"title": "web dev", "description":"html,css etc", "author":"gary webb", "tags":["html","css"]}
{ "index":{} }
{"title": "web apps", "description":"tomcat etc", "author":"paul smith", "tags":["tomcat"]}
{ "index":{} }
{"title": "web etiquette", "description":"etiquette", "author":"sue jones", "tags":["morals"]}
{ "index":{} }
{"title": "Cooking", "description":"Asian cuisine", "author":"gary web", "tags":["food"]}

GET test/_doc/_search?explain=true
{
  "query": {
	"query_string": {
	  "query": "web"
	}    
  }
}

```

The solution is to reach for the special `cross_fields` scoring strategy or perhaps more simply to reindex then search the `everything_else` copy\_to field. This blends the stats from author names, titles etc and avoids some of these odd ranking problems.

---

<div class="post-metadata">

**Author:** ![jilusan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilusan/32/41515_2.png) [@jilusan](https://discuss.elastic.co/u/jilusan)\
**Post date:** [May 1, 2019, 1:20pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/3 "2019-05-01T13:20:17Z")

</div>

Thanks for your solution.

But how to boost title and description fields?

When I try:

```
GET test/_doc/_search?explain=true
{
  "query": {
	"query_string": {
	  "query": "web",
      "fields": [
        "title^4",
        "description^2"
      ]
	  }    
  }
}

```

I get 3 for total of documents searched. And I would like to have 4 instead.

```
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.347925,
    "hits" : [....

```

.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 1, 2019, 1:50pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/4 "2019-05-01T13:50:31Z")

</div>

> [@jilusan](#):
>
> But how to boost title and description fields?

They already are in your example?

> [@jilusan](#):
>
> I get 3 for total of documents searched. And I would like to have 4 instead.

Don't forget to add the "everything\_else" field with the lower boost:

```
GET test/_doc/_search
{
  "query": {
	"query_string": {
	  "query": "web",
		"fields": [
		"title^4",
		"description^2",
		"everything_else^1"
	  ]      
	}    
  }
}

```

---

<div class="post-metadata">

**Author:** ![jilusan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilusan/32/41515_2.png) [@jilusan](https://discuss.elastic.co/u/jilusan)\
**Post date:** [May 1, 2019, 2:39pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/5 "2019-05-01T14:39:03Z")

</div>

I found another solution which gives me the solution:

```
GET test/_doc/_search?explain=true
{
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "web"
        }
      },
      "should": [
        {
          "query_string": {
            "query": "web",
            "fields": [
              "title^4",
              "description^2",
            ]
          } 
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 1, 2019, 2:52pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/6 "2019-05-01T14:52:17Z")

</div>

It may work with these 4 example docs but the more docs you add the rarer that `author:web` term becomes. The rarer it becomes the higher it scores and at some point you may see that the typo `author:web` ranks higher than your attempts to boost titles and descriptions.

Your boosts and IDF (term rarity) are _both_ factors in ranking.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 29, 2019, 3:01pm UTC](https://discuss.elastic.co/t/how-to-boost-two-fields/178994/7 "2019-05-29T15:01:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
