# Query to get results based on priority of fields

**URL:** https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214
**Category:** Elasticsearch
**Created:** [June 15, 2022, 4:38am UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214 "2022-06-15T04:38:40Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![dawood\_abdullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawood_abdullah/32/57142_2.png) [@dawood\_abdullah](https://discuss.elastic.co/u/dawood_abdullah)
#### Post date: [June 15, 2022, 4:38am UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/1 "2022-06-15T04:38:40Z")

</div>

Hi,

I have following mapping :

```auto
> PUT products
> {
> "mappings" : {
> "properties" : {
> "item_name" : {"type" : "keyword"},
> "catalog_name" : {"type" : "keyword"},
> "group" : {"type" : "keyword"},
> "location" : {"type" : "keyword"},
> "price" : {"type" : "double"}
> }
> }
> }

```

and below are the docs of the products index

```auto
> POST /products/_bulk
> {"index":{}}
> {"item_name":"item1", "catalog" : "catalog1", "location" : "india", "price": 100}
> {"index":{}}
> {"item_name":"item2", "catalog" : "catalog1", "location" : "india", "price": 200}
> {"index":{}}
> {"item_name":"item1", "catalog" : "catalog1", "group" : "gold", "price": 90}

```

```auto

Item | Catalog | group | location | price
Item1 | 1.0 | - | india | 100
Item2 | 1.0 | - | india | 200
Item1 | 2.0 | gold | - | 90

```

following is my query:

```auto
GET /products/_search
{
  "query": {"bool": {"must": [
    {"boosting": 
      {
      "positive": {"term": {
        "group": {
          "value": "gold"
        }
        }},
      "negative": {"term": {
        "location": {
          "value": "india"
        }
      }},
      "negative_boost": 0.5
    }}
  ]}}
}

```

the above search query is resulting in:

```auto
Item | Catalog | group | location | price
Item1 | 2.0 | gold	| - | 90

```

but what I want is:

```auto
Item | Catalog | group | location | price
Item2 | 1.0 | - | india | 200
Item1 | 2.0 | gold	| - | 90

```

If the user belongs to gold group and lives in india, priority should be given to gold group item1 and then location.

Can anyone please let me know how to write a query for such a scenario? Thanks in advance.

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [June 15, 2022, 2:14pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/2 "2022-06-15T14:14:47Z")

</div>

Hi @dawood_abdullah !!

Provides more details like some docs, mapping and search term. That way it will be easier to help.

---

<div class="post-metadata">

### Author: ![dawood\_abdullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawood_abdullah/32/57142_2.png) [@dawood\_abdullah](https://discuss.elastic.co/u/dawood_abdullah)
#### Post date: [June 15, 2022, 8:52pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/3 "2022-06-15T20:52:55Z")

</div>

Hi andre, I have updated the question. Thank you.

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [June 15, 2022, 9:13pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/4 "2022-06-15T21:13:30Z")

</div>

Do you see about [Function Score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)?

Your query can be like this:

```auto
GET products/_search
{
  "collapse": {
    "field": "item_name"
  },
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "term": {
              "group": "gold"
            }
          },
          "weight": 50
        },
        {
          "filter": {
            "term": {
              "location": "india"
            }
          },
          "weight": 10
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![dawood\_abdullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawood_abdullah/32/57142_2.png) [@dawood\_abdullah](https://discuss.elastic.co/u/dawood_abdullah)
#### Post date: [June 15, 2022, 9:24pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/5 "2022-06-15T21:24:06Z")

</div>

function\_score query is resulting in all three records:

```auto
Item | Catalog | group | location | price
Item1 | 1.0 | - | india | 100
Item2 | 1.0 | - | india | 200
Item1 | 2.0 | gold | - | 90

```

instead we need only two records in this case, as Item1 in gold group is of priority than item1 of india location.

```auto
Item | Catalog | group | location | price
Item2 | 1.0 | - | india | 200
Item1 | 2.0 | gold | - | 90

```

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [June 15, 2022, 11:24pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/6 "2022-06-15T23:24:17Z")

</div>

I think it's kind of complicated for you to fetch only the Item2 and Item1 docs. I don't see an exclusion criteria for this doc:

```auto
Item | Catalog | group | location | price
Item1 | 1.0 | - | india | 100

```

The query I sent only impact the score of the documents, the query is not filtering documents. With functionScore, you must find the best way to make the documents you want more relevant.

---

<div class="post-metadata">

### Author: ![dawood\_abdullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawood_abdullah/32/57142_2.png) [@dawood\_abdullah](https://discuss.elastic.co/u/dawood_abdullah)
#### Post date: [June 16, 2022, 6:12am UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/7 "2022-06-16T06:12:06Z")

</div>

Thank you for your quick responses Andre.

Is there a way that I can say in the query that Item1 document of india and Item1 document of group are similar, and priortise **item1 of group** over **item1 of india** in the result?

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [June 16, 2022, 12:43pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/8 "2022-06-16T12:43:58Z")

</div>

Maybe you can give a greater weight to the docs that have the "groups" field and/or that are of the "gold" type.

And this case, wich doc must be priority?

```auto
Item | Catalog | group | location | price
Item1 | 1.0 | - | india | 100
Item2 | 1.0 | - | india | 200

```

---

<div class="post-metadata">

### Author: ![dawood\_abdullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dawood_abdullah/32/57142_2.png) [@dawood\_abdullah](https://discuss.elastic.co/u/dawood_abdullah)
#### Post date: [June 16, 2022, 1:31pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/9 "2022-06-16T13:31:16Z")

</div>

```auto
Item | Catalog | group | location | price
Item1 | 1.0 | - | india | 100
Item2 | 1.0 | - | india | 200

```

These are different items (item1, item2), so both will be returned as part of the result.

---

<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: [July 14, 2022, 1:31pm UTC](https://discuss.elastic.co/t/query-to-get-results-based-on-priority-of-fields/307214/10 "2022-07-14T13:31:39Z")

</div>

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