如何使用Elasticsearch中的Java API按月份划分响应并添加计数器。

huangapple 未分类评论50阅读模式
标题翻译

How to divide response by months and add counter with Java API in Elasticsearch

问题

{
    "我有几千份不同时间和三个serviceId的文档在elasticsearch中。是否可能按月份划分所有这些文档,并计数?
文档模型:

{
    "dateTime" : "2011-03-13T11:34:14.869Z",
    "organizationId" : "1a4b7625-dcec-4326-b7dc-96e038b31d0b",
    "accountId" : "a9bfcced-ddaa-477c-8021-18364ac320ee",
    "processInstanceId" : "711e73cb-1286-461f-810f-d5791f71101f",
    "serviceConfigurationId" : "e8be10e8-2ba2-4365-bfb0-96052d90be7e",
    "clusterId" : "542096b3-3982-4d4b-bce1-44b1f988cf7f",
    "serviceId" : "asdf"
}

我正在使用范围搜索(例如一年),希望它执行类似于以下操作:

{
    "_index": "test",
    "_type": "_doc",
    "_id": "Jc0H03AB-y_MhSAimo7v",
    "_score": null,
    "_month": {
        "Jan": [
            {
                "serviceId": "asdf",
                "counter": 4
            },
            {
                "serviceId": "zxcv",
                "counter": 9
            }
        ],
        "Feb":[
        {
                "serviceId": "asdf",
                "counter": 12
            },
            {
                "serviceId": "zxcv",
                "counter": 11
            }
        ], 等等
    }
}

我发现如何使用Java API创建范围查询。

RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dateTime").from(LocalDateTime.parse("2011-11-09T10:30")).to(LocalDateTime.parse("2022-11-09T10:30")).timeZone("Z");
sourceBuilder.query(rangeQueryBuilder);
searchRequest.source(sourceBuilder);
client().search(searchRequest, RequestOptions.DEFAULT);

也许这是不可能的,我在试图寻找解决方案时在浪费时间吗?
}
英文翻译

I have several thousand of docs in elasticsearch with different time and three serviceId's. Is it possible to divide all these docs by month with counter?
Document model:

{
	"dateTime" : "2011-03-13T11:34:14.869Z",
    "organizationId" : "1a4b7625-dcec-4326-b7dc-96e038b31d0b",
    "accountId" : "a9bfcced-ddaa-477c-8021-18364ac320ee",
    "processInstanceId" : "711e73cb-1286-461f-810f-d5791f71101f",
    "serviceConfigurationId" : "e8be10e8-2ba2-4365-bfb0-96052d90be7e",
    "clusterId" : "542096b3-3982-4d4b-bce1-44b1f988cf7f",
    "serviceId" : "asdf"
}

I'm doing a SearchRequest with range (for example a year), and expect it to do something like this:

{
	"_index": "test",
	"_type": "_doc",
	"_id": "Jc0H03AB-y_MhSAimo7v",
	"_score": null,
	"_month": {
		"Jan": [
			{
				"serviceId": "asdf",
				"counter": 4
			},
			{
				"serviceId": "zxcv",
				"counter": 9
			}
		],
		"Feb":[
		{
				"serviceId": "asdf",
				"counter": 12
			},
			{
				"serviceId": "zxcv",
				"counter": 11
			}
		], etc
	}
}

I found how to create a range query with Java API.

RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dateTime").from(LocalDateTime.parse("2011-11-09T10:30")).to(LocalDateTime.parse("2022-11-09T10:30")).timeZone("Z");
sourceBuilder.query(rangeQueryBuilder);
searchRequest.source(sourceBuilder);
client().search(searchRequest, RequestOptions.DEFAULT);

Maybe it's impossible and I'm wasting my time trying to find a solution?

答案1

得分: 0

范围查询将返回日期时间在给定范围内的文档。无法在查询部分按月份对其进行分组。可以使用date_histogram 聚合top_hits 聚合来实现按月份分组。

{
  "size": 0,
  "aggs": {
    "filter_year": { 
      "filter": {
        "range": {
          "dateTime": {
            "gte": "2011-01-01",
            "lte": "2011-12-31"
          }
        }
      },
      "aggs": {
        "month": {
          "date_histogram": { 
            "field": "dateTime",
            "format": "MMM",
            "interval": "month"
          },
          "aggs": {
            "documents": {
              "top_hits": {
                "_source": [
                  "clusterId"
                ],
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}
英文翻译

Range query will return documents where dateTime falls within givens. You cannot group them by months in query part. This can be done using date_histogram aggregation and top_hits aggregationb

{
  "size": 0,
  "aggs": {
    "filter_year": {  --> filter documents which fall in given interval
      "filter": {
        "range": {
          "dateTime": {
            "gte": "2011-01-01",
            "lte": "2011-12-31"
          }
        }
      },
      "aggs": {
        "month": {
          "date_histogram": {  --> group documents on monthly interval
            "field": "dateTime",
            "format": "MMM",
            "interval": "month"
          },
          "aggs": {
            "documents": {
              "top_hits": { --> return documents  under months
                "_source": [
                  "clusterId"
                ],
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

huangapple
  • 本文由 发表于 2020年3月16日 20:53:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/60706394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定