How to add post fields on the basis of result of elastic search












-1














Actually i want to add "is_promoted" field in the response according to the result of elastic search if the "doc_count" is 1 then i have to fetch the id of the property and "is_promoted" flag from the actual result(Which you can see at the question).Its like a post filter in the elastic search. but i want add condition in the post filter. please see the current and expected output.
And below is the current output of the elastic query



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


So i want to add field "is_promoted" if the "doc_count" is 1 and below is the expected output



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


I used aggregation for that.



                    query.bool.minimum_should_match = 1;
aggQuery.zoomedin = {
filter: {
geo_bounding_box: {
location: {
top_left: {
lat: params.geo_bounding_box.location.nw.lat,
lon: params.geo_bounding_box.location.nw.lng
},
bottom_right: {
lat: params.geo_bounding_box.location.se.lat,
lon: params.geo_bounding_box.location.se.lng
}
}
}
},
aggregations: {
result: {
geohash_grid: {
field: "location",
precision: zoomLevel
},
"aggs": {
"centroid": {
"geo_centroid": { "field": "location" }
}
}
}
}


Below is the record structure in my elastic search. I hope it will help you to understand the senario



"hits": {
"total": 7967,
"max_score": 1,
"hits": [
{
"_index": "biproxi-test",
"_type": "listings",
"_id": "5126",
"_score": 1,
"_source": {
"address_line1": "Brandon Town Center Drive",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": null,
"cap_rate": null,
"category": 1,
"city": "Brandon",
"country": "United States",
"county": "Hillsborough",
"floor_location": null,
"inplace_occupancy": null,
"land_size": 3,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 27.937159,
"lon": -82.327498
},
"no_of_units": null,
"postal_code": "33511",
"price": 2185000,
"renovated_year": null,
"square_feet": null,
"state": "Florida",
"state_code": "FL",
"title": "3+- Acres at Brandon Town Center",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5126/caf39154-fb42-483f-9320-9e9c394be66b.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}, {
"_index": "biproxi-test",
"_type": "listings",
"_id": "5213",
"_score": 1,
"_source": {
"address_line1": "1909 N. Columbia Street",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": "1996",
"cap_rate": null,
"category": 2,
"city": "Milledgeville",
"country": "United States",
"county": "Baldwin",
"floor_location": null,
"inplace_occupancy": null,
"land_size": null,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 33.1086,
"lon": -83.25388
},
"no_of_units": null,
"postal_code": "31061",
"price": null,
"renovated_year": null,
"square_feet": null,
"state": "Georgia",
"state_code": "GA",
"title": "Milledge Village - 1 Space Remaining",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5213/33d1cd5b-11a1-427d-8948-2e2db3d8e7f2.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}
}]}









share|improve this question




















  • 1




    What exactly are you trying to achieve? Are you simply trying to add the is_promoted field to the objects in the result array depending on a condition or are you trying to do something in Elasticsearch?
    – Milkncookiez
    2 days ago










  • please mention clearly what is the issue you are facing
    – varnit
    2 days ago










  • I have just edit my question i hope this will help you guys to understand the problem
    – Varun verma
    2 days ago










  • @Milkncookiez Yes i want to add "is_promoted" field in the response according to the document_count in the cluster object
    – Varun verma
    2 days ago


















-1














Actually i want to add "is_promoted" field in the response according to the result of elastic search if the "doc_count" is 1 then i have to fetch the id of the property and "is_promoted" flag from the actual result(Which you can see at the question).Its like a post filter in the elastic search. but i want add condition in the post filter. please see the current and expected output.
And below is the current output of the elastic query



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


So i want to add field "is_promoted" if the "doc_count" is 1 and below is the expected output



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


I used aggregation for that.



                    query.bool.minimum_should_match = 1;
