# Adding a specific score if has field with a specific value (nested)

**URL:** <https://discuss.elastic.co/t/adding-a-specific-score-if-has-field-with-a-specific-value-nested/143460>\
**Category:** Elasticsearch\
**Created:** [August 8, 2018, 9:14am UTC](https://discuss.elastic.co/t/adding-a-specific-score-if-has-field-with-a-specific-value-nested/143460 "2018-08-08T09:14:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ConConovaloff](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/conconovaloff/32/34258_2.png) [@ConConovaloff](https://discuss.elastic.co/u/ConConovaloff)\
**Post date:** [August 8, 2018, 9:14am UTC](https://discuss.elastic.co/t/adding-a-specific-score-if-has-field-with-a-specific-value-nested/143460/1 "2018-08-08T09:14:17Z")

</div>

Hello

I have document with nested documents:

> ```
> {
> "_id": 111
> "price": 1000
> "properties": [
> { "property_id": 22, "property_value": "gas"}
> { "property_id": 23, "property_value": "green"}
> ]
> }
> 
> ```

I have a request that gives me the expected score by the price (from 1 to 100).  
I need to add a +30 score if the nested document has a param\_id == 23 AND param\_value == "red". _However_, if the document doesn't have this nested document, then still return the document, just without these +30 score (can't just add filter that will delete the result without specific value)

I try:

> ```
> {
> "query": {
> "nested": {
> "path": "properties",
> "query": {
> "function_score": {
> "query": {
> "bool": { // search specific nested document
> "filter": [
> { "term": { "properties.property_id": 23 } },
> { "term": { "properties.property_value": "red" } }
> ]
> }
> },
> "script_score": {
> "script": {
> "source": "30" // if find nested document, then add 30 score
> }
> }
> }
> }
> }
> }
> },
> "sort": {
> "_score": {
> "order": "desc"
> }
> }
> }
> 
> ```

result: search\_phase\_execution\_exception

I try:

> ```
> {
> "query": {
> "bool": {
> "must": [
> [
> {
> "nested": {
> "path": "properties",
> "query": [
> {
> "function_score": {
> "functions": [
> {
> "weight": 30 // if find nested document, then add 30 score
> "filter": [ // search specific nested document
> {
> "term": {
> "properties.property_id": 23
> }
> },
> {
> "term": {
> "properties.property_value": "red"
> }
> }
> ]
> }
> ]
> }
> }
> ]
> }
> }
> ]
> ]
> }
> },
> "sort": {
> "_score": {
> "order": "desc"
> }
> }
> }
> 
> ```

result: [\_na] query malformed, must start with start\_object

I try:

> ```
> {
> "query": {
> "bool": {
> "must": [
> [
> {
> "nested": {
> "path": "properties",
> "query": [
> {
> "constant_score": {
> "filter": [ // search specific nested document
> {
> "term": {
> "properties.property_id": 23
> }
> },
> {
> "term": {
> "properties.property_value": "red"
> }
> }
> ],
> "boost": 30 // if find nested document, then add 30 score
> }
> }
> ]
> }
> }
> ]
> ]
> }
> },
> "sort": {
> "_score": {
> "order": "desc"
> }
> }
> }
> 
> ```

result: return document where has property\_id == 23 AND property\_value == "red", but add 1 score, and no dependence on boost

---

<div class="post-metadata">

**Author:** ![ConConovaloff](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/conconovaloff/32/34258_2.png) [@ConConovaloff](https://discuss.elastic.co/u/ConConovaloff)\
**Post date:** [August 9, 2018, 8:10am UTC](https://discuss.elastic.co/t/adding-a-specific-score-if-has-field-with-a-specific-value-nested/143460/2 "2018-08-09T08:10:58Z")

</div>

I'm have solution by Igor Motov:

```
PUT test
{
  "mappings": {
    "doc": {
      "properties": {
        "properties": {
          "type": "nested",
          "properties": {
            "property_id": {
              "type": "integer"
            },
            "property_value": {
              "type": "keyword"
            }
          }
          
        },
        "price": {
          "type": "integer"
        }
      }
    }
  }  
}

PUT test/doc/111
{
  "price": 1000,
  "properties": [
    { "property_id": 22, "property_value": "gas"},
    { "property_id": 23, "property_value": "green"}
  ]
}

PUT test/doc/112
{
  "price": 900,
  "properties": [
    { "property_id": 12, "property_value": "whater"},
    { "property_id": 23, "property_value": "red"}
  ]
}

GET test/doc/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "30"
            }
          },
          "filter": {
            "nested": {
              "path": "properties",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "term": {
                        "properties.property_id": 23
                      }
                    },
                    {
                      "term": {
                        "properties.property_value": "red"
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        {
          "weight": 0
        }
      ],
      "boost_mode": "sum",
      "score_mode": "first"
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}
```

---

<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:** [September 6, 2018, 8:11am UTC](https://discuss.elastic.co/t/adding-a-specific-score-if-has-field-with-a-specific-value-nested/143460/3 "2018-09-06T08:11:02Z")

</div>

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