【问题标题】:How to add post fields on the basis of result of elastic search如何根据弹性搜索的结果添加帖子字段
【发布时间】:2019-05-25 11:55:51
【问题描述】:

实际上我想根据弹性搜索的结果在响应中添加“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
            }
        }
] 
}

所以如果“doc_count”为 1 且低于 预期输出

,我想添加字段“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,
            "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
    }
  }
}]}

【问题讨论】:

  • 您到底想达到什么目的?您只是想根据条件将is_promoted 字段添加到result 数组中的对象,还是尝试在Elasticsearch 中做某事?
  • 请清楚说明您面临的问题是什么
  • 我刚刚编辑了我的问题,希望这能帮助你们理解问题
  • @Milkncookiez 是的,我想根据集群对象中的 document_count 在响应中添加“is_promoted”字段

标签: javascript node.js reactjs elasticsearch


【解决方案1】:

好的,所以,从您的 OP 下的直接 cmets 中的讨论中,我了解到您只想根据特定条件将 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 之类的编译或运行时错误,因为我们在对象中创建了一个新属性。

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

附:该解决方案基于我正确理解您的问题这一事实。否则,请扩展或改进您的 OP。

【讨论】:

  • 对不起。我刚刚再次更新了问题。情况是如果聚合响应如果“doc_count”为1,我必须添加“is_promoted”字段。并且is_promoted标志来自ES的实际结果(您可以在我的问题的最后一部分中看到我发布了实际的 json)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2020-02-04
  • 1970-01-01
  • 2015-06-16
  • 2018-09-29
相关资源
最近更新 更多