|
| 1 | +require 'sinatra' |
| 2 | +require 'json' |
| 3 | +require 'elasticsearch' |
| 4 | +require_relative './games_data.rb' |
| 5 | + |
| 6 | +class ElasticSinatraApp < Sinatra::Base |
| 7 | + use ElasticAPM::Middleware |
| 8 | + |
| 9 | + before do |
| 10 | + @client = Elasticsearch::Client.new( |
| 11 | + hosts: ENV['ELASTICSEARCH_HOSTS'] || 'localhost:9200' |
| 12 | + ) |
| 13 | + end |
| 14 | + |
| 15 | + get '/' do |
| 16 | + response = @client.cluster.health |
| 17 | + json_response(response) |
| 18 | + end |
| 19 | + |
| 20 | + get '/ingest' do |
| 21 | + unless @client.indices.exists(index: 'games') |
| 22 | + @client.indices.create(index: 'games') |
| 23 | + end |
| 24 | + |
| 25 | + ElasticAPMExample::GAMES.each_slice(250) do |slice| |
| 26 | + @client.bulk( |
| 27 | + body: slice.map do |game| |
| 28 | + { index: { _index: 'games', data: game } } |
| 29 | + end |
| 30 | + ) |
| 31 | + end |
| 32 | + json_response(status: 'ok') |
| 33 | + end |
| 34 | + |
| 35 | + get '/search/:query' do |
| 36 | + query = sanitize(params[:query]) |
| 37 | + response = search_elasticsearch(query) |
| 38 | + json_response(response['hits']['hits']) |
| 39 | + end |
| 40 | + |
| 41 | + get '/delete' do |
| 42 | + response = @client.delete_by_query( |
| 43 | + index: 'games', |
| 44 | + body: { |
| 45 | + query: { match_all: {} } |
| 46 | + } |
| 47 | + ) |
| 48 | + json_response(response) |
| 49 | + end |
| 50 | + |
| 51 | + get '/delete/:id' do |
| 52 | + id = sanitize(params[:id]) |
| 53 | + |
| 54 | + begin |
| 55 | + response = @client.delete(index: 'games', id: id) |
| 56 | + json_response(response) |
| 57 | + rescue Elasticsearch::Transport::Transport::Errors::NotFound => e |
| 58 | + json_response(e, 404) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + get '/update' do |
| 63 | + response = [] |
| 64 | + docs = search_elasticsearch |
| 65 | + |
| 66 | + docs['hits']['hits'].each do |doc| |
| 67 | + response << @client.update( |
| 68 | + index: 'games', |
| 69 | + id: doc['_id'], |
| 70 | + body: { |
| 71 | + doc: { |
| 72 | + modified: DateTime.now |
| 73 | + } |
| 74 | + } |
| 75 | + ) |
| 76 | + end |
| 77 | + json_response(response) |
| 78 | + end |
| 79 | + |
| 80 | + get '/error' do |
| 81 | + begin |
| 82 | + @client.delete(index: 'games', id: 'somerandomid') |
| 83 | + rescue Elasticsearch::Transport::Transport::Errors::NotFound => e |
| 84 | + json_response(e, 404) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + get '/doc/:id' do |
| 89 | + response = @client.get(index: 'games', id: sanitize(params[:id])) |
| 90 | + json_response(response) |
| 91 | + end |
| 92 | + |
| 93 | + private |
| 94 | + |
| 95 | + def json_response(response, code = 200) |
| 96 | + [ |
| 97 | + code, |
| 98 | + { 'Content-Type' => 'application/json' }, |
| 99 | + [response.to_json] |
| 100 | + ] |
| 101 | + end |
| 102 | + |
| 103 | + def sanitize(params) |
| 104 | + Rack::Utils.escape_html(params) |
| 105 | + end |
| 106 | + |
| 107 | + def search_elasticsearch(query = '') |
| 108 | + @client.search( |
| 109 | + index: 'games', |
| 110 | + body: |
| 111 | + { |
| 112 | + query: { multi_match: { query: query } } |
| 113 | + } |
| 114 | + ) |
| 115 | + end |
| 116 | +end |
0 commit comments