ElasticSearch过滤器创建

wwtsj6pe  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(143)

嗨,我正在努力创建Elastic搜索过滤器。因此存在如下所示现有查询:

{
   "query":{
      "bool":{
         "must":[
            {
               "match":{
                  "account_id":1231231231
               }
            },
            {
               "multi_match":{
                  "fuzziness":"AUTO",
                  "query":"topics",
                  "type":"most_fields",
                  "fields":[
                     "title.value^11",              
                     "parent_name^4",
                     "*"
                  ],
                  "operator":"and"
               }
            }
         ],
         "filter":[
            
         ],
         "must_not":{
            "terms":{
               "published_status.raw":[
                  "Disabled"
               ]
            }
         }
      }
   },
   "_source":[
      "node_type",
      "parent_name",
      "node_name",
      "title",
    
   ],
   "size":5,
   "indices_boost":{
      "tables":7,
      "columns":1
   },
   "sort":[
      "_score",
      {
         "tbl_popularity_info.NoOfTimeUsed":{
            "order":"desc",
            "unmapped_type":"long"
         }
      }
   ]
}

所以现在的问题是我必须在里面加上两个条件。其中1是具有包含特定类型ID的包括记录,而2是基于ID的排除。目前我有那个条件的SQL(在关系数据库中使用来查询相同的数据)。即:
where (( dmn_id in (123,301)) or ( cat_id in (300))) and not (( coalesce(trm_status_id,'' ) in ('SUG') ) AND ( string_to_array(coalesce(tag_ids,'' ),',') && ARRAY['314'] ) )
所以根据我的理解,我是这样创作的:

"filter": [
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "dmn_id": [123,301]
                }
              },
              {
                "terms": {
                  "cat_id": [300]
                }
              }
            ]
          }
        },
        {
      "bool": {
        "must_not": [
          {
            "bool": {
              "must": [
                {
                  "terms": {
                    "trm_status": ["SUG"]
                  }
                },
                {
                  "terms": {
                    "tags": ["314"]
                  }
                }
              ]
            }
          }
        ]
      }
    }
         
        }
      ]

所以最终我的最终查询是:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "account_id": 1231231231
          }
        },
        {
          "multi_match": {
            "fuzziness": "AUTO",
            "query": "topics",
            "type": "most_fields",
            "fields": [
              "title.value^11",
              "parent_name^4",
              "*"
            ],
            "operator": "and"
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "dmn_id": [123,301]
                }
              },
              {
                "terms": {
                  "cat_id": [300]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "bool": {
                  "must": [
                    {
                      "terms": {
                        "trm_status": ["SUG"]
                      }
                    },
                    {
                      "terms": {
                       "tags": ["314"]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "must_not": {
        "terms": {
          "published_status.raw": ["Disabled"]
        }
      }
    }
  },
  "_source": [
    "node_type",
    "node_sub_type",
  ],
  "size": 5,
  "indices_boost": {
    "tables": 7,
    "datasets": 5,
    "terms": 3,
    "columns": 1
  },
  "sort": [
    "_score",
    {
      "tbl_popularity_info.NoOfTimeUsed": {
        "order": "desc",
        "unmapped_type": "long"
      }
    }
  ]
}

这就是我的问题逻辑对吗?还是我做错了?

u59ebvdq

u59ebvdq1#

是的,你的过滤条件看起来很好。您可以将第二部分(must_not)简化为这个,因为must_not已经具有AND语义。

{
      "bool": {
        "must_not": [
           {
             "terms": {
               "trm_status": ["SUG"]
             }
           },
           {
             "terms": {
              "tags": ["314"]
             }
           }
        ]
      }
    }

相关问题