Skip to content

Instantly share code, notes, and snippets.

@karmi
Last active December 24, 2015 03:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karmi/6739853 to your computer and use it in GitHub Desktop.
Save karmi/6739853 to your computer and use it in GitHub Desktop.
Hello, Elasticsearch in Ruby
#!/usr/bin/env ruby
require File.expand_path '../__common__', __FILE__
# Print Curl-formatted trace
#
$client.transport.tracer = Logger.new(STDERR)
$client.transport.tracer.level = Logger::INFO
$client.transport.tracer.formatter = proc { |severity, datetime, progname, msg| "#{ANSI.ansi(msg, :yellow)}\n" }
p $client.index index: 'demo', type: 'question', id: 1, body: {
title: "How does Elasticsearch work?",
author: "John",
date: "2013-04-01"
}
p $client.index index: 'demo', type: 'question', id: 2, body: {
title: "How does Lucene work?",
author: "John",
date: "2013-05-28"
}
p $client.index index: 'demo', type: 'question', id: 3, body: {
title: "Does Elasticsearch support fuzziness?",
author: "Mary",
date: "2013-05-30"
}
#!/usr/bin/env ruby
require File.expand_path '../__common__', __FILE__
# curl -XGET http://localhost:9200/demo/_search
#
p $client.search index: 'demo'
#!/usr/bin/env ruby
require File.expand_path '../__common__', __FILE__
ap $client.search index: 'demo', q: 'elasticsearch'
#!/usr/bin/env ruby
require File.expand_path '../__common__', __FILE__
ap $client.search index: 'demo', q: '(title:elasticsearch^10 OR author:john) AND date:>=2013-04-01'
#!/usr/bin/env ruby
require File.expand_path '../__common__', __FILE__
ap $client.search index: 'demo', body: {
query: {
filtered: {
query: {
bool: {
should: [
{ match: { title: { query: 'elasticsearch', boost: 10 } } },
{ match: { author: 'john' } }
]
}
},
filter: {
range: {
date: { from: 2013-04-01 }
}
}
}
}
}
#!/usr/bin/env ruby
require File.expand_path '../__common__', __FILE__
require 'jbuilder'
@title = 'elasticsearch'
@author = 'john'
ap $client.search index: 'demo', body: (Jbuilder.encode do |_|
_.query do
_.filtered do
_.query do
_.bool do
_.should do
_.child! do
_.match do
_.title do
_.query @title
_.boost 10
end
end
end if @title
_.child! do
_.match do
_.author @author
end
end if @author
end
end
end
_.filter do
_.range do
_.date { _.from '2013-04-01' }
end
end
end
end
end)
require 'elasticsearch'
require 'awesome_print'
require 'logger'
require 'ansi'
logger = Logger.new(STDERR)
logger.formatter = proc do |severity, datetime, progname, msg|
ANSI.ansi(msg, :faint) + "\n"
end
$client = Elasticsearch::Client.new host: 'localhost:9200', logger: logger
# Download, extract and launch Elasticsearch
# ---------------------------------------------
curl -# http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.5.tar.gz | \
tar xz && \
./elasticsearch-0.90.5/bin/elasticsearch
while ! curl -s 'http://localhost:9200/_cluster/health?wait_for_status=yellow&pretty' && echo
do
clear; cat ./elasticsearch-0.90.5/logs/elasticsearch.log; echo
sleep 0.1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment