Created
May 16, 2012 13:12
-
-
Save lumpidu/2710254 to your computer and use it in GitHub Desktop.
Search ES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Searches ElasticSearch fulltext engine via Tire | |
# | |
# @param term Search term to be used | |
# It has to start with a prefix specifying the field(s) to be searched: | |
# e.g. | |
# _all:Apple | |
# converted_text:Apple | |
# field_summary:Apple, etc. | |
# | |
# Each Lucene search term can be used | |
# | |
# @param options Hash with the following keys: | |
# :page page number [1..] | |
# :per how many items per page [1..] | |
# :days limit search to N days [1..] | |
# :min_length min size of article in converted_text field [1..] | |
# :all show all articles [true/false] | |
# :sort sort field to be used ['id', 'feed_timestamp'] | |
# :fields array of fields that should be returned. If empty: return all available fields | |
# | |
def self.es_search(term, options={}) | |
n_days = options[:days] || 0 | |
n_days_ago = Time.now - n_days * 60 * 60 * 24 | |
min_length = options[:min_length] || 500 | |
if term.nil? or true == options[:all] | |
use_all = true | |
else | |
use_all = false | |
end | |
sort_field = options[:sort] | |
selected_fields =options[:fields] | |
s = Tire.search 'posts' do | |
fields(selected_fields) unless selected_fields.nil? | |
n_millis = n_days_ago.tv_sec * 1000 | |
query do | |
custom_score :script => "_score * Math.pow( (doc['feed_timestamp'].date.getMillis() - #{n_millis}) / (time()-#{n_millis}), 2 )" do | |
boolean do | |
unless use_all | |
# XXX make this configurable: - exact match/ OR / AND | |
should { string term, :default_operator => 'AND' } | |
#should { text '_all', term.split(':')[1], :operator => 'AND', :type => 'phrase' } | |
else | |
should { all } | |
end | |
# don't show texts that were not converted | |
must_not { string 'converted_text:N/A' } | |
end | |
end | |
end | |
# filter by time range | |
filter :range, :feed_timestamp => {:from => n_days_ago, :to => Time.now} if n_days != 0 | |
# filter out articles with less than a specific length | |
filter :numeric_range, :converted_text_size => {:from => min_length } | |
# XXX make sort field parametrized | |
# sort results, paginate | |
sort { by sort_field, 'desc' } unless sort_field.nil? | |
search_size = options[:per] || 10 | |
page = (((options[:page] || 1).to_i) -1) * search_size | |
from page | |
size search_size | |
end | |
puts s.to_curl | |
s.results | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment