我正在尝试获取特定索引的记录,我可以获得每次调用的默认10条记录。
但是,我也希望传递额外的身体参数,以便我可以对记录进行一些过滤和限制。
关注this链接后, 这是我正在使用的代码,
body = '{size:100}'
payload_hash = OpenSSL::Digest.new("sha256").hexdigest(body)
url = "https://myhost/index_name/_search"
signer = Aws::Sigv4::Signer.new(
service: 'es',
region: 'us-west-1',
access_key_id: 'access_key',
secret_access_key: 'secret_key'
)
uri
= URI(url)
t = Time.now.utc
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
headers = {
"host"=>'myhost',
"x-amz-date" => amzdate,
"x-amz-content-sha256" => body
}
signature = signer.sign_request(
http_method: 'POST',
url: url,
headers: headers,
body: body
)
require 'uri'
require 'net/http'
url = URI("https://myhost/index_name/_search")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url, 'size' => '100'})
request["host"] = signature.headers['host']
request["x-amz-content-sha256"] = signature.headers['x-amz-content-sha256']
request["x-amz-date"] = signature.headers['x-amz-date']
request["authorization"] = signature.headers["authorization"]
response = http.request(request)
result = response.read_body
result = JSON.parse(result)
result["hits"]["hits"].count
我总是只收到10条记录。 我也阅读了很多关于net http的文档。不知道我在这里做错了什么。
答案 0 :(得分:0)
我刚刚找到了解决方案,将其发布在下面以供将来查询
我在request['body']
之类的请求中添加了正文,并将其设置为标题。
正确的方法是将json主体传递给签名哈希并设置请求对象的body方法。
body = {"size": 1000}
payload_hash = OpenSSL::Digest.new("sha256").hexdigest(body)
url = "https://my-host/my-index/_search"
signer = Aws::Sigv4::Signer.new(
service: 'es',
region: 'us-west-1',
access_key_id: 'accesss_key',
secret_access_key: 'secret_key',
apply_checksum_header: false
)
uri
= URI(url)
t = Time.now.utc
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
headers = {
"host"=>'my-host',
"x-amz-date" => amzdate
}
signature = signer.sign_request(
http_method: 'POST',
url: url,
headers: headers,
body: body.to_json
)
require 'uri'
require 'net/http'
url = URI("https://my-host/my-index/_search")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request["host"] = signature.headers['host']
request["x-amz-date"] = signature.headers['x-amz-date']
request["authorization"] = signature.headers["authorization"]
request.body = body.to_json
response = http.request(request)
result = response.read_body
result = JSON.parse(result)
result["hits"]["hits"].count