通过Ruby Elasticsearch 7.14获取映射

简述

使用 Ruby 的 Elasticsearch 客户端来操作 Elasticsearch 7.14 上的 REST API。
另外,为了比较,也附上使用 Kibana DevTool 进行的查询。

这次将进行针对索引”shakespeare”的”获取映射”操作。

验收环境

使用Elasticsearch + Kibana (7.14)进行了数据验证,其中包含了名为”Shakespeare”的数据。

使用Ruby的elasticsearch客户端包来操作Elasticsearch 7.14进行验证环境的搭建。

参考的资讯

实践

对于Kibana DevTool

代碼 mǎ)

GET shakespeare/_mapping

结果 (jié​guǒ)

image.png

对于Ruby来说

代码

关键部分是”if __FILE__ == $0 以后”。

MySimpleClient类需要完全复制代码,但是要适当修改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.indices.get_mapping index: 'shakespeare', body: q
  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)

最終的結論是

{"shakespeare"=>
  {"mappings"=>
    {"properties"=>
      {"line_id"=>{"type"=>"long"},
       "line_number"=>
        {"type"=>"text",
         "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}},
       "play_name"=>
        {"type"=>"text",
         "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}},
       "speaker"=>
        {"type"=>"text",
         "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}},
       "speech_number"=>
        {"type"=>"text",
         "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}},
       "text_entry"=>
        {"type"=>"text",
         "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}},
       "type"=>
        {"type"=>"text",
         "fields"=>{"keyword"=>{"type"=>"keyword", "ignore_above"=>256}}}}}}}

验证

DevTool和Ruby的結果是否相同?

image.png
bannerAds