我有两个模型:Products和Skus,其中Product有一个或多个Skus,Sku恰好属于一个Product。它们有下列栏:
Product: id, title, content, category_id
Sku: id, product_id, price我希望能够在不同的搜索和排序配置中每页显示48个产品,但我很难将其转换到elasticsearch。
例如,我不清楚如何在title上搜索,同时根据每个Product的最低价格Sku对相关结果进行排序。我尝试了几种不同的方法,最近的方法是索引属于Sku的所有内容,然后进行如下搜索:
size: '48',
aggs: {
group_by_product: {
terms: { field: 'product_id' }
}
},
filter: {
and: [{
bool: {
must: { range: { price: { gte: 0, lte: 50 } } }
},{
bool: {
must: { terms: { category_id: [ 1, 2, 3, 4, 5, 6 ] } }
}
}]
},
query: {
fuzzy_like_this: {
fields: [ 'title', 'content' ],
like_text: 'Chair',
fuzziness: 1
}
}但是这给出了48个匹配的Skus,其中许多属于相同的Product,所以如果我在搜索之后尝试将它们组合起来,那么我的分页就关闭了。
处理这个用例的最好方法是什么?
更新
使用以下结构尝试使用嵌套方法:
{
size: '48',
query:
{ bool:
{ should:
{ fuzzy_like_this:
{ fields: [ 'title' ],
like_text: 'chair',
fuzziness: 1 },
},
{ must:
{ nested:
{ path: 'skus',
query:
{ bool:
{ must: { range: { price: { gte: 0, lte: 100 } } }
}
}
}
}
}
}
},
sort:
{ _score: 'asc',
'skus.price':
{ nested_path: 'skus',
nested_filter:
{ range: { 'skus.price': { gte: 0, lte: 100 } } },
order: 'asc',
mode: 'min'
}
}
}这可能更接近,但仍然不确定如何格式化它。以上给出的产品按价格订购,但似乎完全忽略了搜索字段。
发布于 2016-04-27 20:38:08
由于分页聚合结果是不可能的,即使将sku包含在product中是一种很好的方法,我还是会使用nested对象,这取决于对查询的需求。
作为一个示例查询:
GET /product/test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "whatever",
"fuzziness": 1,
"prefix_length": 3
}
}
},
{
"nested": {
"path": "skus",
"query": {
"range": {
"skus.price": {
"gte": 11,
"lte": 50
}
}
}
}
}
]
}
},
"sort": [
{
"skus.price": {
"nested_path": "skus",
"order": "asc",
"mode": "min"
}
}
]
}https://stackoverflow.com/questions/36899749
复制相似问题