ElasticSearch在数组上过滤多个匹配查询



我正在尝试根据一些标签和子类别过滤文档。

一些文件:

[{
"_id" : "2oukjh8o9qy2ejhasdkqwe",
"productName" : "ASSORTED DOUGHNUT 1PC",
"productSubCategory" : [
{
"label" : "Veg",
"imageUrl" : "/catImg/fandA.svg"
}
],
"isVisible" : true,
"tags" : [
{
"tagImageUrl" : " ",
"label" : "Favorites",
"tag" : "favorites"
}
]
},
{
"_id" : "638daf9f42e6efc7f06641c2",
"isVisible" : true,
"productName" : "FILTER COFFEE",
"productSubCategory" : [
{
"_id" : ObjectId("638daf18ed445826c06a7328"),
"label" : "Veg"
}
],
"tags" : [
{
"tagImageUrl" : " ",
"label" : "Trending",
"tag" : "trending"
},
{
"tagImageUrl" : " ",
"label" : "Favorites",
"tag" : "favorites"
},
{
"tagImageUrl" : " ",
"label" : "Cabin Friendly",
"tag" : "cabinfriendly"
}
]
},
{
"_id" : "6389d7f942e6efc7f05d51e0",
"isVisible" : true,
"productName" : "ILLY DOPPIO",
"productSubCategory" : [
{
"_id" : ObjectId("638d97236612ca5d5ceb53b9"),
"label" : "Non-Veg"
}
],
"tags" : [
{
"tagImageUrl" : " ",
"label" : "Cabin Friendly",
"tag" : "cabinfriendly"
}
]
}]

查询I am trying

{
"query": {
"bool": {
"must": [
{
"match": {
"productSubCategory.label": {
"query": "Veg",
"operator": "and"
}
}
},
{
"match": {
"isVisible": true
}
}
],
"filter": {
"terms": {
"tags.tag": [
"favorites",
"cabinfriendly"
]
}
}
}
}
}

结果文档必须具有任何标记。标记。必须有isVisible为true和productSubCategory。查询中提供的标签。

通过上面的查询,我也得到了非蔬菜项目。

因为您使用的是match query,它将进行自由文本搜索,并且它也将与非蔬菜匹配。您可以使用term querykeyword类型来代替匹配查询,如下所示:

{
"query": {
"bool": {
"must": [
{
"term": {
"productSubCategory.label.keyword": {
"value": "Veg"
}
}
},
{
"match": {
"isVisible": true
}
}
],
"filter": {
"terms": {
"tags.tag": [
"favorites",
"cabinfriendly"
]
}
}
}
}
}

请注意,我已将字段名称productSubCategory.label替换为productSubCategory.label.keyword

最新更新