# Не срабатывает exists

**URL:** <https://discuss.elastic.co/t/exists/70545>\
**Category:** Вопросы на русском языке\
**Created:** [January 4, 2017, 12:06pm UTC](https://discuss.elastic.co/t/exists/70545 "2017-01-04T12:06:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![intech](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/intech/32/9635_2.png) [@intech](https://discuss.elastic.co/u/intech)\
**Post date:** [January 4, 2017, 12:06pm UTC](https://discuss.elastic.co/t/exists/70545/1 "2017-01-04T12:06:11Z")

</div>

Всех с наступившем!

Сразу к делу.

Запрос

```auto
    POST /finances/finance/_search?pretty=&size=1
    {
      "query": {
        "bool": {
          "must_not": {
            "exists": {
              "field": "history"
            }
          }
        }
      }
    }

```

Результат:

```auto
    {
      "took": 117,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 184840,
        "max_score": 1,
        "hits": [
          {
            "_index": "finances",
            "_type": "finance",
            "_id": "W1-order_5742b1a7ae33e-1605230730471275825",
            "_score": 1,
            "_source": {
              "type": "in",
              "category": "WalletOne",
              "description": "Оплата через онлайн кошелек",
              "price": "1275",
              "status": false,
              "user": "AVS5jWxqfkkF9KhwRlnX",
              "history": [
                {
                  "type": "created",
                  "author": "AVS5jWxqfkkF9KhwRlnX",
                  "group": {
                    "_id": "abonents",
                    "name": "Абоненты"
                  },
                  "v": 1,
                  "changed": {
                    "type": "in",
                    "category": "WalletOne",
                    "description": "Оплата через онлайн кошелек",
                    "price": "1275",
                    "status": false,
                    "user": "AVS5jWxqfkkF9KhwRlnX"
                  },
                  "old": [],
                  "date": 1463988647
                }
              ]
            }
          }
        ]
      }
    }

```

mapping:

```auto
    {
      "finances": {
        "mappings": {
          "finance": {
            "properties": {
              "balance": {
                "type": "float",
                "null_value": 0
              },
              "category": {
                "type": "string",
                "index": "not_analyzed"
              },
              "description": {
                "type": "string",
                "index": "not_analyzed"
              },
              "history": {
                "type": "nested",
                "dynamic": "false",
                "properties": {
                  "author": {
                    "type": "string",
                    "index": "not_analyzed"
                  },
                  "changed": {
                    "type": "nested",
                    "dynamic": "true",
                    "properties": {
                      "balance": {
                        "type": "long"
                      },
                      "category": {
                        "type": "string"
                      },
                      "description": {
                        "type": "string"
                      },
                      "payment": {
                        "dynamic": "true",
                        "properties": {
                          "comment": {
                            "type": "string"
                          }
                        }
                      },
                      "price": {
                        "type": "string"
                      },
                      "status": {
                        "type": "boolean"
                      },
                      "type": {
                        "type": "string"
                      },
                      "user": {
                        "type": "string"
                      }
                    }
                  },
                  "date": {
                    "type": "date",
                    "format": "strict_date_optional_time||epoch_millis"
                  },
                  "group": {
                    "type": "nested",
                    "dynamic": "false",
                    "properties": {
                      "_id": {
                        "type": "string",
                        "index": "not_analyzed"
                      },
                      "name": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  },
                  "old": {
                    "type": "nested",
                    "dynamic": "true"
                  },
                  "type": {
                    "type": "string",
                    "index": "not_analyzed",
                    "null_value": "created"
                  },
                  "v": {
                    "type": "short",
                    "null_value": 1
                  }
                }
              },
              "payment": {},
              "price": {
                "type": "float",
                "null_value": 0
              },
              "status": {
                "type": "boolean",
                "null_value": false
              },
              "type": {
                "type": "string",
                "index": "not_analyzed"
              },
              "user": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          }
        }
      }
    }

```

---

<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 4, 2017, 1:05pm UTC](https://discuss.elastic.co/t/exists/70545/2 "2017-01-04T13:05:08Z")

</div>

`history` это не поле, поэтому по нему искать нельзя, вместо этого надо искать по полям, в которых есть данные, например `history.type`. Однако, поскольку `history` это объект с типом `nested`, надо использовать запрос с типом [`nested`](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-query.html):

```auto
POST /finances/finance/_search?pretty=&size=1
{
  "query": {
    "bool": {
      "must_not": {
        "nested": {
          "path": "history",
          "query": {
            "exists": {
              "field": "history.type"
            }
          }
        }
      }
    }
  }
}

```

---

<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 1, 2017, 1:08pm UTC](https://discuss.elastic.co/t/exists/70545/4 "2017-02-01T13:08:18Z")

</div>

Closed on February 1, 2017, 1:08pm UTC

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