我正在尝试使用节点 js 在 Elasticsearch 中进行搜索。这是我的脚本
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
},
它继续列出所有匹配的结果
我只想要唯一值,我应该如何指定聚合?
我如何提取实际值,或者
keys
在这种情况下,如何从上述查询的结果中提取?
编辑
所以我有点想通了。我可以通过
function showdocs(d){
da=d.hits.hits
for (i=0;i<da.length;i++)
{
console.log(da[i]["_source"]["DocumentID"]);}}
但这只会给我 10 个结果!我确定我在该字段中有 50 个不同的值