gpt4 book ai didi

php - Elasticsearch php : aggregations of documents with date interval

转载 作者:行者123 更新时间:2023-12-02 23:56:07 26 4
gpt4 key购买 nike

我正在尝试使用 Elasticsearch-php 6.0 构建分面搜索,但我很难弄清楚如何使用日期范围聚合。这是我正在尝试做的事情:

映射示例:

"mappings": {
"_doc": {
"properties": {
...
"timeframe": {
"properties": {
"gte": {
"type": "date",
"format": "yyyy"
},
"lte": {
"type": "date",
"format": "yyyy"
}
}
}
...

在我的文档中,我有这个属性:
"timeframe":[{"gte":"1701","lte":"1800"}]

我希望能够显示带有日期范围 slider 的构面,用户可以在其中输入范围(最小值 - 最大值)。理想情况下,这些最小值-最大值应该由 Elasticsearch 聚合在给定当前查询的情况下自动返回。

这是我试图用“伪代码”编写的聚合,给你一个想法:
"aggs": {
"date_range": {
"field": "timeframe",
"format": "yyyy",
"ranges": [{
"from": min(timeframe.gte),
"to": max(timeframe.lte)
}]
}
}

我想我需要使用日期范围聚合、最小/最大聚合和管道聚合,但是我对它们的了解越多,我就越困惑。我找不到如何将整个世界粘合在一起。

请记住,如果这不是实现此目的的正确方法,我可以更改映射和/或文档结构。

谢谢 !

最佳答案

至于我使用 ES 本身的官方“elasticsearch/elasticsearch”包,我可以通过这个文档找到一系列我需要的文档。
您需要阅读文档,因为您需要格式。

$from_date = '2018-03-08T17:58:03Z';
$to_date = '2018-04-08T17:58:03Z';
$params = [
'index' => 'your_index',
'type' => 'your_type',
'body' => [
'query' => [
'range' => [
'my_date_field' => [
//gte = great than or equal, lte = less than or equal
'gte' => $from_date,
// 'lte' => $to_date,
'format' => "yyyy-MM-dd||yyyy-MM-dd'T'HH:mm:ss'Z'",
'boost' => 2.0
]
]
],
]
];
$search = $client->search($params);

关于php - Elasticsearch php : aggregations of documents with date interval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53278272/

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