如何在ElasticSearch中按数组大小对项目进行排序? [英] How to sort items by array size in ElasticSearch?
本文介绍了如何在ElasticSearch中按数组大小对项目进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有300万个具有这种结构的物品:
I have 3 millions items with this structure:
{
"id": "some_id",
"title": "some_title",
"photos": [
{...},
{...},
...
]
}
某些商品的照片
字段可能为空:
Some items may have empty photos
field:
{
"id": "some_id",
"title": "some_title",
"photos": []
}
我想按照片数量排序,以使没有照片的元素位于列表的末尾.
I want to sort by the number of photos to result in elements without photos were at the end of the list.
我有一个可行的解决方案,但是在300万个项目上却很慢:
I have the one working solution but it's very slow on 3 million items:
GET myitems/_search
{
"filter": {
...some filters...
},
"sort": [
{
"_script": {
"script": "_source.photos.size()",
"type": "number",
"order": "desc"
}
}
]
}
此查询执行55秒.如何优化此查询?
This query executes 55 seconds. How to optimize this query?
推荐答案
问题已通过Transform指令解决.现在我有一个映射:
Problem was solved with Transform directive. Now I have a mapping:
PUT /myitems/_mapping/lol
{
"lol" : {
"transform": {
"lang": "groovy",
"script": "ctx._source['has_photos'] = ctx._source['photos'].size() > 0"
},
"properties" : {
... fields ...
"photos" : {"type": "object"},
"has_photos": {"type": "boolean"}
... fields ...
}
}
}
现在,我可以按存在的照片对项目进行排序:
Now I can sort items by photos existence:
GET /test/_search
{
"sort": [
{
"has_photos": {
"order": "desc"
}
}
]
}
不幸的是,这将导致完全重新编制索引.
Unfortunately, this will cause full reindexation.
这篇关于如何在ElasticSearch中按数组大小对项目进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文