# Exist не находит документы с nested полем

**URL:** https://discuss.elastic.co/t/exist-nested/81323
**Category:** Вопросы на русском языке
**Created:** [April 5, 2017, 3:06pm UTC](https://discuss.elastic.co/t/exist-nested/81323 "2017-04-05T15:06:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![klara\_gogi](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@klara\_gogi](https://discuss.elastic.co/u/klara_gogi)
#### Post date: [April 5, 2017, 3:06pm UTC](https://discuss.elastic.co/t/exist-nested/81323/1 "2017-04-05T15:06:32Z")

</div>

Мой маппинг:

{  
"city": {  
"mappings": {  
"nms": {  
"properties": {  
"post": {  
"type": "long"  
},  
"area": {  
"type": "long"  
},  
"segm": {  
"type": "text"  
}  
}

Этот запрос не находит ни один документ:

GET products\_v19/\_search/  
{  
"query": {  
"exists" : { "field" : "segm" }  
}  
}

Почему?

И как совмещать вместе любой запрос с этим? Как раньше, когда был "filter"

---

<div class="post-metadata">

### Author: ![klara\_gogi](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@klara\_gogi](https://discuss.elastic.co/u/klara_gogi)
#### Post date: [April 5, 2017, 3:15pm UTC](https://discuss.elastic.co/t/exist-nested/81323/2 "2017-04-05T15:15:28Z")

</div>

Я написала вымышленный маппинг, и искомое поле nested. Это может быть проблемой?

Спасибо.

---

<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: [April 5, 2017, 3:41pm UTC](https://discuss.elastic.co/t/exist-nested/81323/3 "2017-04-05T15:41:22Z")

</div>

Вы не могли бы показать полный пример с созданием индекса, добавлением записи и поиском, который я мог бы просто скопировать, запустить и сразу увидеть в чем проблема?

---

<div class="post-metadata">

### Author: ![klara\_gogi](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@klara\_gogi](https://discuss.elastic.co/u/klara_gogi)
#### Post date: [April 6, 2017, 11:06am UTC](https://discuss.elastic.co/t/exist-nested/81323/4 "2017-04-06T11:06:21Z")

</div>

Создаю индекс:

```auto
PUT twit
{
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "nested"
        },
        "date":{
            "type":"text"
        }
      }
    }
  }
}

```

Документ:

```auto
 POST twit/1
{
    "tweet":{
        "message":{
            "op":"ewr",
            "ds":"ds"
        },
        "date":"ccv"
    }
}
POST twit/2
{
    "tweet":{
        "message":{
          "fdgd":"dgs",
            "ds":"ds"
        },
        "date":"ccv"
    }
}

```

Ответ:

```auto
{
   "_index": "twit",
   "_type": "1",
   "_id": "AVtC3Rt_iqMx7bItF5QU",
   "_version": 1,
   "result": "created",
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "created": true
}

```

```auto
GET twit/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "exists": {
          "field": "op"
        }
      },
      "boost": 1.2
    }
  }
}

```

Ответ:

```auto
{
  "took": 3939,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

```

Здесь я еще попробовала разные type, а в оригинале все документы лежат в одном.

---

<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: [April 6, 2017, 12:43pm UTC](https://discuss.elastic.co/t/exist-nested/81323/5 "2017-04-06T12:43:59Z")

</div>

Тут несколько проблем.

- `POST twit/1` создает документ с типом `1` и сгенерированным id
- `"tweet":{... }` на верхнем уровне не нужен, либо вам его надо добавить в меппинг.
- для поиска nested объектов надо использовать специальный запрос `nested`
- в 5.x `constant_score`

Со всеми этими

```auto
PUT twit
{
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "nested"
        },
        "date":{
            "type":"text"
        }
      }
    }
  }
}

PUT twit/tweet/1
{
  "message": {
    "op":"ewr",
    "ds":"ds"
  },
  "date":"ccv"
}

PUT twit/tweet/2
{
  "message":{
    "fdgd":"dgs",
    "ds":"ds"
  },
  "date":"ccv"
}

GET twit/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "nested": {
          "path": "message",
          "query": {
            "exists": {
              "field": "message.op"
            }
          }
        }
      },
      "boost": 1.2
    }
  }
}

```

Если у вас только один message на tweet, то он должен быть не `nested` а `object` и тогда пример будет выглядить так

```auto
PUT twit
{
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "object"
        },
        "date":{
            "type":"text"
        }
      }
    }
  }
}

PUT twit/tweet/1
{
  "message": {
    "op":"ewr",
    "ds":"ds"
  },
  "date":"ccv"
}

PUT twit/tweet/2
{
  "message":{
    "fdgd":"dgs",
    "ds":"ds"
  },
  "date":"ccv"
}

GET twit/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "exists": {
          "field": "message.op"
        }
      },
      "boost": 1.2
    }
  }
}

```

---

<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: [May 4, 2017, 12:44pm UTC](https://discuss.elastic.co/t/exist-nested/81323/6 "2017-05-04T12:44:33Z")

</div>

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