elasticsearch,Php,Laravel,elasticsearch" /> elasticsearch,Php,Laravel,elasticsearch" />

如何使用Elasticsearch和PHP获取数组的最后一条记录

如何使用Elasticsearch和PHP获取数组的最后一条记录,php,laravel,elasticsearch,Php,Laravel,elasticsearch,我正在我的laravel-应用程序中使用elasticsearch,我正在尝试使用范围-查询。我有一个公司数组,在不同时期有不同数量的员工,但我只对最新时期感兴趣,在本例中,这意味着员工数组的最后一项 因此,基本上阵列如下所示: "company" => [ "name" => "some company", "company_number" => "1234567"

我正在我的
laravel
-应用程序中使用
elasticsearch
,我正在尝试使用
范围
-查询。我有一个公司数组,在不同时期有不同数量的员工,但我只对最新时期感兴趣,在本例中,这意味着员工数组的最后一项

因此,基本上阵列如下所示:

"company" => [
   "name" => "some company",
   "company_number" => "1234567",
   "status" => "normal",
   "employees" => [
      "period_1" => [
         "amount" => 10
       ],
       "period_2" => [
         "amount" => 15
       ],
       "period_3" => [
         "amount" => 24
       ],
       etc etc...
    ]
 ]
{
  "company" : {
    "company_number" : "1234567",
    "name" : "some company",
    "current_employees" : {        <---
      "period" : "period_3",
      "amount" : 24
    },
    "employees" : {
      ...
    },
    ...
  }
}
因此,在前端,您可以输入一个最小值和一个最大值来搜索拥有一定数量员工的公司。然后在控制器中执行以下操作:

"query":{
    "bool": {
        "should" : [
          { "match" : { "company.status" : "normal" },
          {
           "range": {
              "company.employees": { // I WANT THE LAST ITEM FROM THIS ARRAY
                 "gte": "'. $min . '",
                 "lt" : "'.$max .'"
               }
            }
          }
        ]
    }
}
这基本上是可行的,但当然,没有给出employees数组的最后一条记录

我怎样才能解决这个问题?请帮忙

更新

好的,现在我添加了建议的代码:

  "query": {
      "bool": {
        "should" : [
          { "match" : { "company.status" : "normal" },
          {
           "range": {
              "company.employees": { // I WANT THE LAST ITEM FROM THIS ARRAY
                 "gte": "'. $min . '",
                 "lt" : "'.$max .'"
               }
            }
          }
        ]
      },
      "script": {
           "source": """        
                def period_keys = new ArrayList(ctx._source.company.employees.keySet());
                Collections.sort(period_keys);
                Collections.reverse(period_keys);
                
                def latest_period = period_keys[0];
                def latest_amount = ctx._source.company.employees[latest_period].amount;
                
                ctx._source.company.current_employees = ["period": latest_period, "amount": latest_amount];
                """
            }
        }
    }
但我得到了错误:
意外字符(“{”(code 123)):期望用逗号分隔对象条目

由于我还在学习,我必须说,我不知道发生了什么,Elasticsearch的错误消息非常可怕


不管怎样,有人有什么线索吗?提前谢谢

在运行时查找类似的内容非常困难,而且未进行优化

我假设给定公司的员工数量不会经常变化——这意味着当他们发生变化时(即,您更新该文档),您可以运行以下
\u update\u by\u query
脚本以获取最新期间的员工信息,并将其保存在公司级别,同时保持员工部分不变:

POST companies_index/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "source": """        
      def period_keys = new ArrayList(ctx._source.company.employees.keySet());
      Collections.sort(period_keys);
      Collections.reverse(period_keys);
      
      def latest_period = period_keys[0];
      def latest_amount = ctx._source.company.employees[latest_period].amount;
      
      ctx._source.company.current_employees = ['period': latest_period, 'amount': latest_amount];
    """
  }
}
一艘班轮:

POST companies_index/_update_by_query
{"query":{"match_all":{}},"script":{"source":"      def period_keys = new ArrayList(ctx._source.company.employees.keySet());\n      Collections.sort(period_keys);\n      Collections.reverse(period_keys);\n      \n      def latest_period = period_keys[0];\n      def latest_amount = ctx._source.company.employees[latest_period].amount;\n      \n      ctx._source.company.current_employees = ['period': latest_period, 'amount': latest_amount];"}}
请注意,当上述查询为空时,脚本将应用于索引中的所有文档。当然,您可以将其仅限于一家公司

调用后,您的文档将如下所示:

"company" => [
   "name" => "some company",
   "company_number" => "1234567",
   "status" => "normal",
   "employees" => [
      "period_1" => [
         "amount" => 10
       ],
       "period_2" => [
         "amount" => 15
       ],
       "period_3" => [
         "amount" => 24
       ],
       etc etc...
    ]
 ]
{
  "company" : {
    "company_number" : "1234567",
    "name" : "some company",
    "current_employees" : {        <---
      "period" : "period_3",
      "amount" : 24
    },
    "employees" : {
      ...
    },
    ...
  }
}
{
“公司”:{
“公司编号”:“1234567”,
“名称”:“某公司”,

“current_employees”:{在运行时查找类似的内容非常困难,而且未进行优化

我假设给定公司的员工数量不会经常变化——这意味着当他们发生变化时(即,您更新该文档),您可以运行以下
\u update\u by\u query
脚本以获取最新期间的员工信息,并将其保存在公司级别,同时保持员工部分不变:

POST companies_index/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "source": """        
      def period_keys = new ArrayList(ctx._source.company.employees.keySet());
      Collections.sort(period_keys);
      Collections.reverse(period_keys);
      
      def latest_period = period_keys[0];
      def latest_amount = ctx._source.company.employees[latest_period].amount;
      
      ctx._source.company.current_employees = ['period': latest_period, 'amount': latest_amount];
    """
  }
}
一艘班轮:

POST companies_index/_update_by_query
{"query":{"match_all":{}},"script":{"source":"      def period_keys = new ArrayList(ctx._source.company.employees.keySet());\n      Collections.sort(period_keys);\n      Collections.reverse(period_keys);\n      \n      def latest_period = period_keys[0];\n      def latest_amount = ctx._source.company.employees[latest_period].amount;\n      \n      ctx._source.company.current_employees = ['period': latest_period, 'amount': latest_amount];"}}
请注意,当上述查询为空时,脚本将应用于索引中的所有文档。当然,您可以将其仅限于一家公司

调用后,您的文档将如下所示:

"company" => [
   "name" => "some company",
   "company_number" => "1234567",
   "status" => "normal",
   "employees" => [
      "period_1" => [
         "amount" => 10
       ],
       "period_2" => [
         "amount" => 15
       ],
       "period_3" => [
         "amount" => 24
       ],
       etc etc...
    ]
 ]
{
  "company" : {
    "company_number" : "1234567",
    "name" : "some company",
    "current_employees" : {        <---
      "period" : "period_3",
      "amount" : 24
    },
    "employees" : {
      ...
    },
    ...
  }
}
{
“公司”:{
“公司编号”:“1234567”,
“名称”:“某公司”,

“当前员工”:{Hmm,
脚本
-thing返回
意外字符(“”(代码34)):希望用逗号分隔对象条目
…:-/更新我的答案--三重引号只能在kibana中使用;否则需要转义…嗯,我仍然得到错误…:-/奇怪。你确定运行了一行程序吗?是的,而且“脚本”应该在“查询”中“…嗯,
脚本
-返回
意外字符(''”(代码34)):希望用逗号分隔对象条目
。-/更新我的答案--三重引号只能在kibana中使用;否则需要转义…嗯,我仍然得到错误…:-/奇怪。你确定运行了一行程序吗?是的,还有”脚本“应该在“查询”中。。。