【问题标题】:What does total value shows inside the _search query result in elasticsearch?elasticsearch中_search查询结果中的总值显示什么?
【发布时间】:2020-01-08 10:33:50
【问题描述】:

当我们调用elasticsearch时,说如下: POST https:////_search with body:

{
      "from": 0,
      "size": 1,
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "createdAt": {
                  "gt": "2019-11-11T10:00:00"
                }
              }
            }

          ]
        }
      },
      "sort": [
        {
            "createdAt" : {
                "order" : "desc"
            }
        }
        ]
}

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

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": “<index-name>”,
                "_type": "_doc",
                "_id": "5113c843-dff3-499f-a12e-44c7ac103bcf_0",
                "_score": null,
                "_source": {
                    "oId": "5113c843-dff3-499f-a12e-44c7ac103bcf",
                    "oItemId": 0,
                    "createdAt": "2019-11-13T11:00:00"
                },
                "sort": [
                    1573642800000
                ]
            }
        ]
    }
}

总不捕获分页部分吗?而且它只关心查询报告?无论分页集如何,它都应该显示与查询匹配的项目总数,对吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

    确切地说,如official ES docs 中所述。

    total (Object) 关于返回文档数量的元数据。 返回的参数包括:

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

    eq: Accurate 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"
            }
        }
    }
    

    一旦创建上述索引,索引以下 4 个文档:

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

      {
            "name": "foo 1"
        }
    
    {
        "name": "foo bar"
    }
    
    {
        "name": "foo"
    }
    
    {
        "name": "foo 2"
    }
    

    现在,当您在没有分页的情况下点击以下搜索查询时:

    {
    
        "query": {
            "bool": {
                "must": [
                    {
                        "match": {
                            "name": "foo"
                        }
                    }
                ]
            }
        }
    }
    

    它给出以下响应:

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

    但是当您使用分页点击搜索查询时:

    {
        "from": 0,
        "size": 1,--> note size 1
        "query": {
            "bool": {
                "must": [
                    {
                        "match": {
                            "name": "foo"
                        }
                    }
                ]
            }
        }
    }
    

    它给出以下响应

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多