Exact search in array object type using elasticsearch

后端 未结 2 1928
深忆病人
深忆病人 2021-02-04 04:58

I\'m looking for a way to do exact array matches in elastic search. Let\'s say these are my documents:

{\"id\": 1, \"categories\" : [\"c\", \"d\         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 05:44

    I found a solution for our usage case that appears to work. It relies on two filters and the knowledge of how many categories we want to match against. We make use of a terms filter and a script filter to check the size of the array. In this example, marketBasketList is similar to your categories entry.

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "siteId": 4
              }
            },
            {
              "match": {
                "marketBasketList": {
                  "query": [
                    10,
                    11
                  ],
                  "operator": "and"
                }
              }
            }
          ]
        },
        "boost": 1,
        "filter": {
          "and": {
            "filters": [
              {
                "script": {
                  "script": "doc['marketBasketList'].values.length == 2"
                }
              },
              {
                "terms": {
                  "marketBasketList": [
                    10,
                    11
                  ],
                  "execution": "and"
                }
              }
            ]
          }
        }
      }
    }
    

提交回复
热议问题