# Yii2 and nested objects

**URL:** <https://discuss.elastic.co/t/yii2-and-nested-objects/282268>\
**Category:** Elasticsearch\
**Created:** [August 23, 2021, 5:47pm UTC](https://discuss.elastic.co/t/yii2-and-nested-objects/282268 "2021-08-23T17:47:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Web\_Hildus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/web_hildus/32/93586_2.png) [@Web\_Hildus](https://discuss.elastic.co/u/Web_Hildus)\
**Post date:** [August 23, 2021, 5:47pm UTC](https://discuss.elastic.co/t/yii2-and-nested-objects/282268/1 "2021-08-23T17:47:23Z")

</div>

Hi. I'm totally new in ElasticSearch. I'm using "Yii2 Framework" and stalked on. Elastic not works with nested field. Is here specialist in Yii2 & Elasticsearch? Or where I can find the answer? Documents in index are like this:

```auto
{
  "id" : 28,
  "name" : "Futura Door",
  "description" : "Futura Door",
  "code" : "7339",
  "price" : 14924,
  "values" : {
    "type" : "nested",
    "properties" : [
      {
        "characteristic" : 1,
        "value_string" : "Grey",
        "value_int" : 0
      },
      {
        "characteristic" : 2,
        "value_string" : "Futura",
        "value_int" : 0
      }
    ]
  }
}

```

Query is made with framework like this:

```auto
    'query' => [
        'bool' => [
            'must' => array_merge(
                array_filter([
                    !empty($form->text) ? ['multi_match' => [
                        'query' => $form->text,
                        'fields' => ['name^2', 'description']
                    ]] : false,
                ]),
                array_map(function (Value $value) {
                    return ['nested' => [
                        'path' => 'values',
                        'query' => [
                            'bool' => [
                                'must' => array_filter([
                                    ['match' => ['values.characteristics' => $value->getId()]],
                                    !empty($value->equal) ? ['match' => ['values.value_string' => $value->equal]] : false,
                                    !empty($value->from) ? ['range' => ['values.value_int' => ['gte' => $value->from]]] : false,
                                    !empty($value->to) ? ['range' => ['values.value_int' => ['lte' => $value->to]]] : false,
                                ]),
                            ],
                        ],
                    ]];
                }, array_filter($form->values, function (ValueForm $value) {
                    return $value->isFilled();
                }))
            )
        ],
    ],

```

---

<div class="post-metadata">

**Author:** ![Web\_Hildus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/web_hildus/32/93586_2.png) [@Web\_Hildus](https://discuss.elastic.co/u/Web_Hildus)\
**Post date:** [August 25, 2021, 4:58pm UTC](https://discuss.elastic.co/t/yii2-and-nested-objects/282268/2 "2021-08-25T16:58:10Z")

</div>

Decision were very simple. I had to add path 'product' before values

```auto
array_map(function (ValueForm $value) {
                                        return [
                                            'nested' => [
                                                'path' => 'product.values',
                                                'query' => [
                                                    'bool' => [
                                                        'must' => array_filter([
                                                            ['match' => ['product.values.characteristic' => $value->getId()]],
                                                            !empty($value->equal) ? ['match' => ['product.values.value_string' => $value->equal]] : false,
                                                            !empty($value->from) ? ['range' => ['product.values.value_int' => ['gte' => $value->from]]] : false,
                                                            !empty($value->to) ? ['range' => ['product.values.value_int' => ['lte' => $value->to]]] : false,
                                                        ]),
                                                    ],
                                                ],
                                            ]];
                                    }, array_filter($form->values, function (ValueForm $value) {
                                        return $value->isFilled();
                                    })),

```

---

<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 22, 2021, 4:58pm UTC](https://discuss.elastic.co/t/yii2-and-nested-objects/282268/3 "2021-09-22T16:58:39Z")

</div>

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