doubi3929 2019-05-20 08:33
浏览 461

Elasticsearch查询日期排序父子关系(重复事件)

I’m currently working on an app where we are handling events. So, in Elasticsearch, we do have a document named Event.

Previously, we only had one kind of event (unique event happening the 13 May from 9 AM to 11 AM), the sorting was simple (sort by start_date with an order)

We recently added a new feature that allows us to create recurring events, that means that we now have 2 levels inside Elasticsearch (parent-child relation).

We can have a parent event that is from the 12 May from 2 PM to the 14 May from 6 PM, linked to that event, we have the children that are daily, for example. So we’d have: 12 May 2PM-6PM, 13 May 2PM-6PM, 14 May 2PM-6PM.

The problem with the actual sort is that when we are the 12 May at 10 PM, we’ll find the recurring event on top of the list and after that, will come the unique event.

I’d like to have a sorting where the nearest date has a higher priority. In that case, the unique event should have been the first on the list.

To make that happen, I have indexed node children on recurring event parent, in order to have the children start_date. The idea would be to get the nearest date out of the children node for every recurring event and sort that one with the start_date of every unique event.

I do not have a big experience with elasticsearch, so I’m kind of stuck, I saw a lot of information in the documentation (parent-child, nested objects, scripts, etc.) but I don’t know how to handle this case.

I hope that I have explained myself correctly if you have any questions, feel free to ask them, I would be happy to provide you with additional information.

  • 写回答

2条回答 默认 最新

  • dougong8012 2019-05-21 15:53
    关注

    For the future googlers, here's how I fixed it.

    Had to use scripts and sort with it, here's a partial exemple of the request I'm using

    GET /event/_search
    {
        "query" : {
          "match_all": {}
        },
        "sort" : {
            "_script" : {
                "type" : "number",
                "script": {
                  "lang": "painless",
                  "params": {
                    "currentDate": 1560230000
                  },
                  "source": """
                    def isRecurrenceParent = params._source.is_recurrence_parent;
                    def countChildren = params._source.children.length;
                    def currentDate = params.currentDate;
    
                    if (isRecurrenceParent === false) {
                      return params._source.timestamp;
                    }
    
                    def nearest = 0;
    
                    def lowestDiff = currentDate;
    
                    for (int i = 0; i < countChildren; i++) {
                      def child = params._source.children[i];
    
                      def diff = child.timestamp - currentDate;
                      if (diff > 0 && diff < lowestDiff) {
                        lowestDiff = diff;
                        nearest = child.timestamp;
                      }
                    }
    
                    return nearest;
    """
                },
                "order" : "asc"
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型