aggQuery.zoomedin = {
filter: {
geo_bounding_box: {
location: {
top_left: {
lat: params.geo_bounding_box.location.nw.lat,
lon: params.geo_bounding_box.location.nw.lng
},
bottom_right: {
lat: params.geo_bounding_box.location.se.lat,
lon: params.geo_bounding_box.location.se.lng
}
}
}
},
aggregations: {
result: {
geohash_grid: {
field: "location",
precision: zoomLevel
},
"aggs": {
"centroid": {
"geo_centroid": { "field": "location" }
}
}
}
}


Below is the record structure in my elastic search. I hope it will help you to understand the senario



"hits": {
"total": 7967,
"max_score": 1,
"hits": [
{
"_index": "biproxi-test",
"_type": "listings",
"_id": "5126",
"_score": 1,
"_source": {
"address_line1": "Brandon Town Center Drive",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": null,
"cap_rate": null,
"category": 1,
"city": "Brandon",
"country": "United States",
"county": "Hillsborough",
"floor_location": null,
"inplace_occupancy": null,
"land_size": 3,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 27.937159,
"lon": -82.327498
},
"no_of_units": null,
"postal_code": "33511",
"price": 2185000,
"renovated_year": null,
"square_feet": null,
"state": "Florida",
"state_code": "FL",
"title": "3+- Acres at Brandon Town Center",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5126/caf39154-fb42-483f-9320-9e9c394be66b.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}, {
"_index": "biproxi-test",
"_type": "listings",
"_id": "5213",
"_score": 1,
"_source": {
"address_line1": "1909 N. Columbia Street",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": "1996",
"cap_rate": null,
"category": 2,
"city": "Milledgeville",
"country": "United States",
"county": "Baldwin",
"floor_location": null,
"inplace_occupancy": null,
"land_size": null,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 33.1086,
"lon": -83.25388
},
"no_of_units": null,
"postal_code": "31061",
"price": null,
"renovated_year": null,
"square_feet": null,
"state": "Georgia",
"state_code": "GA",
"title": "Milledge Village - 1 Space Remaining",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5213/33d1cd5b-11a1-427d-8948-2e2db3d8e7f2.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}
}]}









share|improve this question




















  • 1




    What exactly are you trying to achieve? Are you simply trying to add the is_promoted field to the objects in the result array depending on a condition or are you trying to do something in Elasticsearch?
    – Milkncookiez
    2 days ago










  • please mention clearly what is the issue you are facing
    – varnit
    2 days ago










  • I have just edit my question i hope this will help you guys to understand the problem
    – Varun verma
    2 days ago










  • @Milkncookiez Yes i want to add "is_promoted" field in the response according to the document_count in the cluster object
    – Varun verma
    2 days ago
















-1












-1








-1







Actually i want to add "is_promoted" field in the response according to the result of elastic search if the "doc_count" is 1 then i have to fetch the id of the property and "is_promoted" flag from the actual result(Which you can see at the question).Its like a post filter in the elastic search. but i want add condition in the post filter. please see the current and expected output.
And below is the current output of the elastic query



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


So i want to add field "is_promoted" if the "doc_count" is 1 and below is the expected output



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


I used aggregation for that.



                    query.bool.minimum_should_match = 1;
