Reputation: 3068
I am sending a nested aggregation query on a nested object using the Node.js client v7.2.0 for elasticsearch. While executing the query on Node.js client, i am getting all the records for that index and not the filtered result with aggregation. Executing the same query using kibana or curl provides the expected result.
Query :-
const client = new Client({ node: 'http://localhost:9200' });
let body = {
"size": 0,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "doc.parentKey",
"query": {
"bool": {
"must": [
{
"term": {
"doc.parentKey.name.keyword": "Name1"
}
}
]
}
}
}
},
{
"term": {
"doc.owner.keyword": "0aa7b933-eab1-4939-b4d2-38c2d0602cba"
}
}
]
}
},
"aggs": {
"parentKey": {
"nested": {
"path": "doc.parentKey"
},
"aggs": {
"response": {
"aggs": {
"name": {
"terms": {
"field": "doc.parentKey.name.keyword"
},
"aggs": {
"value": {
"terms": {
"field": "doc.parentKey.value.keyword"
}
}
}
}
},
"filter": {
"term": {
"doc.parentKey.name.keyword": "Name1"
}
}
}
}
}
}
}
let {body: response} = await client.search({index: 'test-index', body});
Expected result (Getting this from kibana and curl)
{
"took" : 47,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 131,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"parentKey" : {
"doc_count" : 10218,
"response" : {
"doc_count" : 131,
"name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Name1",
"doc_count" : 131,
"value" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Value1",
"doc_count" : 131
}
]
}
}
]
}
}
}
}
}
Result from Node.js client
{
"body": {
"took": 1277,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 1,
"hits": [
{
"_index": "indexName",
"_type": "_doc",
"_id": "2f0e4524-1203-40a6-8527-aa980fc349a7[8196858]",
"_score": 1,
"_source": {
"doc": {
"owner": "059d2569-f268-417f-906b-cbed1f6fbcec",
"parentKey": [
{
"value": "Value3",
"name": "Name3"
},
{
"value": "Value4",
"name": "Name4"
}
]
},
"@version": "1",
"doc_as_upsert": true,
"@timestamp": "2019-07-31T09:29:27.644Z"
}
},
{
"_index": "indexName",
"_type": "_doc",
"_id": "6f5652a3-8b33-4c53-8bea-316e8ab1b8c5[8201864]",
"_score": 1,
"_source": {
"doc": {
"owner": "71a83599-1751-4fa4-896a-46b644f29a50"
"parentKey": [
{
"value": "Value1",
"name": "Name1"
},
{
"value": "Value2",
"name": "Name2"
}
]
},
"@version": "1",
"doc_as_upsert": true,
"@timestamp": "2019-07-31T09:29:33.795Z"
}
}
]
}
},
"statusCode": 200,
"headers": {
"content-type": "application/json; charset=UTF-8",
"date": "Thu, 08 Aug 2019 13:27:15 GMT",
"server": "nginx/1.12.2",
"content-length": "113899",
"connection": "keep-alive"
},
"warnings": null,
"meta": {
"context": null,
"request": {
"params": {
"method": "GET",
"path": "/indexName/_search",
"body": "",
"querystring": "",
"headers": {
"User-Agent": "elasticsearch-js/7.3.0 (linux 3.10.0-862.9.1.el7.x86_64-x64; Node.js v8.9.4)",
"Content-Type": "application/json",
"Content-Length": "0"
},
"timeout": 30000
},
"options": {
"size": 0,
"aggs": {
"parentKey": {
"nested": {
"path": "doc.parentKey"
},
"aggs": {
"response": {
"aggs": {
"name": {
"terms": {
"field": "doc.parentKey.name.keyword"
},
"aggs": {
"value": {
"terms": {
"field": "doc.parentKey.value.keyword"
}
}
}
}
},
"filter": {
"term": {
"doc.parentKey.name.keyword": "63bb04a3eb41417e8d0e61e074293581"
}
}
}
}
}
},
"warnings": null
},
"id": 1
},
"name": "elasticsearch-js",
"connection": {
"url": "http://elasticsearch",
"id": "http://elasticsearch",
"headers": {},
"deadCount": 0,
"resurrectTimeout": 0,
"_openRequests": 0,
"status": "alive",
"roles": {
"master": true,
"data": true,
"ingest": true,
"ml": false
}
},
"attempts": 0,
"aborted": false
}
}
The response from Node.js client is truncated(the response consisted of 10 objects inside hits.hits array which we would also get by running match_all for this index)
Any kind of help would be highly appreciated.
Upvotes: 1
Views: 293
Reputation: 217534
I think you're missing a key in your search
call:
let {body: response} = await client.search({index: 'test-index', body: body});
^
|
add the body key
Upvotes: -1