Elasticsearch返回带有查询must_not不存在的文档

时间:2019-07-09 21:45:14

标签: elasticsearch

Elasticsearch:6.5.4

问题:我正在执行布尔查询(以下示例),在其中检查特定字段的存在。问题是,我要在字段确实存在但数组为空的地方获取结果。

我的问题是,如何正确执行查询并仅获得根本没有将nlp添加到文档中的结果。

示例查询:

{
    "size": 100,
    "sort": [{
        "publishedAt": {
            "order": "asc"
        }
    }],
    "_source": {
        "includes": ["nlp"]
    },
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "nlp.categories.gcp"
                }
            }
        }
    }
}

样本映射: (这是由Elastic Search自动创建的,但null_value除外,我尝试添加它)。

{
    "mapping": {
        "article": {
            "properties": {
                "nlp": {
                    "properties": {
                        "categories": {
                            "properties": {
                                "gcp": {
                                    "properties": {
                                        "confidence": {
                                            "type": "float"
                                        },
                                        "name": {
                                            "type": "text",
                                            "fields": {
                                                "keyword": {
                                                    "type": "keyword",
                                                    "null_value": "[]",
                                                    "ignore_above": 256
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

抽样结果:

{
    "took": 68,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1126581,
        "max_score": null,
        "hits": [
            {
                "_index": "news",
                "_type": "article",
                "_id": "UTuVmmsBE1H01hY9Rn6i",
                "_score": null,
                "_source": {
                    "nlp": {
                        "categories": {
                            "gcp": []
                        }
                    }
                },
                "sort": [
                    1509940860000
                ]
            },
            {
                "_index": "news",
                "_type": "article",
                "_id": "2w6PmmsBIpi-jAhhO13F",
                "_score": null,
                "_source": {
                    "nlp": {
                        "categories": {
                            "gcp": []
                        }
                    }
                },
                "sort": [
                    1510027260000
                ]
            }
        ]
    }
}

当nlp.categories.gcp中包含值时,典型的响应将如下所示。

{
    "took": 26,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 475690,
        "max_score": null,
        "hits": [
            {
                "_index": "news",
                "_type": "article",
                "_id": "6Q6JmmsBIpi-jAhhAlcm",
                "_score": null,
                "_source": {
                    "nlp": {
                        "categories": {
                            "gcp": [
                                {
                                    "confidence": 0.8999999761581421,
                                    "name": "/Travel/Hotels & Accommodations"
                                }
                            ]
                        }
                    }
                },
                "sort": [
                    1510215565000
                ]
            },
            {
                "_index": "news",
                "_type": "article",
                "_id": "rzunmmsBE1H01hY9sLyE",
                "_score": null,
                "_source": {
                    "nlp": {
                        "categories": {
                            "gcp": [
                                {
                                    "confidence": 0.9399999976158142,
                                    "name": "/Travel/Hotels & Accommodations"
                                }
                            ]
                        }
                    }
                },
                "sort": [
                    1510228881000
                ]
            }
        ]
    }
}

0 个答案:

没有答案