lib/data_tables.rb in data_tables-0.1.9 vs lib/data_tables.rb in data_tables-0.1.10
- old
+ new
@@ -42,11 +42,11 @@
def define_datatables_action(controller, action, modelCls, columns, options = {})
conditions = options[:conditions] || []
scope = options[:scope] || :domain
named_scope = options[:named_scope]
named_scope_args = options[:named_scope_args]
- except = options[:except]
+ except = options[:except] || []
es_block = options[:es_block]
#
# ------- Ohm ----------- #
#
@@ -77,24 +77,27 @@
# ----------- Elasticsearch/Tire for Ohm ----------- #
#
elastic_index_name = "#{Tire::Model::Search.index_prefix}#{modelCls.to_s.underscore}"
logger.debug "*** (datatable:#{__LINE__}) Using tire for search #{modelCls} (#{elastic_index_name})"
- search_condition = elasticsearch_sanitation(search_condition, except)
- just_excepts = except ? elasticsearch_sanitation(nil, except) : "*"
- logger.debug "*** search_condition = #{search_condition}; sort by #{column_name_sym}:#{sort_dir}; domain=`#{domain.inspect}'"
-
retried = 0
if Tire.index(elastic_index_name){exists?}.response.code != 404
begin
controller_instance = self
results = Tire.search(elastic_index_name) do
- # retry #2 exclude search terms (and sorting) from search query
- if retried < 2
- query { string search_condition }
- else
- query { string just_excepts }
+ query do
+ boolean do
+ if search_condition && retried < 2
+ must { match :_all, search_condition, type: 'phrase_prefix' }
+ else
+ must { all }
+ end
+
+ except.each do |expt|
+ must_not { term expt[0].to_sym, expt[1].to_s }
+ end
+ end
end
# retry #1 exclude sorting from search query
sort{ by column_name_sym, sort_dir } if retried < 1
@@ -109,13 +112,19 @@
end.results
objects = results.map{ |r| modelCls[r._id] }.compact
total_display_records = results.total
-
total_records = Tire.search(elastic_index_name, search_type: 'count') do
- query { string just_excepts }
+ query do
+ boolean do
+ must { all }
+ except.each do |expt|
+ must_not { term expt[0].to_sym, expt[1].to_s }
+ end
+ end
+ end
filter(:term, domain: domain) unless domain.blank?
es_block.call(self) if es_block.respond_to?(:call)
end.results.total
rescue Tire::Search::SearchRequestFailed => e
if retried < 2
@@ -179,14 +188,14 @@
#
# ------- Elasticsearch ----------- #
#
define_method action.to_sym do
domain_name = ActiveRecord::Base.connection.schema_search_path.to_s.split(",")[0]
- logger.debug "*** Using ElasticSearch for #{modelCls.name}"
+ logger.debug "*** (datatables:#{__LINE__}) Using ElasticSearch for #{modelCls.name}"
objects = []
- condstr = ""
+ condstr = nil
unless params[:sSearch].blank?
sort_column_id = params[:iSortCol_0].to_i
sort_column_id = 1 if sort_column_id == 0
sort_column = columns[sort_column_id]
if sort_column && sort_column.has_key?(:attribute)
@@ -198,15 +207,24 @@
current_page = (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i rescue 0)+1
per_page = params[:iDisplayLength] || 10
column_name = columns[sort_column][:name] || 'message'
sort_dir = params[:sSortDir_0] || 'desc'
- condstr = elasticsearch_sanitation(condstr, except)
-
begin
query = Proc.new do
- query { string(condstr) }
+ query do
+ boolean do
+ if condstr
+ must { match :_all, condstr, type: 'phrase_prefix' }
+ else
+ must { all }
+ end
+ except.each do |expt|
+ must_not { term expt[0].to_sym, expt[1].to_s }
+ end
+ end
+ end
filter(:term, domain: domain_name) unless domain_name.blank?
es_block.call(self) if es_block.respond_to?(:call)
end
results = modelCls.search(page: current_page,
@@ -351,21 +369,9 @@
end
columnNames
end
end
- end
-
- def elasticsearch_sanitation(search_string, except)
- logger.debug "*** elasticsearch_sanitation.before = `#{search_string}'"
- search_string = '*' if search_string.blank?
- search_string.strip!
- numerical_search = (search_string.split.count > 1) ? "" : "OR *#{search_string.gsub(":","\\:")}*"
- search_string = "(\"*#{search_string}*\" #{numerical_search}) " unless search_string =~ /(\*|\")/
- exceptions = except.map { |f| "NOT #{f[0]}:\"#{f[1]}\""}.join(" AND ") if except
- search_string += " AND " + exceptions if exceptions
- logger.debug "*** elasticsearch_sanitation.after = `#{search_string}'"
- search_string
end
# gets the value for a column and row
def datatables_instance_get_value(instance, column)
if column[:special]