aggQuery.zoomedin = {
filter: {
geo_bounding_box: {
location: {
top_left: {
lat: params.geo_bounding_box.location.nw.lat,
lon: params.geo_bounding_box.location.nw.lng
},
bottom_right: {
lat: params.geo_bounding_box.location.se.lat,
lon: params.geo_bounding_box.location.se.lng
}
}
}
},
aggregations: {
result: {
geohash_grid: {
field: "location",
precision: zoomLevel
},
"aggs": {
"centroid": {
"geo_centroid": { "field": "location" }
}
}
}
}


Below is the record structure in my elastic search. I hope it will help you to understand the senario



"hits": {
"total": 7967,
"max_score": 1,
"hits": [
{
"_index": "biproxi-test",
"_type": "listings",
"_id": "5126",
"_score": 1,
"_source": {
"address_line1": "Brandon Town Center Drive",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": null,
"cap_rate": null,
"category": 1,
"city": "Brandon",
"country": "United States",
"county": "Hillsborough",
"floor_location": null,
"inplace_occupancy": null,
"land_size": 3,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 27.937159,
"lon": -82.327498
},
"no_of_units": null,
"postal_code": "33511",
"price": 2185000,
"renovated_year": null,
"square_feet": null,
"state": "Florida",
"state_code": "FL",
"title": "3+- Acres at Brandon Town Center",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5126/caf39154-fb42-483f-9320-9e9c394be66b.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}, {
"_index": "biproxi-test",
"_type": "listings",
"_id": "5213",
"_score": 1,
"_source": {
"address_line1": "1909 N. Columbia Street",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": "1996",
"cap_rate": null,
"category": 2,
"city": "Milledgeville",
"country": "United States",
"county": "Baldwin",
"floor_location": null,
"inplace_occupancy": null,
"land_size": null,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 33.1086,
"lon": -83.25388
},
"no_of_units": null,
"postal_code": "31061",
"price": null,
"renovated_year": null,
"square_feet": null,
"state": "Georgia",
"state_code": "GA",
"title": "Milledge Village - 1 Space Remaining",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5213/33d1cd5b-11a1-427d-8948-2e2db3d8e7f2.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}
}]}









share|improve this question















Actually i want to add "is_promoted" field in the response according to the result of elastic search if the "doc_count" is 1 then i have to fetch the id of the property and "is_promoted" flag from the actual result(Which you can see at the question).Its like a post filter in the elastic search. but i want add condition in the post filter. please see the current and expected output.
And below is the current output of the elastic query



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


So i want to add field "is_promoted" if the "doc_count" is 1 and below is the expected output



 {
"total_records": 32392,
"fetched_records": 3845,
"result": [
{
"key": "dp3w",
"doc_count": 343,
"centroid": {
"location": {
"lat": 41.919059987131064,
"lon": -87.71653202438385
},
"count": 343
}
},
{
"key": "djvw",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 33.49416188221888,
"lon": -82.0443208285174
},
"count": 1
}
},
{
"key": "9qhs",
"doc_count": 1,
"is_promoted":true,
"centroid": {
"location": {
"lat": 34.52696419113244,
"lon": -117.29711956000672
},
"count": 1
}
}
]
}


I used aggregation for that.



                    query.bool.minimum_should_match = 1;
aggQuery.zoomedin = {
filter: {
geo_bounding_box: {
location: {
top_left: {
lat: params.geo_bounding_box.location.nw.lat,
lon: params.geo_bounding_box.location.nw.lng
},
bottom_right: {
lat: params.geo_bounding_box.location.se.lat,
lon: params.geo_bounding_box.location.se.lng
}
}
}
},
aggregations: {
result: {
geohash_grid: {
field: "location",
precision: zoomLevel
},
"aggs": {
"centroid": {
"geo_centroid": { "field": "location" }
}
}
}
}


Below is the record structure in my elastic search. I hope it will help you to understand the senario



"hits": {
"total": 7967,
"max_score": 1,
"hits": [
{
"_index": "biproxi-test",
"_type": "listings",
"_id": "5126",
"_score": 1,
"_source": {
"address_line1": "Brandon Town Center Drive",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": null,
"cap_rate": null,
"category": 1,
"city": "Brandon",
"country": "United States",
"county": "Hillsborough",
"floor_location": null,
"inplace_occupancy": null,
"land_size": 3,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 27.937159,
"lon": -82.327498
},
"no_of_units": null,
"postal_code": "33511",
"price": 2185000,
"renovated_year": null,
"square_feet": null,
"state": "Florida",
"state_code": "FL",
"title": "3+- Acres at Brandon Town Center",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5126/caf39154-fb42-483f-9320-9e9c394be66b.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}, {
"_index": "biproxi-test",
"_type": "listings",
"_id": "5213",
"_score": 1,
"_source": {
"address_line1": "1909 N. Columbia Street",
"address_line2": "USA",
"building_class": null,
"building_type": null,
"built_year": "1996",
"cap_rate": null,
"category": 2,
"city": "Milledgeville",
"country": "United States",
"county": "Baldwin",
"floor_location": null,
"inplace_occupancy": null,
"land_size": null,
"lease_type_id": null,
"lease_type": null,
"listing_group": "Retail",
"listing_type": [
1
],
"location": {
"lat": 33.1086,
"lon": -83.25388
},
"no_of_units": null,
"postal_code": "31061",
"price": null,
"renovated_year": null,
"square_feet": null,
"state": "Georgia",
"state_code": "GA",
"title": "Milledge Village - 1 Space Remaining",
"status": 2,
"listing_image": "https://biproxi.s3.amazonaws.com/image/listing/5213/33d1cd5b-11a1-427d-8948-2e2db3d8e7f2.jpg",
"seller_id": "113157245308689129523",
"is_promoted": false
}
}
}]}






