1

我正在尝试使用节点 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. 我如何提取实际值,或者keys在这种情况下,如何从上述查询的结果中提取?

编辑

所以我有点想通了。我可以通过

function showdocs(d){
    da=d.hits.hits

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

但这只会给我 10 个结果!我确定我在该字段中有 50 个不同的值

4

2 回答 2

2

使用大小 0 获取聚合桶,

"body": {
  "size": 0,
  "aggs": {
    "docs": {
      "terms": {
        "field": "DocumentID",
        "size": 10 // if you want 50 make it 50
      }
    }
  }
}
于 2017-09-26T13:32:08.173 回答
1

您可以在搜索查询中使用 size 参数。如果未指定,大小默认为 10。请参阅下面添加的尺寸参数。

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

于 2016-05-05T09:13:47.093 回答