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

Alv*_*ves 3 php arrays elasticsearch

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

在我的应用程序中,我有一个来自后端的 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 订购它们。

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

提前致谢

Slo*_*ens 5

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

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

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 }
'
Run Code Online (Sandbox Code Playgroud)

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

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

curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
   "filter": {
      "ids": {
         "type": "docs",
         "values": [ 1, 8, 2, 5 ]
      }
   }
}'
Run Code Online (Sandbox Code Playgroud)

但这些不一定按照您想要的顺序排列。使用基于脚本的评分,您可以根据文档 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"
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

并按正确顺序返回文件:

{
   "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
            ]
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

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