缺少ElasticSearch中嵌套属性的查询

时间:2017-06-23 09:52:59

标签: elasticsearch elasticsearch-5

我正在尝试编写一个查询,该查询返回所有不具有特定字段的文档,该字段是嵌套属性,但它不起作用。 我正在使用 ES 5.4

{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "shares"
          }
        }
      ]
    }
  }
}

我做错了什么?

这是我的映射

{
  "test": {
    "aliases": {},
    "mappings": {
      "vendor": {
        "properties": {
          "address": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "categories": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "displayTypes": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "emailId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "facebookAppId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "foursquareType": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "googleTypes": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "hours": {
            "properties": {
              "day": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "from": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "to": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "isOnBoarded": {
            "type": "boolean"
          },
          "kiaskCategories": {
            "type": "keyword"
          },
          "latitude": {
            "type": "float"
          },
          "location": {
            "type": "geo_point"
          },
          "longitude": {
            "type": "float"
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "phoneNumber": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "pictures": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "placeId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "price": {
            "type": "float"
          },
          "rating": {
            "type": "float"
          },
          "shares": {
            "type": "nested",
            "properties": {
              "_id": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "content": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "createdDate": {
                "type": "date"
              },
              "endDate": {
                "type": "date"
              },
              "facebookLikes": {
                "type": "long"
              },
              "images": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "price": {
                "type": "long"
              },
              "quantity": {
                "type": "long"
              },
              "shareType": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "startDate": {
                "type": "date"
              },
              "tags": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "source": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "state": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "timezone": {
            "type": "float"
          },
          "url": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "website": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "yelpTypes": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1496319860700",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "0S0A9YN-S-SrW4eeOHX06w",
        "version": {
          "created": "5040099"
        },
        "provided_name": "test"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

这应该有效 您已强制执行nested data type映射并使用nested query

{
  "query" : {
    "nested": {
      "path": "shares",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists" : {
                "field" : "shares._id"
              }
            }
          ]
        }
      }
    }
  }
}

注意:以下设置适用于我。

PUT test_index
{
  "mappings": {
    "document_type" : {
      "properties": {
        "name" : {
          "type": "text"
        },
        "shares" : {
          "type": "nested"
        }
      }
    }
  }
}

POST test_index/document_type
{
  "name" : "vicky"

}

POST test_index/_search
{
  "query": {
    "nested": {
      "path": "shares",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists" : {
                "field" : "shares.city"
              }
            }
          ]
        }
      }
    }

  }
}