javascript node.js reactjs elasticsearch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Varun verma

3617




3617








  • 1




    What exactly are you trying to achieve? Are you simply trying to add the is_promoted field to the objects in the result array depending on a condition or are you trying to do something in Elasticsearch?
    – Milkncookiez
    2 days ago










  • please mention clearly what is the issue you are facing
    – varnit
    2 days ago










  • I have just edit my question i hope this will help you guys to understand the problem
    – Varun verma
    2 days ago










  • @Milkncookiez Yes i want to add "is_promoted" field in the response according to the document_count in the cluster object
    – Varun verma
    2 days ago
















  • 1




    What exactly are you trying to achieve? Are you simply trying to add the is_promoted field to the objects in the result array depending on a condition or are you trying to do something in Elasticsearch?
    – Milkncookiez
    2 days ago










  • please mention clearly what is the issue you are facing
    – varnit
    2 days ago










  • I have just edit my question i hope this will help you guys to understand the problem
    – Varun verma
    2 days ago










  • @Milkncookiez Yes i want to add "is_promoted" field in the response according to the document_count in the cluster object
    – Varun verma
    2 days ago










1




1




What exactly are you trying to achieve? Are you simply trying to add the is_promoted field to the objects in the result array depending on a condition or are you trying to do something in Elasticsearch?
– Milkncookiez
2 days ago




What exactly are you trying to achieve? Are you simply trying to add the is_promoted field to the objects in the result array depending on a condition or are you trying to do something in Elasticsearch?
– Milkncookiez
2 days ago












please mention clearly what is the issue you are facing
– varnit
2 days ago




please mention clearly what is the issue you are facing
– varnit
2 days ago












I have just edit my question i hope this will help you guys to understand the problem
– Varun verma
2 days ago




I have just edit my question i hope this will help you guys to understand the problem
– Varun verma
2 days ago












@Milkncookiez Yes i want to add "is_promoted" field in the response according to the document_count in the cluster object
– Varun verma
2 days ago






@Milkncookiez Yes i want to add "is_promoted" field in the response according to the document_count in the cluster object
– Varun verma
2 days ago














1 Answer
1






active

oldest

votes


















0














Okay, so, from the discussion in the direct comments under your OP I understand that you only want to add is_promoted field to some of the objects in the result array of the ES response, according to a certain condition.



What you need to do is very simple: loop through the result array, check the condition for each object and add the property.



for (const obj of elasticSearchResult.result) {
if (+obj.doc_count <= 1) {
obj['is_promoted'] = true;
}
}


I add the + sign in front of the obj.doc_count to ensure it's parsed to int and thus conducting a proper conditional comparison. The is_promoted property is added as an array key to ensure there are no compile or runtime errors of the sort of Property does not exist, since we create a new property in the object.



I don't know why do you use this aggregation functionality. You problem is purely JavaScript related and has nothing to do with ES or Node.js in particular.



P.S. This solution is based on the fact I understand your question correctly. Otherwise, please extend or improve your OP.






share|improve this answer





















  • sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
    – Varun verma
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945283%2fhow-to-add-post-fields-on-the-basis-of-result-of-elastic-search%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Okay, so, from the discussion in the direct comments under your OP I understand that you only want to add is_promoted field to some of the objects in the result array of the ES response, according to a certain condition.



