[014] 使用Ruby的elasticsearch进行文档搜索7.14版本

概述

使用Ruby的Elasticsearch客户端在Elasticsearch 7.14上操作REST API。
此外,为了比较,也将Kibana DevTool中的查询一并记录。

本次从索引“shakespeare”中进行“文档搜索”。
提取包含字段“text_entry”值为“ACT”的文档。

然而,在这里我们仍然只取得 Elasticsearch 的默认行为下的10个结果。

验证环境

使用弹性搜索+Kibana(7.14)进行了以下验证数据“Shakespeare”的注册。

[00] 使用Ruby的elasticsearch客户端软件包,尝试操作Elasticsearch 7.14… 构建验证环境版

借鉴的信息 de

实践

如果是Kibana DevTool的情况

填補

[004] 请查看使用 Ruby elasticsearch 7.14 获取的映射。

image.png

代码 (daima)

GET shakespeare/_search
{
  "query": {
    "query_string": { 
      "query": "text_entry:ACT"
    }
  }
}

结局

image.png

如果是Ruby的情况

代码 mǎ)

重要的部分是从”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 = {
    "query": {
      # note_0006
      "query_string": { 
        "query": "text_entry:ACT"
      }
    }
  }
  res = client.search 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)
# note_0006: mapping は次の通り
# {"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}}}}}}}

结果

{"took"=>94,
 "timed_out"=>false,
 "_shards"=>{"total"=>1, "successful"=>1, "skipped"=>0, "failed"=>0},
 "hits"=>
  {"total"=>{"value"=>296, "relation"=>"eq"},
   "max_score"=>8.445108,
   "hits"=>
    [{"_index"=>"shakespeare",

     {"_index"=>"shakespeare",
      "_type"=>"_doc",
      "_id"=>"4974",
      "_score"=>8.445108,
      "_source"=>
       {"type"=>"act",
        "line_id"=>4975,
        "play_name"=>"Henry VI Part 1",
        "speech_number"=>11,
        "line_number"=>"",
        "speaker"=>"VERNON",
        "text_entry"=>"ACT IV"}},
     {"_index"=>"shakespeare",
      "_type"=>"_doc",
      "_id"=>"5574",
      "_score"=>8.445108,
      "_source"=>
       {"type"=>"act",
        "line_id"=>5575,
        "play_name"=>"Henry VI Part 1",
        "speech_number"=>20,
        "line_number"=>"",
        "speaker"=>"CHARLES",
        "text_entry"=>"ACT V"}}]}}

验证

DevTool和Ruby的結果是否一致?

?看起来是一致的

image.png
广告
将在 10 秒后关闭
bannerAds