如何根据ElasticSearch结果添加post字段

hgtggwj0  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(309)

实际上,我想根据ElasticSearch的结果在响应中添加“is\u promoted”字段,如果“doc\u count”为1,则必须从实际结果中获取属性的id和“is\u promoted”标志(您可以在问题处看到),这就像ElasticSearch中的后期过滤器。但我想在后过滤器中添加条件。请查看当前和预期输出。下面是弹性查询的当前输出

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

因此,如果“doc\u count”是1并且低于预期输出,我想添加字段“is\u promoted”

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

我用聚合法。

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

下面是我ElasticSearch中的记录结构。我希望它能帮助你理解塞纳里奥

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

wr98u20j1#

好吧,那么,从你的评论下的直接讨论,我明白你只想补充 is_promoted 字段中的某些对象 result es响应的数组,根据一定的条件。
您需要做的非常简单:通过 result 数组,检查每个对象的条件并添加属性。

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

我加上 + 在门前签名 obj.doc_count 以确保它被解析为 int 从而进行适当的条件比较。这个 is_promoted 属性作为数组键添加,以确保没有类似的编译或运行时错误 Property does not exist ,因为我们在对象中创建了一个新属性。
我不知道你为什么要使用这个聚合功能。您的问题完全与javascript相关,与es或node.js无关。
p、 这个解决方案是基于我正确理解你的问题。否则,请延长或改善您的op。

相关问题