数组上的ElasticSearch筛选器沿着多个匹配查询

vof42yt1  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(167)

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

部分文档:

[{
    "_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"
        }
    ]
}]

正在尝试的查询

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

要求

结果文档必须具有查询中提供得任何标记.tag.必须具有isVisible为true与productSubCategory.label,如查询中提供得那样.
与上述查询,我得到非蔬菜项目以及。

wb1gzix0

wb1gzix01#

因为您使用地是match query,它将执行自由文本搜索,也将与非veg匹配;您可以使用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

相关问题