我是 Elasticsearch 的新手,我正在尝试在 R 中运行一个基本查询。因为我需要一个 API 密钥,所以我无法在 R 中使用任何可用的 Elasticsearch 库。
我可以检索弹性搜索索引中的所有文档,但似乎无法运行自定义查询。我认为这一定是因为我的GET
请求格式不正确。这是我到目前为止所拥有的:
json_query <- jsonlite::toJSON('{
"query": {
"match" : {
"LastName": "Baggins"
}
}
}
')
我尝试将 my_query 添加为body=
参数,但它只是不运行查询(而是检索 10000 个文档)。我最终尝试将其粘贴到 url 参数:
get_scroll_id <- httr::GET(url =paste("'https://Myserver:9200/indexOfInterest/_search?scroll=1m&size=10000'",my_query),
encoding='json',
add_headers(.headers = c("Authorization" = "ApiKey ****", "Content-Type" = "application/json")),
config=httr::config(ssl_verifypeer = FALSE,ssl_verifyhost = FALSE))
scroll_data <- fromJSON(content(get_scroll_id, as="text"))
这给了我错误:
Error in curl::curl_fetch_memory(url, handle = handle) :
Protocol "" not supported or disabled in libcurl
我还尝试将查询添加为查询参数,如下所示:
get_scroll_id <- httr::GET(url ='https://Myserver:9200/indexOfInterest/_search?scroll=1m&size=10000',
query= json_query,
encoding='json',
add_headers(.headers = c("Authorization" = "ApiKey *****==", "Content-Type" = "application/json")),
verbose(),
config=httr::config(ssl_verifypeer = FALSE,ssl_verifyhost = FALSE))
这给了我输出:
GET https://Myserver:9200/indexOfInterest/_search?{
"query": {
"match" : {
"LastName" : "Baggins"
}
}
}
Options:
* ssl_verifypeer: FALSE
* ssl_verifyhost: FALSE
* debugfunction: function (type, msg)
{
switch(type + 1, text = if (info) prefix_message("* ", msg), headerIn = prefix_message("<- ", msg), headerOut = prefix_message("-> ", msg), dataIn = if (data_in) prefix_message("<< ", msg, TRUE), dataOut = if (data_out) prefix_message(">> ", msg, TRUE), sslDataIn = if (ssl && data_in) prefix_message("*< ", msg, TRUE), sslDataOut = if (ssl && data_out) prefix_message("*> ", msg, TRUE))
}
* verbose: TRUE
Headers:
* Authorization: ApiKey *****==
* Content-Type: application/json
查看 Elasticsearch 文档,curl 如下:
curl 'localhost:9200/get-together/event/_search?pretty&scroll=1m' -d ' {
"query": {
"match" : {
"LastName" : "Baggins"
}
}
}'
如何为 Elasticsearch 创建正确的命令?