gpt4 book ai didi

php - 按输入数组的顺序从 elasticsearch 中检索信息

转载 作者:搜寻专家 更新时间:2023-10-31 20:41:13 25 4
gpt4 key购买 nike

似乎无法找到我的疑问的答案,所以我决定发布问题,看看是否有人可以帮助我。

在我的应用程序中,我有一个来自后端的 id 数组,并且已经按照我的需要进行了排序,例如:[0] => 23, [1] => 12, [2] => 45, [3] => 21

然后我使用术语过滤器“询问”elasticsearch 与该数组中存在的每个 id 对应的信息。问题是结果不是按照我发送的 id 的顺序出现的,所以结果会混淆,比如:[0] => 21, [1] => 45, [2] => 23, [3 ] => 12

请注意,我无法在 elasticsearch 中通过在后端对数组进行排序来进行排序。

我也不能在 php 中订购它们,因为我正在从 elasticsearch 检索分页结果,所以如果每个 oage 有 2 个结果,elasticsearch 只能给我 [0] => 21,[1] => 的信息45,所以我什至不能用 php 订购它们。

如何获得按输入数组排序的结果?有什么想法吗?

提前致谢

最佳答案

这是您可以使用自定义脚本评分的一种方法。

首先我创建了一些虚拟数据:

curl -XPUT "http://localhost:9200/test_index"

curl -XPOST "http://localhost:9200/test_index/_bulk " -d'
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 1 } }
{ "name" : "Document 1", "id" : 1 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 2 } }
{ "name" : "Document 2", "id" : 2 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 3 } }
{ "name" : "Document 3", "id" : 3 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 4 } }
{ "name" : "Document 4", "id" : 4 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 5 } }
{ "name" : "Document 5", "id" : 5 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 6 } }
{ "name" : "Document 6", "id" : 6 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 7 } }
{ "name" : "Document 7", "id" : 7 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 8 } }
{ "name" : "Document 8", "id" : 8 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 9 } }
{ "name" : "Document 9", "id" : 9 }
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : 10 } }
{ "name" : "Document 10", "id" : 10 }
'

我使用了 "id" 字段,尽管它是多余的,因为 "_id" 字段被转换为字符串,并且使用整数编写脚本更容易。

您可以使用 ids 过滤器通过 id 取回一组特定的文档:

curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
"filter": {
"ids": {
"type": "docs",
"values": [ 1, 8, 2, 5 ]
}
}
}'

但这些不一定按照您想要的顺序排列。使用script based scoring ,您可以根据文档 ID 定义自己的顺序。

这里我传入一个参数,该参数是将 id 与分数相关联的对象列表。评分脚本简单地循环遍历它们,直到找到当前文档 ID 并返回该文档的预定分数(如果未列出,则为 0)。

curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
"filter": {
"ids": {
"type": "docs",
"values": [ 1, 8, 2, 5 ]
}
},
"sort" : {
"_script" : {
"script" : "for(i:scoring) { if(doc[\"id\"].value == i.id) return i.score; } return 0;",
"type" : "number",
"params" : {
"scoring" : [
{ "id": 1, "score": 1 },
{ "id": 8, "score": 2 },
{ "id": 2, "score": 3 },
{ "id": 5, "score": 4 }
]
},
"order" : "asc"
}
}
}'

并且文档以正确的顺序返回:

{
"took": 11,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "docs",
"_id": "1",
"_score": null,
"_source": {
"name": "Document 1",
"id": 1
},
"sort": [
1
]
},
{
"_index": "test_index",
"_type": "docs",
"_id": "8",
"_score": null,
"_source": {
"name": "Document 8",
"id": 8
},
"sort": [
2
]
},
{
"_index": "test_index",
"_type": "docs",
"_id": "2",
"_score": null,
"_source": {
"name": "Document 2",
"id": 2
},
"sort": [
3
]
},
{
"_index": "test_index",
"_type": "docs",
"_id": "5",
"_score": null,
"_source": {
"name": "Document 5",
"id": 5
},
"sort": [
4
]
}
]
}
}

这是一个可运行的例子:http://sense.qbox.io/gist/01b28e5c038c785f0844abb7c01a71d69a32a2f4

关于php - 按输入数组的顺序从 elasticsearch 中检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21145264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com