Elasticsearch 通过分页按价格对 InnerHits 进行排序
Elasticsearch sorting InnerHits by price with pagination
我的 elasticsearch 索引中有几千个文档。
文档有一些嵌套的集合。其中之一是 "variants" 的嵌套集合。
JSON 文件之一的结构是:
{
"id" : 1,
"name": "lollipop",
"model_name": "candy",
"variants": [
{
"id": 1000,
"taste": "orange",
"gross_price": 13,
"stock_quantity": 15
},
{
"id": 1001,
"taste": "apple",
"gross_price": 7,
"stock_quantity": 1
},
{
"id": 1002,
"taste": "bannana",
"gross_price": 9,
"stock_quantity": 13
},
,
{
"id": 1003,
"taste": "pinaple",
"gross_price": 19,
"stock_quantity": 10
},
... and more and more ...
],
... and more and more ...
}
我的店铺在一个页面上只显示了48个产品。
我的索引共有 4800 种产品,所以我的商店有 100 页。
我在按价格排序时遇到问题。
在任何页面上,我想获得下一个 48 种价格最低的产品,其中 stock_quantity 大于 1。为了获得最轻的结果 - 我按变体使用 inner_hits。然后我只有那些符合我条件的变体。
我写了一些 elasticsearch 查询(第一页):
GET myshop_index/product/_search
{
"from": 0,
"size": 48,
"sort": [
{
"variants.gross_price": {
"mode": "min",
"order": "asc",
"nested_path": "variants"
}
}
],
"_source": {
"excludes": [
"variants"
]
},
"query": {
"bool": {
"filter": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"range": {
"variants.stock_quantity": {
"gt": 1
}
}
}
]
}
},
"path": "variants",
"inner_hits": {
"name": "variants",
"size": 10000
}
}
}
]
}
}
}
}
我得到了前 48 个产品及其变体,但这种排序不适用于我索引中的所有产品变体。这种排序方式如下:
- 获取前 48 个具有变体的产品
- 按升序排列变体价格
- return 结果
但我需要得到这个场景:
- 按 gross_price 升序
对我索引中的所有产品变体进行排序
- return 前 48 个产品及其变体(使用 inner_hits)按 gross_price 升序排序。
有什么想法吗?
好的,我解决了我的问题。我找到了 Nested Sorting 。这对我有用。尤其是第 5 点。 "The nested_filter in the sort clause is the same as the nested query in the main query clause. The reason is explained next." 然后解释
我的 elasticsearch 索引中有几千个文档。 文档有一些嵌套的集合。其中之一是 "variants" 的嵌套集合。 JSON 文件之一的结构是:
{
"id" : 1,
"name": "lollipop",
"model_name": "candy",
"variants": [
{
"id": 1000,
"taste": "orange",
"gross_price": 13,
"stock_quantity": 15
},
{
"id": 1001,
"taste": "apple",
"gross_price": 7,
"stock_quantity": 1
},
{
"id": 1002,
"taste": "bannana",
"gross_price": 9,
"stock_quantity": 13
},
,
{
"id": 1003,
"taste": "pinaple",
"gross_price": 19,
"stock_quantity": 10
},
... and more and more ...
],
... and more and more ...
}
我的店铺在一个页面上只显示了48个产品。 我的索引共有 4800 种产品,所以我的商店有 100 页。
我在按价格排序时遇到问题。
在任何页面上,我想获得下一个 48 种价格最低的产品,其中 stock_quantity 大于 1。为了获得最轻的结果 - 我按变体使用 inner_hits。然后我只有那些符合我条件的变体。
我写了一些 elasticsearch 查询(第一页):
GET myshop_index/product/_search
{
"from": 0,
"size": 48,
"sort": [
{
"variants.gross_price": {
"mode": "min",
"order": "asc",
"nested_path": "variants"
}
}
],
"_source": {
"excludes": [
"variants"
]
},
"query": {
"bool": {
"filter": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"range": {
"variants.stock_quantity": {
"gt": 1
}
}
}
]
}
},
"path": "variants",
"inner_hits": {
"name": "variants",
"size": 10000
}
}
}
]
}
}
}
}
我得到了前 48 个产品及其变体,但这种排序不适用于我索引中的所有产品变体。这种排序方式如下:
- 获取前 48 个具有变体的产品
- 按升序排列变体价格
- return 结果
但我需要得到这个场景:
- 按 gross_price 升序 对我索引中的所有产品变体进行排序
- return 前 48 个产品及其变体(使用 inner_hits)按 gross_price 升序排序。
有什么想法吗?
好的,我解决了我的问题。我找到了 Nested Sorting 。这对我有用。尤其是第 5 点。 "The nested_filter in the sort clause is the same as the nested query in the main query clause. The reason is explained next." 然后解释