gpt4 book ai didi

javascript - Elasticsearch NodeJS 搜索 : How to retrieve results from response

转载 作者:行者123 更新时间:2023-11-30 12:05:28 26 4
gpt4 key购买 nike

我正在尝试使用 Node 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
},

然后它继续列出所有匹配的结果

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

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

编辑

所以我有点明白了。我可以访问

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

关于javascript - Elasticsearch NodeJS 搜索 : How to retrieve results from response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com