# Добавление конкретного score если есть поле с конкретным значением

**URL:** <https://discuss.elastic.co/t/score/143453>\
**Category:** Вопросы на русском языке\
**Created:** [August 8, 2018, 8:28am UTC](https://discuss.elastic.co/t/score/143453 "2018-08-08T08:28:37Z")\
**Posts on this page:** 4\
**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, 8:28am UTC](https://discuss.elastic.co/t/score/143453/1 "2018-08-08T08:28:37Z")

</div>

Добрый день

есть документ с nested полем

> ```
> {
> "_id": 111
> "price": 1000
> "properties": [
> { "property_id": 22, "property_value": "газ"}
> { "property_id": 23, "property_value": "зеленый"}
> ]
> }
> 
> ```

У меня есть запрос который выдает мне ожидаемый score за price (от 1 до 100).  
Мне нужно добавить, +30 score если nested документ имеет param\_id == 23 AND param\_value == "красный". _НО_ при этом, если в документе нет этого nested документа, то все равно возвращать документ, просто без этих +30 score

Пытался делать:

> ```
> {
> "query": {
> "nested": {
> "path": "properties",
> "query": {
> "function_score": {
> "query": {
> "bool": { // ищу конкретный nested документ 
> "filter": [
> { "term": { "properties.property_id": 13 } },
> { "term": { "properties.property_value": "газированная" } }
> ]
> }
> },
> "script_score": {
> "script": {
> "source": "30" // за каждый найденный возвращаю 30 score
> }
> }
> }
> }
> }
> }
> },
> "sort": {
> "_score": {
> "order": "desc"
> }
> }
> }
> Результат: search_phase_execution_exception
> 
> ```

Пытался:

> ```
> {
> "query": {
> "bool": {
> "must": [
> [
> {
> "nested": {
> "path": "properties",
> "query": [
> {
> "function_score": {
> "functions": [
> {
> "weight": 30 // +30 score за найденный документ
> "filter": [ // ищу конкретный nested документ
> {
> "term": {
> "properties.property_id": 13
> }
> },
> {
> "term": {
> "properties.property_value": "газированная"
> }
> }
> ]
> }
> ]
> }
> }
> ]
> }
> }
> ]
> ]
> }
> },
> "sort": {
> "_score": {
> "order": "desc"
> }
> }
> }
> Результат: [_na] query malformed, must start with start_object
> 
> ```

Пытался:

> ```
> {
> "query": {
> "bool": {
> "must": [
> [
> {
> "nested": {
> "path": "properties",
> "query": [
> {
> "constant_score": {
> "filter": [ // ищем конкретный документ
> {
> "term": {
> "properties.property_id": 117
> }
> },
> {
> "term": {
> "properties.property_value": "вода"
> }
> }
> ],
> "boost": 3 // даем +30 score за найденный результат
> }
> }
> ]
> }
> }
> ]
> ]
> }
> },
> "sort": {
> "_score": {
> "order": "desc"
> }
> }
> }
> Результат: возвращает документы где property_id == 117 AND property_value == "вода", но добавляет только 1 score в не зависимости от boost
> 
> ```

Заранее благодарен

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 8, 2018, 2:34pm UTC](https://discuss.elastic.co/t/score/143453/2 "2018-08-08T14:34:00Z")

</div>

```auto
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": "газ"},
    { "property_id": 23, "property_value": "зеленый"}
  ]
}

PUT test/doc/112
{
  "price": 1000,
  "properties": [
    { "property_id": 12, "property_value": "вода"},
    { "property_id": 13, "property_value": "газированная"}
  ]
}

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": 13
                      }
                    },
                    {
                      "term": {
                        "properties.property_value": "газированная"
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        {
          "weight": 0
        }
      ],
      "boost_mode": "sum",
      "score_mode": "first"
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}

```

---

<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:08am UTC](https://discuss.elastic.co/t/score/143453/3 "2018-08-09T08:08:05Z")

</div>

Вы спасли меня 0\_0  
Спасибо! )

---

<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:08am UTC](https://discuss.elastic.co/t/score/143453/4 "2018-09-06T08:08:16Z")

</div>

Closed on September 6, 2018, 8:08am UTC

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