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

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

Сразу к делу.

Запрос

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

Результат:

    {
      "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:

    {
      "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"
              }
            }
          }
        }
      }
    }

history это не поле, поэтому по нему искать нельзя, вместо этого надо искать по полям, в которых есть данные, например history.type. Однако, поскольку history это объект с типом nested, надо использовать запрос с типом nested:

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

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