Skip to content
Snippets Groups Projects
Commit 919cb059 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

move some search and query building logic for anothers methods

parent 4931a1ff
No related branches found
No related tags found
No related merge requests found
......@@ -92,33 +92,11 @@ module OrientDb
# FULLTEXT ENGINE LUCENE
# METADATA {"analyzer":"org.apache.lucene.analysis.br.BrazilianAnalyzer"}
def search(params)
# mount cache_key with params
valid_params = %w(query order subject type school_level year)
cache_key = "search_result/"
valid_params.each do |param|
if params[param].class == Array
cache_key += params[param].join('-')
else
cache_key += params[param].to_s
end
cache_key += '/' unless (param == valid_params.last)
end
cache_key = build_search_cache_key params
# get results in cache or search
Rails.cache.fetch(cache_key, expires_in: 10.minutes) do
qry = params[:query]
order = order_by(params[:order])
query = "SELECT FROM LearningObject WHERE [name, description, author] LUCENE '#{qry}'"
query += " AND out('IsAbout') CONTAINS (name in ['" + params[:subject].join("','") + "'])" unless params[:subject].blank?
unless params[:school_level].blank?
params[:school_level] << 'Ensino Fundamental Inicial' << 'Ensino Fundamental Final' if params[:school_level].include? 'Ensino Fundamental'
query += " AND out('IsAbout') CONTAINS (name in ['" + params[:school_level].join("','") + "'])"
end
query = "SELECT * FROM (" + query + ") WHERE " + fetch_types(params[:type]) unless params[:type].blank?
query = "SELECT * FROM (" + query + ") WHERE " + fetch_year(params[:year]) unless params[:year].blank?
query = "SELECT * FROM (" + query + ") ORDER BY #{order}"
query = "SELECT @rid.asString(), last_modified FROM (" + query + ")"
query = build_search_query params
connection.query query, limit: 10000
end
end
......@@ -184,5 +162,36 @@ module OrientDb
"LearningObject"
end
def build_search_query(params={})
query = "SELECT FROM LearningObject WHERE [name, description, author] LUCENE '#{params[:query]}'"
query += " AND out('IsAbout') CONTAINS (name in ['" + params[:subject].join("','") + "'])" unless params[:subject].blank?
unless params[:school_level].blank?
params[:school_level] << 'Ensino Fundamental Inicial' << 'Ensino Fundamental Final' if params[:school_level].include? 'Ensino Fundamental'
query += " AND out('IsAbout') CONTAINS (name in ['" + params[:school_level].join("','") + "'])"
end
query = "SELECT * FROM (" + query + ") WHERE " + fetch_types(params[:type]) unless params[:type].blank?
query = "SELECT * FROM (" + query + ") WHERE " + fetch_year(params[:year]) unless params[:year].blank?
query = "SELECT * FROM (" + query + ") ORDER BY #{order_by(params[:order])}"
query = "SELECT @rid.asString(), last_modified FROM (" + query + ")"
query
end
def search_params
%w(query order subject type school_level year)
end
def build_search_cache_key(params={})
cache_key = "search_result/"
search_params.each do |param|
if params[param].class == Array
cache_key += params[param].join('-')
else
cache_key += params[param].to_s
end
cache_key += '/' unless (param == search_params.last)
end
cache_key
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment