# Почему запросы возвращают разные данные?

**URL:** <https://discuss.elastic.co/t/topic/35277>\
**Category:** Вопросы на русском языке\
**Created:** [November 23, 2015, 7:40am UTC](https://discuss.elastic.co/t/topic/35277 "2015-11-23T07:40:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton\_Kotelev](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Anton\_Kotelev](https://discuss.elastic.co/u/Anton_Kotelev)\
**Post date:** [November 23, 2015, 7:40am UTC](https://discuss.elastic.co/t/topic/35277/1 "2015-11-23T07:40:31Z")

</div>

Всем привет. Используется ES версии 1.7.х.  
Создаю индекс с вот таким маппингом:

> put nestobj  
> {  
> "settings" : {  
> "number\_of\_shards" : 3,  
> "number\_of\_replicas" : 1  
> },  
> "mappings":  
> {  
> "mytype" :  
> {  
> "dynamic\_templates": [  
> {  
> "nested\_feature": {  
> "path\_match" : "_\_obj",  
> "mapping" : {  
> "type" : "nested"  
> }  
> }  
> },  
> {  
> "nested\_type\_template": {  
> "path\_match": "_\_obj.Type",  
> "match\_mapping\_type" : "string",  
> "mapping": {  
> "type" : "string",  
> "index" : "not\_analyzed"  
> }  
> }  
> },  
> {  
> "nested\_value\_template" : {  
> "path\_match" : "\*\_obj.Value",  
> "match\_mapping\_type" : "string",  
> "mapping" : {  
> "type" : "string",  
> "index" : "analyzed"  
> }  
> }  
> }  
> ],  
> "properties" : {  
> "\_id" : {  
> "type" : "string",  
> "index" : "not\_analyzed"  
> },  
> "product" : {  
> "type" : "string",  
> "index" : "analyzed"  
> },  
> "manufacturer" : {  
> "type" : "string",  
> "index" : "analyzed"  
> }  
> }  
> }  
> }  
> }

Основной особенностью является требование к динамическим полям: они должны создаваться с 2 "свойствами" - **Value** (анализ.) и **Type** (не анализ.). Далее добавляю данные:

> post nestobj/mytype/\_bulk  
> {"index":{"\_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde3"}}  
> {"product" : "myproduct\_1", "manufacturer" : "company\_1"}  
> {"index":{"\_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde4"}}  
> {"product" : "myproduct\_2", "manufacturer" : "company\_2"}  
> {"index":{"\_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde5"}}  
> {"product" : "myproduct\_3", "manufacturer" : "company\_1", "properties\_obj.Value" : "value\_for\_product\_3", "properties\_obj.Type" : "newtype"}  
> {"index":{"\_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde6"}}  
> {"product" : "myproduct\_4", "manufacturer" : "company\_2", "properties\_obj.Value" : "value\_for\_product\_4", "properties\_obj.Type" : "type"}  
> {"index":{"\_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde7"}}  
> {"product" : "myproduct\_5", "manufacturer" : "company\_1", "properties\_obj.Value" : "value\_for\_product\_3", "properties\_obj.Type" : "newtype", "settings\_obj.Value" : "settings\_for\_product\_5", "settings\_obj.Type" : "settings type"}

Если к этим данным делать запросы на поиск вида :

> get nestobj/mytype/\_search  
> {  
> "query": {  
> "term": {  
> "settings\_obj.Type": {  
> "value": "settings type"  
> }  
> }  
> }  
> }

> get nestobj/mytype/\_search?q=settings\_obj.Type:settings type

, то возвращаются разные наборы данных. На первый запрос вернутся все документы, даже не содержащие искомое значение. Второй запрос возвращает более корректный результат, но почему-то вместо одного документа вернулось 2. Учитывая, что поиск ведется по полю с типом индекса "_not\_analyzed_", не понятно - почему срабатывает документ без этого поля?

Как так получается?? Возможно я неправильно делаю **Mapping**? Тогда как он должен выглядеть под мои условия (динамические поля с 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:** [November 23, 2015, 3:35pm UTC](https://discuss.elastic.co/t/topic/35277/2 "2015-11-23T15:35:05Z")

</div>

Тут 3 проблемы:

1. Неправильно отформатирован запрос bulk. В версии 1.x `{"foo.bar": "baz"}` и `{"foo": {"bar": "baz"}}` - это два разных документа. Чтобы сделать `nested`, надо пользоваться вторым вариантом:

```auto
POST nestobj/mytype/_bulk
{"index":{"_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde3"}}
{"product" : "myproduct_1", "manufacturer" : "company_1"}
{"index":{"_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde4"}}
{"product" : "myproduct_2", "manufacturer" : "company_2"}
{"index":{"_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde5"}}
{"product" : "myproduct_3", "manufacturer" : "company_1", "properties_obj": {"Value" : "value_for_product_3", "Type" : "newtype"}}
{"index":{"_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde6"}}
{"product" : "myproduct_4", "manufacturer" : "company_2", "properties_obj": {"Value" : "value_for_product_4", "Type" : "type"}
{"index":{"_id":"768198f9-f61f-4fb0-a16e-3b56f1c4dde7"}}
{"product" : "myproduct_5", "manufacturer" : "company_1", "properties_obj": {"Value" : "value_for_product_3", "Type" : "newtype"}, "settings_obj": {"Value" : "settings_for_product_5", "Type" : "settings type"}}

```

1. Что бы искать объекты с типом `nested`, надо пользоваться специальным запросом с типом `nested`:

```auto
GET nestobj/mytype/_search
{
  "query": {
    "nested": {
      "path": "settings_obj",
      "query": {
        "term": {
          "settings_obj.Type": {
            "value": "settings type"
          }
        }
      }
    }
  }
}

```

1. Судя по документам и запросам в примере `nested` вам ни к чему. Возможно, просто плохой пример.

---

<div class="post-metadata">

**Author:** ![Anton\_Kotelev](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Anton\_Kotelev](https://discuss.elastic.co/u/Anton_Kotelev)\
**Post date:** [November 23, 2015, 6:35pm UTC](https://discuss.elastic.co/t/topic/35277/3 "2015-11-23T18:35:33Z")

</div>

Игорь, спасибо за ответы!  
По 3 пункту попробую выборку как с object, так и с nested.  
Еще раз спасибо за помощь!

---

<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 6, 2017, 1:50pm UTC](https://discuss.elastic.co/t/topic/35277/4 "2017-07-06T13:50:32Z")

</div>

Closed on July 6, 2017, 1:50pm UTC


