在elasticsearch中_search查询结果中的total值显示什么?

hat*_*lla 4 elasticsearch

当我们调用elasticsearch时,如下所示:\nPOST https:////_search with body:

\n\n
{\n      "from": 0,\n      "size": 1,\n      "query": {\n        "bool": {\n          "must": [\n            {\n              "range": {\n                "createdAt": {\n                  "gt": "2019-11-11T10:00:00"\n                }\n              }\n            }\n\n          ]\n        }\n      },\n      "sort": [\n        {\n            "createdAt" : {\n                "order" : "desc"\n            }\n        }\n        ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我看到我只得到 1 个结果,因为分页设置为 1,但响应中的内部点击总数显示 2。这是我得到的响应:

\n\n
{\n    "took": 4,\n    "timed_out": false,\n    "_shards": {\n        "total": 5,\n        "successful": 5,\n        "skipped": 0,\n        "failed": 0\n    },\n    "hits": {\n        "total": {\n            "value": 2,\n            "relation": "eq"\n        },\n        "max_score": null,\n        "hits": [\n            {\n                "_index": \xe2\x80\x9c<index-name>\xe2\x80\x9d,\n                "_type": "_doc",\n                "_id": "5113c843-dff3-499f-a12e-44c7ac103bcf_0",\n                "_score": null,\n                "_source": {\n                    "oId": "5113c843-dff3-499f-a12e-44c7ac103bcf",\n                    "oItemId": 0,\n                    "createdAt": "2019-11-13T11:00:00"\n                },\n                "sort": [\n                    1573642800000\n                ]\n            }\n        ]\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\x99t 总计是否\xe2\x80\x99t 捕获分页部分?而且它只关心查询报告?它应该显示与查询匹配的项目总数,而不考虑分页集,对吧?

\n

Ami*_*wal 7

是的,你是对的,total 不捕获分页部分,只关心查询报告,即。无论文档的总数与给定查询匹配。

准确的说是ES官方文档中解释的。

Total(对象)有关返回文档数量的元数据。返回的参数包括:

value:返回的文档总数。关系:表示是否返回的文档数量。返回值是:

eq:准确的 gte:下界,包括返回的文档

这意味着它返回的文档总数,但由于在您的示例中分页设置为 1,内部命中只有 1 个文档。您可以通过创建如下示例示例来轻松交叉检查这种理解:

创建一个仅包含 1 个文本字段的示例索引:

URL:- http://localhost:9200/{your-index-name}/ --> PUT method

{
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            }
        }
    },
    "settings": {
        "index": {
            "number_of_shards": "1",
            "number_of_replicas": "1"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

创建上述索引后,将索引以下 4 个文档:

网址:-http://localhost:9200/{your-index-name}/_doc/{1,2,like..} --> POST method

  {
        "name": "foo 1"
    }

{
    "name": "foo bar"
}

{
    "name": "foo"
}

{
    "name": "foo 2"
}
Run Code Online (Sandbox Code Playgroud)

现在,当您点击以下不分页的搜索查询时:

{

    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "foo"
                    }
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出以下响应:

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4, --> Note 4 here
            "relation": "eq"
        },
        "max_score": 0.12199639,
        "hits": [
            {
                "_index": "59638303",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.12199639,
                "_source": {
                    "name": "foo"
                }
            },
            {
                "_index": "59638303",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.12199639,
                "_source": {
                    "name": "foo"
                }
            },
            {
                "_index": "59638303",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.09271725,
                "_source": {
                    "name": "foo bar"
                }
            },
            {
                "_index": "59638303",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.09271725,
                "_source": {
                    "name": "foo 1"
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当您使用分页进行搜索查询时:

{
    "from": 0,
    "size": 1,--> note size 1
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "foo"
                    }
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下响应

{
    "took": 23,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4, --> this is still 4
            "relation": "eq"
        },
        "max_score": 0.12199639,
        "hits": [
            {
                "_index": "59638303",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.12199639,
                "_source": {
                    "name": "foo"
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在上面的查询中,您可以更改大小并仅检查内部命中数组发生更改,但包含总计的外部命中对象始终保持与 4 相同,这证实您的理解是正确的。