ElasticSearch是否可以仅显示聚合?

时间:2019-03-26 10:01:26

标签: node.js elasticsearch

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

但是,每当我尝试使用聚合对字段进行计数时,聚合字段最终都不会出现在结果中。

到目前为止,我已经尝试将其用作查询/函数:

    public void onClick(int position) {

    Intent MyEditIntent = new 
    Intent(MainActivity.this,RecipeDetails.class);

    MyEditIntent.putExtra("recipe_target", 
    recipeList.get(position).getTargetPeople());
    startActivity(MyEditIntent);
}
************************************************************
int recipeTarget = getIntent().getIntExtra("recipe_target", 0);
************************************************************
targetView = findViewById(R.id.targetView);
targetView.setText(recipeTarget);

结果是:

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
      }
    }
  }

我做错什么了吗,还是我只是缺乏知识? 感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:1)

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

console.log(resp.aggregations)