# Elasticsearch query on multiple fields with custom weight

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-on-multiple-fields-with-custom-weight/126141>\
**Category:** Elasticsearch\
**Created:** [March 29, 2018, 6:24pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-multiple-fields-with-custom-weight/126141 "2018-03-29T18:24:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnu-team8](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vishnu-team8](https://discuss.elastic.co/u/vishnu-team8)\
**Post date:** [March 29, 2018, 6:24pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-multiple-fields-with-custom-weight/126141/1 "2018-03-29T18:24:41Z")

</div>

> <https://stackoverflow.com/questions/49562698/elasticsearch-query-on-multiple-fields-with-custom-weight>

Let's say my index has name and skills.

```
{
   "name": "Vishnu Prasad",
   "skills": ["devops", "ruby"]
}
{
   "name": "Prasad",
   "skills": ["ruby"]
}
{
   "name": "Jonathan",
   "skills": ["devops"]
}
{
   "name": "Vishnu Prasad A D",
   "skills": ["devops", "nodejs"]
}
{
   "name": "Vishnu",
   "skills": ["devops"]
}
{
   "name": "Vishnu Dinakaran",
   "skills": ["devops"]
}

```

My criteria are,

| field | weight |
| --- | --- |
| name | 10 |
| --------------------- | ------------------ |
| skills | 9 |
| --------------------- | ------------------ |
| field3 | 8 |
| --------------------- | ------------------ |

When I search for `name: Vishnu Prasad` and `skill: devops`, the results should be based on the weight.

If both of the field matches, then it should come first. Also, the score should be more for phrase matches. i.e "Vishnu Prasad" should appear before "Vishnu".

I have written the following query but it is not giving me in the order that I need.

```
GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "name": {
                    "query": "Vishnu Prasad",
                  }
                }
              },
              {
                "match": {
                  "skills": {
                    "query": "devops",
                  }
                }
              }
            ]
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "name": {
              "query": "Vishnu Prasad",
              "boost": 10
            }
          }
        },
        {
          "match_phrase": {
            "skills": {
              "query": "devops",
              "boost": 9
            }
          }
        }
      ]
    }
  }
}

```

The order that I need is,

```
{
   "name": "Vishnu Prasad",
   "skills": ["devops", "ruby"]
}
{
   "name": "Vishnu Prasad A D",
   "skills": ["devops", "nodejs"]
}
{
   "name": "Vishnu Dinakaran",
   "skills": ["devops"]
}
{
   "name": "Vishnu",
   "skills": ["devops"]
}
{
   "name": "Jonathan",
   "skills": ["devops"]
}
{
   "name": "Prasad",
   "skills": ["ruby"]
}
```

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [April 7, 2018, 5:37pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-multiple-fields-with-custom-weight/126141/2 "2018-04-07T17:37:26Z")

</div>

Yes, ranking and scoring is a difficult problem. And to obtain the order that you need, you need to run a lot of experiments with different types and combinations of queries to see what works for you.  
Some tools and hints that can work in you case:

For something like this, I would use the query below:

> the score should be more for phrase matches. i.e "Vishnu Prasad" should appear before "Vishnu".  
> I would probably use `bool` should query:

```auto
{
  "query": {
    "bool" : {
      "should" : [
        {"match_phrase" : {"name" : "Vishnu Prasad"}},
        {"match" : {"name" : "Vishnu Prasad"}}
      ]
    }
  }
}

```

This will ensure if a doc contains a phrase, it will be scored higher.  
Then you can one such `should` query with your `name` field, another `should` query with your `skills` field, and then another higher level `should` query that will combine these two.

You may also consider experimenting with [dis max query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html) and [multi match query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html).

---

<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 5, 2018, 5:38pm UTC](https://discuss.elastic.co/t/elasticsearch-query-on-multiple-fields-with-custom-weight/126141/3 "2018-05-05T17:38:06Z")

</div>

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