[英]How to execute ElasticSearch agg query from Apache Nifi using InvokeHTTP?
I want to execute the following query from Apache Nifi: 我想从Apache Nifi执行以下查询:
GET /myindex/_search
{
"size": 0,
"aggs": {
"range": {
"date_range": {
"field": "Datetime",
"ranges": [
{
"from": "2017-02-17T16:00:00Z||-1H/H",
"to": "2017-02-17T16:00:00Z||/H"
}
]
}
}
}
}
I want to get the value of doc_count
. 我想获取doc_count
的值。
I tried to use InvokeHTTP
and directly pasted the above-defined query into the field HTTP Method
. 我尝试使用InvokeHTTP
并将上面定义的查询直接粘贴到HTTP Method
字段中。 I also set Remote URL
to http://localhost:9200
. 我还将Remote URL
设置为http://localhost:9200
。 I connected InvokeHTTP
with PutFile
in order to save a response on the disk. 我将InvokeHTTP
与PutFile
连接InvokeHTTP
,以便将响应保存在磁盘上。 The relationship is Response
. 关系是Response
。
When I run InvokeHTTP
, it does not give me any error. 当我运行InvokeHTTP
,它不会给我任何错误。 However, it neither outputs any result (FlowFile for Response). 但是,它都不输出任何结果(用于响应的FlowFile)。 I am sure that the result is not an empty set, because I tested this query with curl
. 我确定结果不是空集,因为我使用curl
测试了此查询。
What is wrong with my approach? 我的方法有什么问题? Should I define HTTP Method
in some different way? 我应该以其他方式定义HTTP Method
吗?
For verbs like POST, PUT, and PATCH, you'd normally have the above JSON body as the content of a flow file and pass that to InvokeHttp, setting the correct verb and URL there. 对于POST,PUT和PATCH之类的动词,通常会将上述JSON正文作为流文件的内容,然后将其传递给InvokeHttp,并在其中设置正确的动词和URL。 However the InvokeHttp documentation states that the message body will not be sent for a GET verb. 但是,InvokeHttp文档指出不会为GET动词发送消息正文。
The good news is that the Elasticsearch REST API for the search endpoint supports both GET and POST. 好消息是,用于搜索端点的Elasticsearch REST API支持GET和POST。 From their latest doc: "Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well." 从他们最新的文档中可以得出:“ HTTP GET和HTTP POST均可用于通过主体执行搜索。由于并非所有客户端都支持通过主体进行GET,因此也允许进行POST。”
I would set the content of a flow file to the above JSON body (perhaps using GenerateFlowFile or ReplaceText), then use POST as the verb. 我将流文件的内容设置为上述JSON主体(也许使用GenerateFlowFile或ReplaceText),然后将POST用作动词。
There is a JsonQueryElasticsearch
processor, which supports JSON elasticsearch queries. 有一个JsonQueryElasticsearch
处理器,它支持JSON JsonQueryElasticsearch
查询。
Your query goes into the Query
field. 您的查询进入“ Query
字段。 The processor has hits
and aggregations
relations for you to process the result. 处理器具有hits
和aggregations
关系,以供您处理结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.