编写带有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
}
1条答案
按热度按时间xa9qqrwz1#
也许最好的方法是在
should
查询中有2个must
。