Elasticsearch NodeJSsearch:如何从响应中检索结果

我正在尝试使用节点js在Elasticsearch中进行search。 这是我的脚本

var elasticsearch = require('elasticsearch'); var client = elasticsearch.Client({ host: 'localhost:9200', log: 'trace' }); client.ping({ // ping usually has a 3000ms timeout requestTimeout: Infinity, // undocumented params are appended to the query string hello: "elasticsearch!" }, function (error) { if (error) { console.trace('elasticsearch cluster is down!'); } else { console.log('All is well'); getmeres(client); } }); function getmeres(client) { client.search({ index: 'researchtest', body: { "aggs": { "docs": { "terms": { "field": "DocumentID", "size": 0 } } } } }, function (error, response) { if (error) { console.trace('Search query failed'); } else { console.log('All is well'); d=response;//console.log(response); showdocs(d) } }); } function showdocs(d){ console.log(d["hits"]);} 

我明白了

 All is well { total: 92, max_score: 1, hits: [ { _index: 'researchtest', _type: 'test', _id: 'AVLC2axzrLgN-DZLLsmp', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2izQrLgN-DZLLsnw', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2EEnrLgN-DZLLsjj', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2F7MrLgN-DZLLsj6', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2nhDrLgN-DZLLsol', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2pMUrLgN-DZLLsox', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2mTMrLgN-DZLLsoL', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2rDZrLgN-DZLLspS', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2t5ErLgN-DZLLsp8', _score: 1, _source: [Object] }, { _index: 'researchtest', _type: 'test', _id: 'AVLC2dVHrLgN-DZLLsnA', _score: 1, _source: [Object] } ] } 

但我没有看到我想要的实际值。 我如何从响应中获取值? 在控制台我也看到

 "aggregations": { "docs": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "2235", "doc_count": 2 }, { "key": "12312", "doc_count": 2 }, { "key": "4565", "doc_count": 2 }, { "key": "7809780", "doc_count": 2 }, { "key": "8678", "doc_count": 2 }, 

并继续列出所有匹配的结果

  1. 我只想要唯一的值,我应该如何指定聚合呢?

  2. 在这种情况下,如何从上述查询的结果中提取实际值或keys

编辑

所以我有点想通了。 我能够通过访问

 function showdocs(d){ da=d.hits.hits for (i=0;i<da.length;i++) { console.log(da[i]["_source"]["DocumentID"]);}} 

但是这只给了我10个结果! 我知道我在这个领域有50个不同的价值观

使用大小0来获得聚合桶,

 "body": { "size": 0, "aggs": { "docs": { "terms": { "field": "DocumentID", "size": 10 // if you want 50 make it 50 } } } } 

您可以在search查询中使用大小参数。 如果没有指定,大小默认为10。 见下面添加的大小参数。

client.search({index:'researchtest',size:100 ……………