What you need to do is very simple: loop through the result array, check the condition for each object and add the property.



for (const obj of elasticSearchResult.result) {
if (+obj.doc_count <= 1) {
obj['is_promoted'] = true;
}
}


I add the + sign in front of the obj.doc_count to ensure it's parsed to int and thus conducting a proper conditional comparison. The is_promoted property is added as an array key to ensure there are no compile or runtime errors of the sort of Property does not exist, since we create a new property in the object.



I don't know why do you use this aggregation functionality. You problem is purely JavaScript related and has nothing to do with ES or Node.js in particular.



P.S. This solution is based on the fact I understand your question correctly. Otherwise, please extend or improve your OP.






share|improve this answer





















  • sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
    – Varun verma
    2 days ago
















0














Okay, so, from the discussion in the direct comments under your OP I understand that you only want to add is_promoted field to some of the objects in the result array of the ES response, according to a certain condition.



What you need to do is very simple: loop through the result array, check the condition for each object and add the property.



for (const obj of elasticSearchResult.result) {
if (+obj.doc_count <= 1) {
obj['is_promoted'] = true;
}
}


I add the + sign in front of the obj.doc_count to ensure it's parsed to int and thus conducting a proper conditional comparison. The is_promoted property is added as an array key to ensure there are no compile or runtime errors of the sort of Property does not exist, since we create a new property in the object.



I don't know why do you use this aggregation functionality. You problem is purely JavaScript related and has nothing to do with ES or Node.js in particular.



P.S. This solution is based on the fact I understand your question correctly. Otherwise, please extend or improve your OP.






share|improve this answer





















  • sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
    – Varun verma
    2 days ago














0












0








0






Okay, so, from the discussion in the direct comments under your OP I understand that you only want to add is_promoted field to some of the objects in the result array of the ES response, according to a certain condition.



What you need to do is very simple: loop through the result array, check the condition for each object and add the property.



for (const obj of elasticSearchResult.result) {
if (+obj.doc_count <= 1) {
obj['is_promoted'] = true;
}
}


I add the + sign in front of the obj.doc_count to ensure it's parsed to int and thus conducting a proper conditional comparison. The is_promoted property is added as an array key to ensure there are no compile or runtime errors of the sort of Property does not exist, since we create a new property in the object.



I don't know why do you use this aggregation functionality. You problem is purely JavaScript related and has nothing to do with ES or Node.js in particular.



P.S. This solution is based on the fact I understand your question correctly. Otherwise, please extend or improve your OP.






share|improve this answer












Okay, so, from the discussion in the direct comments under your OP I understand that you only want to add is_promoted field to some of the objects in the result array of the ES response, according to a certain condition.



What you need to do is very simple: loop through the result array, check the condition for each object and add the property.



for (const obj of elasticSearchResult.result) {
if (+obj.doc_count <= 1) {
obj['is_promoted'] = true;
}
}


I add the + sign in front of the obj.doc_count to ensure it's parsed to int and thus conducting a proper conditional comparison. The is_promoted property is added as an array key to ensure there are no compile or runtime errors of the sort of Property does not exist, since we create a new property in the object.



I don't know why do you use this aggregation functionality. You problem is purely JavaScript related and has nothing to do with ES or Node.js in particular.



P.S. This solution is based on the fact I understand your question correctly. Otherwise, please extend or improve your OP.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Milkncookiez

2,15973157




2,15973157












  • sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
    – Varun verma
    2 days ago


















  • sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
    – Varun verma
    2 days ago
















sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
– Varun verma
2 days ago




sorry . i just updated the question again. The situation is i have to add the "is_promoted" field if the aggregation response if the "doc_count" is 1. and that is_promoted flag comes from the actual result of the ES(Which you can see in the last part of my question where i posted actual json).
– Varun verma
2 days ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945283%2fhow-to-add-post-fields-on-the-basis-of-result-of-elastic-search%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

generate and download xml file after input submit (php and mysql) - JPK

Angular Downloading a file using contenturl with Basic Authentication

Can't read property showImagePicker of undefined in react native iOS