簡體   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