首页 文章

在数组中查找多个精确值

提问于
浏览
0

我有一个如下所示的文档,其中PCL是一个关键字

{
    "name": "test",
    "PCL": [
            "product1_company1_location1",
            "product2_company2_location2"
          ]
  }

我正在尝试查找与数组中给出的值完全匹配的所有文档的计数 . 然而,弹性搜索文档清楚地表明,由于“术语”和“术语”是“包含操作”,因此没有“完全等于”,并且建议的使用计数的解决方案在此处不起作用,因为用户可以在阵列中具有尽可能多的元素 .

我尝试将PCL字段放入由字符分隔的字符串中以区分每个PCL,但如果字符串的顺序不相同,例如,P1C1L1_P2C2L2与P2C2L2_P1C1L1相同,则具有相同PCL的用户不太可能看到另一个文档 .

这是我正在努力解决的问题

GET indexName/_search
   {
    "query": {
     "bool": {
      "should": [
       {
          "bool": {
            "must": [
              {
                "terms": {
                  "PCL": [
                    "product1_company1_vendor1",
                    "product2_company2_vendor2"
                  ]
                }
              }
            ]
          }
         },
         {
           "bool": {
             "must": [
               {
                 "terms": {
                   "PCL": [
                     "product1_company1_vendor_1",
                     "product3_company3_vendor_3"
                   ]
                 }
               }
             ]
           }
          }
        ]
      }
     },
     "aggs": {
      "PCL": {
        "terms": {
          "field": "PCL"
         }
       }
     }
   }

无论如何使用Elasticsearch实现这一目标? (注意:我已决定对PCL进行排序,因此没有订单冲突并将其存储为字符串,因为数组内的完全匹配是不可能的)

1 回答

  • 0

    如果要获得完全匹配,则必须将搜索字符串括在 " 中 . 这将确保检索完全匹配结果 .

    例如:

    {
      "name": "test",
      "PCL": [
               "\"product1_company1_location1\"",
               "\"product2_company2_location2\""
             ]
    }
    

相关问题