0
0

More than 5 years have passed since last update.

Elasticsearch への問い合わせ (Ruby)

Last updated at Posted at 2018-10-04

こちらで作成したデータに Ruby で問い合わせをします。
Elasticsearch へのデータ投入

Arch Linux でのライブラリーのインストール

yay -S ruby-rest-client
elastic_query.rb
#! /usr/bin/ruby
# -*- coding: utf-8 -*-
#
#   elastic_search.rb
#
#                   Oct/04/2018
#
# ---------------------------------------------------------------------
require 'restclient'
require 'json'
#
# ---------------------------------------------------------------------
STDERR.puts "*** 開始 ***"

URL = "http://localhost:9200/_search?q=tags:apple,moon"

str_json = RestClient.get URL

dict_aa = JSON.parse(str_json)

list_aa = dict_aa['hits']['hits']
list_aa.each {|unit|
    puts unit['_id']
    ss = unit['_source']
    puts "\t" + ss['name']
    puts "\t" + ss['title']
    puts "\t" + ss['content']
    str_out = ""
    ss['tags'].each {|tag|
        str_out += "\t" + tag
        }
    puts str_out
    }

STDERR.puts "*** 終了 ***"
# ---------------------------------------------------------------------
#

実行結果

$ ./elastic_query.rb 
*** 開始 ***
t0003
    渡辺五郎
    My Name Is Watanabe
    I love fish
    apple   orange  banana
t0002
    田中康夫
    My Name Is Tanaka
    I love cat
    Earth   Moon    Mars
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0