如何创建具有多个属性和多个选项的过滤器

问题描述 投票:0回答:1

我需要创建一个过滤器,可以选择多个属性,并且每个属性都有多个选项。

这是可供查阅的数据(都是有弹性的)

数组属性是单个产品的属性。

{
    "attributes": [
        {
            "id": "bafa62bf-cc24-4434-943e-6931bb31091f",
            "value": "Black",
            "attributeName": "Color",
            "attributeId": "1c5aae51-a58b-44bc-9073-2c0e4e97bebe"
        },
        {
            "id": "71dd6aeb-03ab-4fae-9ebd-6bf41ab821fa",
            "value": "Brand",
            "attributeName": "Tesla",
            "attributeId": "4c1c9939-6348-42e4-9c07-293587645300"
        },
        {
            "id": "f5fc9a7f-cc11-46fe-aa04-aa0f21ef4f14",
            "value": "Sedan",
            "attributeName": "Type",
            "attributeId": "8cdf4684-3306-4924-92dd-36efc874d416"
        },
        {
            "id": "c666cb4a-4442-4979-aed3-4156a6d92fec",
            "value": "KM",
            "attributeName": "0",
            "attributeId": "975461d4-65a6-47fa-8dc9-595d6def34c5"
        },
        {
            "id": "b3c66548-2a56-47f2-bdfd-2fc043d9d330",
            "value": "2020",
            "attributeName": "Year",
            "attributeId": "f546d055-8c87-45cf-8700-b3d6ca114dbc"
        }
    ]
}

我将从前端接收此有效负载,它将用于过滤:

{
    "attributesForFilter": [
        {
            "id": "4c1c9939-6348-42e4-9c07-293587645300",
            "values": [
                "Tesla"
            ]
        },
        {
            "id": "1c5aae51-a58b-44bc-9073-2c0e4e97bebe",
            "values": [
                "Grey",
                "Black"
            ]
        },
        {
            "id": "f546d055-8c87-45cf-8700-b3d6ca114dbc",
            "values": [
                "2020"
            ]
        }
    ]
}

在此示例中,它必须返回属性数组中具有值 TeslaGrayBlack2020 的结果。

我已经成功实现了这个过滤器,但是我需要将其返回到前端,这是用户可以根据上面应用的过滤器进行选择的可能性

{
    "attributes": [
        {
            "id": "1c5aae51-a58b-44bc-9073-2c0e4e97bebe",
            "name": "Color",
            "source": {
                "options": [
                    {
                        "id": "6e73fac6-95b3-4665-97ef-8590f9702ddd",
                        "value": "Black"
                    }
                ]
            }
        },
        {
            "id": "71dd6aeb-03ab-4fae-9ebd-6bf41ab821fa",
            "name": "Brand",
            "source": {
                "options": [
                    {
                        "id": "6e73fac6-95b3-4665-97ef-8590f9702ddd",
                        "value": "Tesla"
                    }
                ]
            }
        },
        {
            "id": "b3c66548-2a56-47f2-bdfd-2fc043d9d330",
            "name": "Year",
            "source": {
                "options": [
                    {
                        "id": "6e73fac6-95b3-4665-97ef-8590f9702ddd",
                        "value": "2020"
                    }
                ]
            }
        }
    ]
}

这些是用户可以继续过滤的选项和属性,但是在应用过滤器时,其他可能性将被删除,例如“年份”属性,它仅返回 2020,但必须返回更多值。其他属性也一样。发生这种情况是因为过滤器排除了可能的选项,只留下前端在 attributeForFilter 中传递的确切选项

我相信我需要应用两种不同类型的过滤器,或者可能修改属性数据,但我找不到解决方案。

可以使用elastic来完成,或者简单地在elastic中搜索产品并通过代码应用逻辑(我正在使用golang)

go elasticsearch filter logic
1个回答
0
投票

如果您有过滤器和聚合,并且聚合仍必须提供所有可能的其他值,则需要将过滤器应用为

post_filter
,以便在聚合阶段之后应用过滤。

query
(因此是您的过滤器)将文档集减少为仅匹配的文档集,然后在该文档集上运行聚合。使用
post_filter
时,仅在聚合运行后才会过滤命中,因此聚合不会受到过滤器的影响,并且仍然聚合完整的数据集。

© www.soinside.com 2019 - 2024. All rights reserved.