AbtPst
AbtPst

Reputation: 8018

Elasticsearch NodeJS search : How to retrieve results from response

I am trying to search in Elasticsearch using node js. Here is my script

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"]);}

i get

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

but i dont see the actual values that i want here. how do i get values from the response? In the console i also see

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

and it goes on to list all of the matched results

  1. I only want the unique values, how should i specify the aggregation for that?

  2. How do i extract the actual values or keys in this case, from the result of the above query?

Edit

so i kind of figured it out. i am able to access by

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

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

but this only gives me 10 results back! I know for sure that i have 50 distinct values in that field

Upvotes: 1

Views: 2435

Answers (2)

Surabhi K
Surabhi K

Reputation: 144

Use size 0 to get the aggregation buckets,

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

Upvotes: 2

Sourabh Jain
Sourabh Jain

Reputation: 628

You can use the size parameter in your search query. The size is defaulted to 10 if not specified. See size parameter added below.

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

Upvotes: 1

Related Questions