ElasticSearch基于文档属性的条件'should'

tjrkku2a  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(153)

编写带有should子句的ElasticSearch查询的最佳方法是什么?其中should子句中的值根据文档的其他属性而不同?
下面是用例:在此应用程序中,最终用户搜索可从多个托运人处获得的产品。
根据某些业务规则,应用程序使用可以完成订单的发货人的ID填充should子句。查询将根据should中的shipper_id值返回相应的搜索结果。
现在,我们需要根据文档的一些属性指定第二个shipper_id值列表。如果文档具有属性"color": "red",则使用当前业务逻辑和具有相同shipper_id列表的相同should。但是对于color具有非红色值的文档,将有不同的shipper_id列表。
我们希望在搜索结果中返回这两组文档。返回的每个文档必须是(1)红色并使用现有的业务逻辑和shipper_id列表,或者(2)非红色并使用不同的shipper_id列表。
下面是没有新要求的查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                        "product_status": {
                            "query": "active"
                        }
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "shipper_id": "39"
                                }
                            },
                            {
                                "term": {
                                    "shipper_id": "40"
                                }
                            },
                            {
                                "term": {
                                    "shipper_id": "41"
                                }
                            },
                            {
                                "term": {
                                    "shipper_id": "48"
                                }
                            },
                            {
                                "term": {
                                    "shipper_id": "49"
                                }
                            },
                            {
                                "term": {
                                    "shipper_id": "50"
                                }
                            }
                        ]
                    }
                }
            ],
            "filter": [],
            "should": [],
            "must_not": []
        }
    },
    "sort": [
        {
            "last_modified": "DESC"
        }
    ],
    "from": 0,
    "size": 50
}
xa9qqrwz

xa9qqrwz1#

也许最好的方法是在should查询中有2个must

GET test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {}
            ]
          }
        },
        {
          "bool": {
            "must": [
              {}
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

相关问题