elasticsearch查询与部分值过滤

eoxn13cs  于 11个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(82)

我使用Query来获取所有的sub_attributes.id(13,15,19). bu在结果中应该只获取id,其中sub_attributes.id为13,15,&19.不是我提到的id.应该获取部分索引值,并且使用must_not,不获取我期望的确切结果。
下面是我用来获取值的查询,

查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "sub_attributes.id": [13, 15, 19]
                    }
                }
            ]
        }
    }
}

字符串

结果:

{
    "_index": "attrib",
    "_id": "id1",
    "_score": 1.0,
    "_source": {
        "product_attribute_id": 5,
        "main_attribute_name": "Grades"
        "sub_attributes": [
            {
                "id": 13,
                "attribute_label": null,
                "attribute_value": "Grade A"
            },
            {
                "id": 14,
                "attribute_label": null,
                "attribute_value": "Grade B"
            }
        ]
    }
},
{
    "_index": "attrib",
    "_id": "id2",
    "_score": 1.0,
    "_source": {
        "product_attribute_id": 6,
        "main_attribute_name": "Operations",
        "sub_attributes": [
            {
                "id": 15,
                "attribute_label": null,
                "attribute_value": "Building"
            },
            {
                "id": 16,
                "attribute_label": null,
                "attribute_value": "Sliding"
            }
            {
                "id": 19,
                "attribute_label": null,
                "attribute_value": "Double fold"
            }
        ]
    }
}

预期结果:

{
    "_index": "attrib",
    "_id": "id1",
    "_score": 1.0,
    "_source": {
        "product_attribute_id": 5,
        "main_attribute_name": "Grades",
        "sub_attributes": [
            {
                "id": 13,
                "attribute_label": null,
                "attribute_value": "Grade A"
            }
        ]
    }
},
{
    "_index": "attrib",
    "_id": "id2",
    "_score": 1.0,
    "_source": {
        "product_attribute_id": 6,
        "main_attribute_name": "Operations"
        "sub_attributes": [
            {
                "id": 15,
                "attribute_label": null,
                "attribute_value": "Building"
            },
            {
                "id": 19,
                "attribute_label": null,
                "attribute_value": "Double fold"
            }
        ]
    }
}```

want to know which term needs to be used to fetch the expected result

0md85ypi

0md85ypi1#

Tldr;

我不相信你可以。Elasticsearch允许过滤文档,但不能过滤文档的子部分。

解决方案

你可以修改你的文档,看起来像这样:

{
    "product_attribute_id": 5,
    "main_attribute_name": "Grades"
    "sub_attributes":
    {
        "id": 13,
        "attribute_label": null,
        "attribute_value": "Grade A"
    }
}

字符串
这将创建更多的文档,但更容易实现你想要的。

相关问题