Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@OriPekelman
Created April 4, 2012 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OriPekelman/2304220 to your computer and use it in GitHub Desktop.
Save OriPekelman/2304220 to your computer and use it in GitHub Desktop.
Minimal File Indexer Elastic Search Example (ruby)
source :rubygems
gem 'tire'
gem 'sinatra'
require "sinatra"
require "tire"
get '/*' do
"<form><input type=\"search\" name=\"q\" value=\"#{params[:q]}\"> <input type=\"submit\" value=\"Search\" /></form>#{params[:q].nil? ? 'You need to search for something, try \'tire\'' : results(params[:q]).join} "
end
def results(q)
s = Tire.search('files') {
query { string "content:#{q}" }
highlight :title}
s.results.collect do |doc|
"<h1>#{doc.file_name}</h1><pre>#{ doc.content}</pre>"
end
end
require 'tire'
Tire.index 'files' do
delete
create
Dir["**/*"].select{|file| !File.directory?(file) && !(%x(file #{file}) =~ /text/).nil?}.collect{|f| store :file_name=>f, :content=>IO.read(f) }
refresh
end

This is ruby. This example assumes you are on something like a unix/linux system (or the part that sees if the files are text based or not will fail)

To run the example:

Save the three files to a directory

Install ruby on the command line type:

gem install bundler
bundle

run

ruby indexer.rb

to index all the files in the current directory and in its subdirectories that are text based

run

ruby index.rb

to get a nice web page with a search form (visit http://localhost:4567 to see the result)

@ericTsiliacos
Copy link

Does this work? I ran ruby indexer.rb first. Then when I ran index.rb the following threw an error:

 s.results.each do |doc|

The error message was:

Tire::Search::SearchRequestFailed at /
500 : {"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[DuHC-F8lTEyBduM4jTkuvw][files][0]: SearchParseException[[files][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"query_string":{"query":"content:"}}}]]]; nested: QueryParsingException[[files] Failed to parse query [content:]]; nested: ParseException[Cannot parse 'content:': Encountered "" at line 1, column 8.\nWas expecting one of:\n "(" ...\n "" ...\n ...\n ...\n ...\n ...\n "[" ...\n "{" ...\n ...\n ]; nested: ParseException[Encountered "" at line 1, column 8.\nWas expecting one of:\n "(" ...\n "" ...\n ...\n ...\n ...\n ...\n "[" ...\n "{" ...\n ...\n ]; }{[DuHC-F8lTEyBduM4jTkuvw][files][3]: SearchParseException[[files][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"query_string":{"query":"content:"}}}]]]; nested: QueryParsingException[[files] Failed to parse query [content:]]; nested: ParseException[Cannot parse 'content:': Encountered "" at line 1, column 8.\nWas expecting one of:\n "(" ...\n "" ...\n ...\n ...\n ...\n ...\n "[" ...\n "{" ...\n ...\n ]; nested: ParseException[Encountered "" at line 1, column 8.\nWas expecting one of:\n "(" ...\n "" ...\n ...\n ...\n ...\n ...\n "[" ...\n "{" ...\n ...\n ]; }]","status":500}

@OriPekelman
Copy link
Author

Yup this basically works, it just doesn't like the query string to be empty.. so http://localhost:4567?q=index (for example) will work, hey this is just an example ...

@ericTsiliacos
Copy link

Thank you so much! It is working now =) Great small example to get me started. Really appreciate it.

@OriPekelman
Copy link
Author

updated example to ignore nil query string and even added a nice submit button. Less minimal now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment