可能是一个非常愚蠢的问题,检查elasticsearch中的文档字段是否存在的最佳方法是什么?我在文档中找不到任何内容。
例如,如果此文档没有字段/键“价格”,我不想在结果中返回。
{“更新”:“2015/09/17 11:27:27”,“名称”:“眼影”,“格式”:“1.5克/0.05盎司”,}
我可以做什么?
谢谢
您可以将exists
filter与bool/must
filter结合使用,如下所示:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "price"
}
},
... <-- your other constraints, if any
]
}
}
}
}
}
已弃用(自ES5起)您也可以将missing
filter与bool/must_not
filter结合使用:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must_not": [
{
"missing": {
"field": "price"
}
}
]
}
}
}
}
}
exists
过滤器已经被ES 2.1中的exists
查询所取代,尽管它的工作原理是相同的。此外,删除了丢失的过滤器,并且不再使用缺少的查询。
要获得具有特定字段的所有文档,
"bool": {
"must": {
"exists": {
"field": "my_field"
}
}
}
并获得所有没有特定字段的文档,像这样使用must_not
"bool": {
"must_not": {
"exists": {
"field": "my_field"
}
}
}
弹性文档:https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-missing-query.html
您可以使用exists过滤器:
{
"query": {
"filtered": {
"filter": {
"exists": {
"field": "status"
}
},
"query": {
"match_all": {}
}
}
}
}
此致,阿兰
你可以直接做
{
"query": {
"exists": {
"field": "fieldName"
}
}
}
如果你想添加一些比赛,那么你可以去
{
"query": {
"bool": {
"must": [{
"match": {
"fieldName": "value"
}
},
{
"exists": {
"field": "fieldName"
}
}]
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
GET /_search
{
"query": {
"exists" : { "field" : "price" }
}
}
来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html