哥妞 海淀山后有哥妞

ES-script-查询7点到9点的数据

2019-11-04

ES-script-查询7点到9点的数据

版本说明

ES 版本
6.2.4

在使用 ES 进行统计的时候,经常会用到查询某个小时的统计数据,比如统计每个小时的数据;比如计算早晚高峰的数据。

这些场景都可以使用 script 进行查询、统计。

ES 语句

{
"query": {
	"bool": {
		"filter": [{
			"range": {
				"datetime": {
					"from": "2019-10-01 00:00:00",
					"to": "2019-11-01 12:00:00",
					"include_lower": true,
					"include_upper": true,
					"boost": 1.0
				}
			}
		}, {
			"script": {
				"script": {
					"source": "(doc['datetime'].value.hourOfDay)>=17",
					"lang": "painless"
				},
				"boost": 1.0
			}
		}, {
			"script": {
				"script": {
					"source": "(doc['datetime'].value.hourOfDay)<19",
					"lang": "painless"
				},
				"boost": 1.0
			}
		}, {
			"exists": {
				"field": "linkdirname",
				"boost": 1.0
			}
		}],
		"adjust_pure_negative": true,
		"boost": 1.0
	}
},
"sort": [{
	"datetime": {
		"order": "desc"
	}
}],
"aggregations": {
	"sum_volume": {
		"sum": {
			"field": "volume"
		}
	}
} }

java 代码

/**
 * 计算早晚高峰流量
 * @param startDate
 * @param endDate
 * @param volume_max
 * @param startHour
 * @param endHour
 * @param sumId
 * @return
 */
private int computeMorningAndEveningPeak(String startDate, String endDate, SumAggregationBuilder volume_max,
                                         int startHour, int endHour, String sumId, String comField) throws ParseException {
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    boolQueryBuilder.filter(QueryBuilders.rangeQuery("datetime").gte(startDate).lte(endDate));
    boolQueryBuilder.filter(QueryBuilders.scriptQuery(new Script("(doc['datetime'].value.hourOfDay)>=" + startHour)));
    boolQueryBuilder.filter(QueryBuilders.scriptQuery(new Script("(doc['datetime'].value.hourOfDay)<" + endHour)));

    boolQueryBuilder.filter(QueryBuilders.existsQuery(comField));
    sourceBuilder.query(boolQueryBuilder);
    sourceBuilder.aggregation(volume_max).sort("datetime", SortOrder.DESC);
    // 查询ES
    SearchResponse response = ElasticsearchUtil.search(restHighLevelClient, sourceBuilder, ES_FLOW_5MIN, ES_DATA);
    return getSumFlowByResponse(response, sumId);
}

这个属性能取哪些值,取决于 org.joda.time.MutableDateTime,有需要的可以查看源码。

  1. mills
  2. Chronology
  3. Zone
  4. ZoneRetainFields
  5. Year
  6. Weekyear
  7. MonthOfYear
  8. WeekOfWeekyear
  9. DayOfYear
  10. DayOfMonth
  11. DayOfWeek
  12. HourOfDay
  13. MinuteOfHour
  14. SecondOfDay
  15. SecondOfMinute
  16. MillisOfDay
  17. MillisOfSecond

等等


1分也是爱


微信扫描二维码,关注公众号 (转载本站文章请注明作者和出处 哥妞-geniu)

上一篇 k-NN 算法

评论

Content