# Странное поведение inner\_hits при работе с has\_child

**URL:** <https://discuss.elastic.co/t/inner-hits-has-child/164627>\
**Category:** Вопросы на русском языке\
**Created:** [January 17, 2019, 12:08pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627 "2019-01-17T12:08:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 17, 2019, 12:08pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/1 "2019-01-17T12:08:51Z")

</div>

Здравствуйте! Подскажите, пожалуйста, я наткнулся на странное поведение при работе с has\_child запросом. Дело в том, что в некоторых запросах у меня оказывается пустым массив inner\_hits.hits.

Например, в запросе ниже, прилетает ответ с пустым inner\_hits, а если убрать один элемент из should верхнего уровня, inner\_hits будет заполнен.

Могли бы вы пояснить такое поведение? Версия ES 6.4.3

```
{
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "filter": [
                                        {
                                            "has_child": {
                                                "type": "document",
                                                "inner_hits": {
                                                    "size": 1
                                                },
                                                "query": {
                                                    "bool": {
                                                        "should": [
                                                            {
                                                                "span_near": {
                                                                    "clauses": [
                                                                        {
                                                                            "span_multi": {
                                                                                "match": {
                                                                                    "prefix": {
                                                                                        "fileContent": {
                                                                                            "value": "молок",
                                                                                            "rewrite": "top_terms_4096"
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        },
                                                                        {
                                                                            "span_multi": {
                                                                                "match": {
                                                                                    "prefix": {
                                                                                        "fileContent": {
                                                                                            "value": "натур",
                                                                                            "rewrite": "top_terms_4096"
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    ],
                                                                    "slop": 10,
                                                                    "in_order": false
                                                                }
                                                            }
                                                        ]
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            },
                            { ***** }
                        ]
                    }
                }
            ]
        }
    }
}

```

Ответ:

```
{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 6,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "tenders_2019",
        "_type": "_doc",
        "_id": "tender_33058543",
        "_score": null,
        "sort": [
          1547746740000
        ],
        "inner_hits": {
          "document": {
            "hits": {
              "total": 0,
              "max_score": null,
              "hits": []
            }
          }
        }
      },
      {
        "_index": "tenders_2019",
        "_type": "_doc",
        "_id": "tender_33059452",
        "_score": null,
        "sort": [
          1547719620000
        ],
        "inner_hits": {
          "document": {
            "hits": {
              "total": 4,
              "max_score": 634.7183,
              "hits": [
                {
                  "_index": "tenders_2019",
                  "_type": "_doc",
                  "_id": "document_77837537",
                  "_score": 634.7183,
                  "_routing": "tender_33059452",
                  "_source": {
                    "docId": 156399288,
                    "document": {
                      "name": "document",
                      "parent": "tender_33059452"
                    },
                    "name": "b3931cf5-233b-417d-9113-e7be5957b779.docx",
                    "contentLength": 9021,
                    "id": 77837537,
                    "source": "РАЗДЕЛ II СПЕЦИФИКАЦИЯ молочные продукты.docx"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
```

---

<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:** [January 17, 2019, 1:12pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/2 "2019-01-17T13:12:35Z")

</div>

А маппинг как выглядит для этих полей?

---

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 17, 2019, 1:16pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/3 "2019-01-17T13:16:29Z")

</div>

Вот маппинг полей по которые должны попадать в inner\_hits:

```
"docId": {
          "type": "long"
        },
        "name": {
          "type": "text",
          "index": false
        },
        "source": {
          "type": "text",
          "index": false
        },
        "fileContent": {
          "type": "text",
          "analyzer": "russian",
          "store": true
        },
        "contentLength": {
          "type": "integer"
        }
```

---

<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:** [January 17, 2019, 1:39pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/4 "2019-01-17T13:39:14Z")

</div>

> [@IvanTushin](#):
>
> Например, в запросе ниже, прилетает ответ с пустым inner\_hits, а если убрать один элемент из should верхнего уровня, inner\_hits будет заполнен.

И как при этом выглядит результат с `"_id": "tender_33058543",`? И есть ли он там? Я так думаю, что его там не будет, а появляется он из-за того, что он совпадает с частью в ` { ***** }` а не с `has_child`.

---

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 17, 2019, 2:01pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/5 "2019-01-17T14:01:51Z")

</div>

Полную версию запросов выложил сюда [https://gist.github.com/tushinivan/7c689b18ecc4331a3b66f644fd3e8ac2](https://gist.github.com/tushinivan/7c689b18ecc4331a3b66f644fd3e8ac2)

Там 2 запроса и 2 результата выполнения.

---

<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:** [January 17, 2019, 2:10pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/6 "2019-01-17T14:10:34Z")

</div>

А вы не могли бы привести полный пример с установками, маппингом и данными, который я мог бы в кибане запустить и посмотреть что там конкретно происходит? Иначе, слишком много предположений надо делать.

---

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 17, 2019, 2:37pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/7 "2019-01-17T14:37:00Z")

</div>

Хорошо, сделаю. Это займет некоторое время.

---

<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:** [January 17, 2019, 2:41pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/8 "2019-01-17T14:41:15Z")

</div>

Есть еще одна теория, которую можно быстро проверить. Вы не могли бы попробовать заменить все `"bool": { "filter": [` на `"bool": { "must": [` и посмотреть не изменит-ли это ситуацию.

---

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 17, 2019, 3:47pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/9 "2019-01-17T15:47:53Z")

</div>

Нет, это никак не повлияло на результат..

---

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 17, 2019, 5:00pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/10 "2019-01-17T17:00:48Z")

</div>

Создал файл для воспроизведения в кибане. На тестовом индексе проблема воспроизводится. Ссылку на документ отправил личным письмом.

---

<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:** [January 17, 2019, 6:49pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/11 "2019-01-17T18:49:23Z")

</div>

Спасибо. Похоже на баг. Я упростил ваш пример и открыл [https://github.com/elastic/elasticsearch/issues/37584](https://github.com/elastic/elasticsearch/issues/37584)

Не уверен, как с этим бороться пока баг не исправили. Надо как-то объединить все `has_child` в один, но с вашим запросом это нетривиально.

---

<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:** [January 17, 2019, 7:27pm UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/12 "2019-01-17T19:27:15Z")

</div>

Оказалось, все просто. Надо всего-лишь дать этим `inner_hits` разные имена: [https://gist.github.com/imotov/8ba51253b94296d0ef562a94e12f10cc/revisions](https://gist.github.com/imotov/8ba51253b94296d0ef562a94e12f10cc/revisions)

---

<div class="post-metadata">

**Author:** ![IvanTushin](https://avatars.discourse-cdn.com/v4/letter/i/dbc845/32.png) [@IvanTushin](https://discuss.elastic.co/u/IvanTushin)\
**Post date:** [January 18, 2019, 5:14am UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/13 "2019-01-18T05:14:09Z")

</div>

Спасибо!! Это помогло.

---

<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:** [February 15, 2019, 5:14am UTC](https://discuss.elastic.co/t/inner-hits-has-child/164627/14 "2019-02-15T05:14:13Z")

</div>

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