[010] 使用 Ruby 和 Elasticsearch 7.14 仅获取 10 个文档

简而言之

使用Ruby的Elasticsearch客户端在Elasticsearch 7.14中操作REST API。
此外,为了比较,还将在Kibana DevTool中提供查询语句。

这次将从索引“shakespeare”中进行“仅获取10个文档”的操作。

验证环境

使用了Elasticsearch + Kibana (7.14),按照下列方案注册了验证数据“Shakespeare”。

[00] 使用 Ruby 的 Elasticsearch 客户端包来操作 Elasticsearch 7.14 … 构建验证环境。

引用的资讯

实践 (Shí

Kibana DevTool 的情况

编码

GET shakespeare/_search
{

}

结果

image.png

对于Ruby

“代码” (Mention)

重要的部分是「如果 __FILE__ == $0 之后」。

我的简易客户端类可以完整复制(但仅适当替换为192.168.10.115)。

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
require 'multi_json'
require 'faraday'
require 'elasticsearch/api'
require 'active_support/core_ext' #! note_0004
require 'active_support' #! note_0005

class MySimpleClient
# note_0001
  include Elasticsearch::API
  CONNECTION = ::Faraday::Connection.new url: 'http://192.168.10.115:29200'
  def perform_request(method, path, params, body, headers = nil)
    #! note_0003
    CONNECTION.run_request \
      method.downcase.to_sym,
      path_with_params(path, params),
      (body ? MultiJson.dump(body): nil),
      {'Content-Type' => 'application/json'}
  end

  private

  def path_with_params(path, params)
    return path if params.blank?

    case params
    when String
      "#{path}?#{params}"
    when Hash
      "#{path}?#{params.to_query}"
    else
      raise ArgumentError, "Cannot parse params: '#{params}'"
    end
  end
end

if __FILE__ == $0

  client = MySimpleClient.new

  q = {}
  res = client.search index: 'shakespeare', body: {}
  h = JSON.parse(res)
  pp h
end

# note_0001: https://rubydoc.info/gems/elasticsearch-api
# note_0002: https://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Cluster/Actions#health-instance_method
# note_0003: client.cluster.health から呼び出されるので実装が必要である
# note_0004: 'active_support' を 'active_support/core_ext' に変更する.
#            APIドキュメントにある 'active_support' 指定だと次のエラーが発生してしまうためである.
#            tutorial.rb:26:in `path_with_params': undefined method `blank?' for {}:Hash (NoMethodError)
# note_0005: require 'active_support' が存在しないと次のエラーが発生してしまう.
#            /usr/local/bundle/gems/activesupport-6.0.4/lib/active_support/core_ext/object/json.rb:42:
#              in `to_json': uninitialized constant ActiveSupport::JSON (NameError)

结果一下子就出来了。 (The result came out all of a sudden.)

{"took"=>0,
 "timed_out"=>false,
 "_shards"=>{"total"=>1, "successful"=>1, "skipped"=>0, "failed"=>0},
 "hits"=>
  {"total"=>{"value"=>10000, "relation"=>"gte"},
   "max_score"=>1.0,
   "hits"=>
    [{"_index"=>"shakespeare",

     {"_index"=>"shakespeare",
      "_type"=>"_doc",
      "_id"=>"8",
      "_score"=>1.0,
      "_source"=>
       {"type"=>"line",
        "line_id"=>9,
        "play_name"=>"Henry IV",
        "speech_number"=>1,
        "line_number"=>"1.1.6",
        "speaker"=>"KING HENRY IV",
        "text_entry"=>"Shall daub her lips with her own childrens blood;"}},
     {"_index"=>"shakespeare",
      "_type"=>"_doc",
      "_id"=>"9",
      "_score"=>1.0,
      "_source"=>
       {"type"=>"line",
        "line_id"=>10,
        "play_name"=>"Henry IV",
        "speech_number"=>1,
        "line_number"=>"1.1.7",
        "speaker"=>"KING HENRY IV",
        "text_entry"=>"Nor more shall trenching war channel her fields,"}}]}}

验证

DevTool和Ruby的結果是否一致比較?

?看起来大家都持同样的意见

image.png
在Elasticsearch 7.14中,默认只获取10条数据。
广告
将在 10 秒后关闭
bannerAds