繁体   English   中英

如何根据弹性搜索结果添加帖子字段

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

实际上,我想根据弹性搜索的结果在响应中添加“ is_promoted”字段,如果“ doc_count”为1,则我必须从实际结果中获取属性的ID和“ is_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,
            "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
            }
        }
] 
}

所以我想添加字段“ is_promoted”,如果“ doc_count”为1,并且低于预期输出

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

以下是我的弹性搜索中的记录结构。 希望它能帮助您了解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
    }
  }
}]}

好的,因此,从OP下直接注释中的讨论中,我了解到,根据特定条件,您只想将is_promoted字段添加到ES响应的result数组中的某些对象中。

您需要做的非常简单:遍历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 is_promoted Property的运行时错误,因为我们在对象中创建了一个新属性。

我不知道您为什么使用此聚合功能。 您的问题纯粹是与JavaScript相关,而与ES或Node.js无关。

PS:此解决方案基于我正确理解您的问题的事实。 否则,请扩展或改善您的OP。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM