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.

展开翻译

译文

我正在处理一个我们正在处理事件的应用程序。
因此,在Elasticsearch中,我们确实有一个 名为Event的文档。</ p>

以前,我们只有一种事件(5月13日上午9点到11点发生的独特事件),排序很简单(按start_date排序) )</ p>

我们最近添加了一个新功能,允许我们创建重复事件,这意味着我们现在在Elasticsearch(父子关系)中有2个级别。</ p>
\ n

我们可以举行父母活动,从5月12日下午2点到5月14日下午6点,与该活动相关,例如,我们有每天的孩子。 所以我们有:5月12日下午6点至下午6点,下午5点至下午6点至下午6点,下午6点至下午6点。</ p>

实际排序的问题是,当我们是5月12日时 晚上10点,我们会在列表顶部找到重复发生的事件,之后会发生这个独特的事件。</ p>

我想要排序最近的日期有一个 优先级更高。 在这种情况下,唯一事件应该是列表中的第一个。</ p>

为了实现这一点,我已经将节点子节点编入循环事件父节点,以便让子节点为start_date。

的想法是为每个重复事件从子节点中获取最近的日期,并使用每个唯一事件的start_date对其进行排序。</ p>

我没有很好的经验 elasticsearch,所以我有点卡住,我在文档中看到了很多信息(父子,嵌套对象,脚本等),但我不知道如何处理这种情况。</ p>

如果您有任何问题,我希望我能正确解释自己,随时问他们,我很乐意为您提供更多信息。</ p>
</ div>

2个回答

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"
        }
    }
}

展开翻译

译文



对于未来的googlers,以下是我修复它的方法。</ p>

必须使用脚本和排序 有了它,这是我正在使用的请求的部分例子</ p>

  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 if(isRecurrenceParent === false){
return params._source.timestamp;
}

def nearest = 0;

def lowestDiff = currentDate; \ n
for(int i = 0; i&lt; countChildren; i + +){
def child = params._source.children [i];

def diff = child.timestamp - currentDate;
if(diff&gt; 0&amp;&amp; 差异&lt; lowDiff){

nightDiff = diff;
nearest = child.timestamp;
}
}

返回最近;
“”“
},
”order“:”asc“
}
}
}
</ code> </ pre>
</ div>

First thing you should consider is parent and child docs are saved separately. It means Parent-Event::1 and Child-Event::1 are saved in a same shard (ES routes to shard where parent located by its id hash) but document types are different. So, you should fetch Parent and Children documents separately by query and sort by date. (You can make following queries in php if works)

P.S: I have also same situation but I had to implement in Java. So, I made a ES query builder (https://github.com/mashhur/java-elasticsearch-querybuilder) which supports parent-child relationship queries too, you can take a look for the reference.

// search child events and sort by date
GET events/_search {
"query": {
"has_parent": {
  "parent_type": "parent-event",
  "query": {
    "match_all": {}
   }
  },
  "sort": [{"start_date": {"desc"}}]
 }
}


// search parent events and sort by date
GET events/_search {
"query": {
"has_child": {
  "type": "child-event",
  "query": {
    "match_all": {}
   }
  },
  "sort": [{"start_date": {"desc"}}]
 }
}

展开翻译

译文



首先要考虑的是父文件和子文档是分开保存的。 这意味着Parent-Event :: 1和Child-Event :: 1保存在同一个分片中(ES路由到分片,其中父代通过其id哈希定位)但文档类型不同。 因此,您应该通过查询单独获取父文件和子文档并按日期排序。
(如果有效,您可以在php中进行以下查询)</ p>

PS:我也有同样的情况但我 必须在Java中实现。 所以,我创建了一个ES查询构建器( https://github.com/mashhur/java-elasticsearch -querybuilder )也支持父子关系查询,您可以查看引用。</ p>

  //搜索子事件并按日期排序
GET events / _search {
“query”:{
“has_parent”:{
“parent_type”:“parent-event”,
“query”:{
“match_all”:{}
}
},
“sort”:[{“start_date”:{“desc”}}]
}
}

//搜索父事件并按日期排序
GET events / _search {
“query”:{
“has_child”:{
“type”:“child-event”,
“query”:{
“match_all”:{}
}
},
“排序 “:[{”start_date“:{”desc“}}]
}
}
</ code> </ pre>
</ div>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!