0

I am trying to filter the documents based on some tags and subcategory.

Some documents:

[{
    "_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 I am trying

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

Requirement

The result documents must have any of the tags.tag provided in the query. Must have isVisible as true and productSubCategory.label as provided in the query.

With the above query I am getting Non-Veg items as well.

1 Answer 1

1

Because you are using match query which will do the free text search and it will match with the non-veg as well. You can use term query with keyword type insted of match query like shown below:

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

Please note that i have replace field name productSubCategory.label with productSubCategory.label.keyword

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.