Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js ElasticSearch有没有办法只显示聚合?_Node.js_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Node.js,elasticsearch" /> elasticsearch,Node.js,elasticsearch" />

Node.js ElasticSearch有没有办法只显示聚合?

Node.js ElasticSearch有没有办法只显示聚合?,node.js,elasticsearch,Node.js,elasticsearch,我正在尝试从ElasticSearch检索一些数据。 到目前为止,一切正常,我可以查询数据 但每当我尝试使用聚合计算字段时,聚合字段就不在最后的结果中 到目前为止,我尝试将此作为我的查询/函数: var client = new elasticsearch.Client({ host: 'xxxxxxxxxxxxxxxxxxxxxxx', log:"trace" }); client.ping({ requestTimeout: 30000, }, function (err

我正在尝试从ElasticSearch检索一些数据。 到目前为止,一切正常,我可以查询数据

但每当我尝试使用聚合计算字段时,聚合字段就不在最后的结果中

到目前为止,我尝试将此作为我的查询/函数:

var client = new elasticsearch.Client({
  host: 'xxxxxxxxxxxxxxxxxxxxxxx',
  log:"trace"
});
client.ping({
    requestTimeout: 30000,
  }, function (error) {
    if (error) {
      console.error('elasticsearch cluster is down!');
    } else {
      console.log('All is well');
    }
  });
  client.search({
       "index":"worklight__appsession__1485302400000",
       "type":"AppSession",
        "body":{
            "query": {
                "filtered": {
                  "query": {
                    "query_string": {
                      "analyze_wildcard": true,
                      "query": "*"
                    }
                  },
                  "filter": {
                    "bool": {
                      "must": [
                        {
                          "range": {
                            "timestamp": {
                              "gte": 1553507131976,
                              "lte": 1553593531976
                            }
                          }
                        }
                      ],
                      "must_not": []
                    }
                  }
                }
              },
              "aggs": {
                "1": {
                  "cardinality": {
                    "field": "deviceID"
                  }
                }
              }
    }
  }).then(function (resp) {
      var hits = resp.hits.hits;
      console.log(hits)
  }, function (err) {
      console.trace(err.message);
  });
结果是:

Elasticsearch DEBUG: 2019-03-26T09:46:21Z
  starting request {
    "method": "HEAD",
    "requestTimeout": 30000,
    "castExists": true,
    "path": "/",
    "query": {}
  }


Elasticsearch DEBUG: 2019-03-26T09:46:21Z
  starting request {
    "method": "POST",
    "path": "/worklight__appsession__1485302400000/AppSession/_search",
    "body": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "analyze_wildcard": true,
              "query": "*"
            }
          },
          "filter": {
            "bool": {
              "must": [
                {
                  "range": {
                    "timestamp": {
                      "gte": 1553507131976,
                      "lte": 1553593531976
                    }
                  }
                }
              ],
              "must_not": []
            }
          }
        }
      },
      "aggs": {
        "1": {
          "cardinality": {
            "field": "deviceID"
          }
        }
      }
    },
    "query": {}
  }


Elasticsearch TRACE: 2019-03-26T09:46:22Z
  -> HEAD http://xx/

  <- 200
Elasticsearch DEBUG: 2019-03-26T09:46:22Z
  Request complete

All is well
Elasticsearch TRACE: 2019-03-26T09:46:22Z
  -> POST http://xx/worklight__appsession__1485302400000/AppSession/_search
  {
    "query": {
      "filtered": {
        "query": {
          "query_string": {
            "analyze_wildcard": true,
            "query": "*"
          }
        },
        "filter": {
          "bool": {
            "must": [
              {
                "range": {
                  "timestamp": {
                    "gte": 1553507131976,
                    "lte": 1553593531976
                  }
                }
              }
            ],
            "must_not": []
          }
        }
      }
    },
    "aggs": {
      "1": {
        "cardinality": {
          "field": "deviceID"
        }
      }
    }
  }
  <- 200
  {
    "took": 4,
    "timed_out": false,
    "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
    },
    "hits": {
      "total": 325,
      "max_score": 1,
      "hits": [
      ...
      confidential data here, not relevant to the topic.
      ...
    }
      ]
    },
    "aggregations": {
      "1": {
        "value": 133
      }
    }
  }
是我做错了什么,还是我只是缺乏知识?
谢谢您的时间。

您正在做
console.log(resp.hits.hits)
。请尝试以下方法:

console.log(resp.aggregations)
console.log(resp.aggregations)