面试题答案
一键面试实现思路
- 利用
bool
查询来组合多个条件。 - 在
bool
查询的must
子句中,分别添加针对客户城市和订单商品属性的查询条件。 - 对于多层嵌套结构,使用
nested
查询来深入到嵌套文档内部进行匹配。
ElasticSearch DSL查询语句
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "order_items",
"query": {
"nested": {
"path": "order_items.product.attributes",
"query": {
"bool": {
"must": [
{
"match": {
"order_items.product.attributes.color": "red"
}
}
]
}
}
}
}
}
},
{
"match": {
"customer.city": "New York"
}
}
]
}
}
}