如何在Elasticsearch中对对象数组应用组合筛选查询符合条件的商品
Elasticsearch 查询解决方案
前置注意事项
- 首先需要确认你的索引中
attributes字段的映射类型为nested,否则数组内对象的属性关联关系会被ES扁平化处理,导致跨对象匹配的错误结果。
完整查询DSL
{ "query": { "bool": { "should": [ { "bool": { "must": [ { "term": { "category.keyword": "blouses" } }, { "nested": { "path": "attributes", "query": { "bool": { "must": [ { "term": { "attributes.attribute.keyword": "women-blouse-neckline" } }, { "term": { "attributes.tag.keyword": "round-neck" } } ] } } } } ] } }, { "bool": { "must": [ { "term": { "category.keyword": "dresses" } }, { "nested": { "path": "attributes", "query": { "bool": { "must": [ { "term": { "attributes.attribute.keyword": "women-dress-neckline" } }, { "term": { "attributes.tag.keyword": "v-neck" } } ] } } } } ] } } ], "minimum_should_match": 1 } } }
逻辑说明
- 外层
bool.should用来匹配两类符合要求的商品,minimum_should_match: 1保证至少命中其中一类条件 - 每个类目对应的条件使用
bool.must同时匹配类目值和对应的属性筛选规则 - 嵌套查询
nested指定查询路径为attributes,保证同一个attribute对象内的两个字段同时满足筛选要求,避免匹配到不同对象下的属性组合 - 所有
term查询使用.keyword后缀是默认对应字段为text+keyword双类型,如果你直接设置对应字段为keyword类型,可以去掉后缀。
内容的提问来源于stack exchange,提问作者Naresh Sharma
相关产品推荐
相关产品推荐

