首页 > 解决方案 > elasticsearch _search api stats 标签有什么作用?

问题描述

我正在尝试在用户使用 _search API 时收集数据,我偶然发现了一个可以放入搜索查询的标签,这可能会为我简化一切,但我找不到足够的信息。

_search 正文中的 stats 标签是否会影响任何内容?喜欢返回的结果?

我能在上面找到的唯一信息是这个页面https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html它所声明的只是字符串,字符串 [ ], Boolean — 用于记录和统计目的的请求的特定标记。elasticsearch 是否真的在某处记录它?

我的 _search 请求示例:

木花

GET myindex/doc/_search
{
  "query": {
    "match_all": {}
  }, 
  "stats": ["my string of data"]
}

卷曲

curl -XGET "http://localhost:9200/myindex/doc/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }, 
  "stats": ["my string of data"]
}'

标签: elasticsearch

解决方案


查询中的stats关键字_search旨在定义一些统计信息组,您可以稍后使用_statsAPI 进行查询。例如,假设您myindex使用my-query组进行查询:

curl -XGET "http://localhost:9200/myindex/doc/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }, 
  "stats": ["my-query"]
}'

然后,您可以使用以下查询获取该组的索引级搜索统计信息:

curl -XGET "http://localhost:9200/myindex/_stats/search?groups=my-stats"

你会得到这样的东西:

{
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_all": {
    "primaries": {
      "search": {
        "open_contexts": 0,
        "query_total": 5806,
        "query_time_in_millis": 73948,
        ...
        "groups": {
          "my-query": {                         <----------
            "query_total": 8,
            "query_time_in_millis": 81,
            ...
          }
        }
      }
    },
    "total": {
      "search": {
        "open_contexts": 0,
        "query_total": 5806,
        "query_time_in_millis": 73948,
        ...
        "groups": {
          "my-query": {                         <----------
            "query_total": 8,
            "query_time_in_millis": 81,
            ...
          }
        }
      }
    }
  },
  "indices": {
    "listings-master": {
      "uuid": "oUYHBiU8RVayI95uCw3Clg",
      "primaries": {
        "search": {
          "open_contexts": 0,
          "query_total": 5806,
          "query_time_in_millis": 73948,
          ...
          "groups": {
            "my-query": {                         <----------
              "query_total": 8,
              "query_time_in_millis": 81,
              ...
            }
          }
        }
      },
      "total": {
        "search": {
          "open_contexts": 0,
          "query_total": 5806,
          "query_time_in_millis": 73948,
          ...
          "groups": {
            "my-query": {                         <----------
              "query_total": 8,
              "query_time_in_millis": 81,
              ...
            }
          }
        }
      }
    }
  }
}

推荐阅读