diff --git a/app/controllers/search/solr_controller.rb b/app/controllers/search/solr_controller.rb deleted file mode 100644 index 6054b17d6d37b4bf0a9f43abcd70cd09801ffe4b..0000000000000000000000000000000000000000 --- a/app/controllers/search/solr_controller.rb +++ /dev/null @@ -1,127 +0,0 @@ -class Search::SolrController < ApplicationController - - def search - queryParams = {:q => '*:*', :fq => params[:qry]} - - results = solr_client.get 'select', - :params => merge_search_parameters(queryParams, solr_search_default_parameters) - - objectsFound1 = select_items_from_results(results) - #@numFound = results["response"]["numFound"] - @numFound =objectsFound1.length - - per_page=10 - @npagnac=10 - if(params[:paginacao]) - per_page=params[:paginacao].to_i - @npagnac=per_page - end - - if(params[:page]) - #params[:page] = 1 - else - params[:page] = 1 - end - - current_page= params[:page].to_i - - if params[:sort] - @ordena=params[:sort] - end - if params[:sort] == "author" - objectsFound1 = sort_author(objectsFound1); - elsif params[:sort] == "publicationasc" - objectsFound1 = objectsFound1.sort_by{|item| [ item['search.resourceid'] ]} - elsif params[:sort] == "publicationdesc" - objectsFound1 = objectsFound1.sort_by{|item| -item['search.resourceid'] } - elsif params[:sort] == "relevance" - items= objectsFound1.collect{ |x| Ranking::Item.new("",1,1,1, x) } #puts each object in an item - - rater = Ranking::Rater.new( - Ranking::Strategies::BasicRater.new( - #define sorting weights - positionWeight = 1000, - useWeight = 1, - likeWeight = 100 - ) - ) - - a = rater.sortByRate(items) # Returns sorted array of items - objectsFound1= a.collect{ |x| x.info } #get only the info fields(objects) - - elsif params[:sort] == "title" - objectsFound1 = sort_title(objectsFound1); - end - # current_page, per_page, total_results - @objectsFound = WillPaginate::Collection.create(current_page, per_page, objectsFound1.length) do |pager| - @start = (current_page-1)*per_page - pager.replace (objectsFound1.to_a[@start,per_page]) - end - - #search.resourceid - #for item in objectsFound - # Busca no orientDb (rid,classe,rate) - #end - end - - private - - #Resource types: - # 2 = Item - # 3 = Collection - # 4 = Community - - def select_items_from_results(results) - resultList = Set.new - results["response"]["docs"].select { |r| - r["search.resourcetype"].to_i.equal? 2 - }.each do |i| - resultList.add i - end - return resultList - end - - def solr_client - return RSolr.connect :url => 'http://portalmecdev.c3sl.ufpr.br:8080/solr/search/' - end - - def merge_search_parameters(p1, p2) - return p1.merge(p2) { |key,oldval,newval| key = [newval,oldval].flatten } - end - - def solr_search_default_parameters - return { - "fl"=>[" - search.resourcetype, search.resourceid, - handle, author, title, dc.description - "], - "type"=>"0", - "start"=>"0", - "rows"=>"1000", - "facet"=>"true", - "facet.offset"=>"0", - "facet.mincount"=>"1", - "f.subject_tax_0_filter.facet.sort"=>"count", - "f.subject_tax_0_filter.facet.limit"=>"11", - "f.author_filter.facet.limit"=>"11", - "f.dateIssued.year.facet.sort"=>"index", - "f.dateIssued.year.facet.limit"=>"10", - "f.author_filter.facet.sort"=>"count", - "facet.field"=>["author_filter","subject_tax_0_filter","dateIssued.year"], - "fq"=>["NOT(withdrawn:true)","NOT(discoverable:false)","read:(g0 OR g0)"] - } - end - - def sort_author(items) - items = items.sort_by{|item| [ item['author'].to_s.downcase ]} - items.each{ |item| item['author'] = sort_author(item['author']) if (item['author'].nil? ? [] : item['author']).size > 0 } - items - end - - - def sort_title(items) - items = items.sort_by{|item| [ item['title'].to_s.downcase ]} - items.each{ |item| item['title'] = sort_title(item['title']) if (item['title'].nil? ? [] : item['title']).size > 0 } - items - end -end \ No newline at end of file diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..a59fd8163bae09371322c428f04d4e2cfc6019ab --- /dev/null +++ b/app/controllers/search_controller.rb @@ -0,0 +1,74 @@ +class SearchController < ApplicationController + + include SearchEngine::OrientdbLucene + + def index + + objectsFound = search(params[:qry]) + @numFound = objectsFound.length + + per_page=10 + @npagnac=10 + if(params[:paginacao]) + per_page=params[:paginacao].to_i + @npagnac=per_page + end + + if(params[:page]) + #params[:page] = 1 + else + params[:page] = 1 + end + + current_page = params[:page].to_i + + if params[:sort] + @ordena = params[:sort] + end + if params[:sort] == "author" + objectsFound = sort_author(objectsFound); + elsif params[:sort] == "publicationasc" + objectsFound = objectsFound.sort_by{|item| [ item.id ]} + elsif params[:sort] == "publicationdesc" + objectsFound = objectsFound.sort_by{|item| - item.id } + elsif params[:sort] == "relevance" + items= objectsFound.collect{ |x| Ranking::Item.new(x.name,x.views,0,x.likes,x) } #puts each object in an item + + rater = Ranking::Rater.new( + Ranking::Strategies::BasicRater.new( + #define sorting weights + positionWeight = 1000, + useWeight = 1, + likeWeight = 100 + ) + ) + + a = rater.sortByRate(items) # Returns sorted array of items + objectsFound = a.collect{ |x| x.info } #get only the info fields(objects) + + elsif params[:sort] == "title" + objectsFound = sort_title(objectsFound); + end + # current_page, per_page, total_results + @results = WillPaginate::Collection.create(current_page, per_page, objectsFound.length) do |pager| + @start = (current_page-1)*per_page + pager.replace (objectsFound.to_a[@start,per_page]) + end + + end + + private + + def sort_author(items) + # items = items.sort_by{|item| [ item['author'].to_s.downcase ]} + # items.each{ |item| item['author'] = sort_author(item['author']) if (item['author'].nil? ? [] : item['author']).size > 0 } + items + end + + + def sort_title(items) + # items = items.sort_by{|item| [ item['title'].to_s.downcase ]} + # items.each{ |item| item['title'] = sort_title(item['title']) if (item['title'].nil? ? [] : item['title']).size > 0 } + items + end +end diff --git a/app/repositories/orient_db/learning_object_repository.rb b/app/repositories/orient_db/learning_object_repository.rb index e7a317fba3ca9c8cc105ca4717b5e19418c37dee..c6ba4f58c2e49b3bb0b083072a119cdfc88e661c 100644 --- a/app/repositories/orient_db/learning_object_repository.rb +++ b/app/repositories/orient_db/learning_object_repository.rb @@ -51,6 +51,20 @@ module OrientDb connection.command sprintf("DELETE VERTEX LearningObject where @rid = '%s'", learning_object.id) end + def search(qry) + # TO CREATE "index:learningobject_search" ON OrientDB, USE THE COMMAND: + # CREATE INDEX learningobject_search + # ON LearningObject (name, description) + # FULLTEXT ENGINE LUCENE + # METADATA {"analyzer":"org.apache.lucene.analysis.br.BrazilianAnalyzer"} + learning_objects_hash = connection.query " + SELECT EXPAND(rid) + FROM index:learningobject_search + WHERE key LUCENE '#{qry}' + ", limit: -1 + learning_objects = build_learning_objects(learning_objects_hash) || [] + end + #def author_of(rid) # connection.query "SELECT expand(in) FROM (SELECT expand(out_author_of) FROM User WHERE @rid=#{rid})" #end @@ -86,8 +100,18 @@ module OrientDb :metadata => args["metadata"], :last_modified => args["last_modified"]) - lo.likes = count_likes lo - lo.views = count_views lo + unless args["in_Likes"].nil? + lo.likes = args["in_Likes"].count + else + lo.likes = 0 + end + + unless args["in_Views"].nil? + lo.views = args["in_Views"].count + else + lo.views = 0 + end + lo end diff --git a/app/views/search/solr/search.html.erb b/app/views/search/index.html.erb similarity index 98% rename from app/views/search/solr/search.html.erb rename to app/views/search/index.html.erb index 0523ac66fa4b08698923d7959ffbad683f18c911..30d1d8d357007af118e561ce7455aefa588c9db4 100644 --- a/app/views/search/solr/search.html.erb +++ b/app/views/search/index.html.erb @@ -315,7 +315,7 @@ <!-- search results --> <div class="col-sm-8"> - <% unless @objectsFound.empty? %> + <% unless @results.empty? %> <h4>Objetos encontrados: <%= @numFound %></h4> <nav class="navbar navbar-default"> <div class="container-fluid" > @@ -344,11 +344,11 @@ </nav> <br /> <div class="container-fluid"> - <%= will_paginate @objectsFound %><!--%=@objectsFound%--> - <% @objectsFound.each do |o| %> - <%= render partial: "shared/application/object_tag1", locals: {obj: o} %> + <%= will_paginate @results %> + <% @results.each do |o| %> + <%= render partial: "shared/application/object_tag", locals: {obj: o} %> <% end %> - <%= will_paginate @objectsFound %> + <%= will_paginate @results %> </div> <% end %> </div><!-- col-sm-8 --> diff --git a/app/views/shared/application/_object_tag.html.erb b/app/views/shared/application/_object_tag.html.erb index 21783cf9ad5c5ba839cef297ea25995b660233ee..c117569976df7ec75f87225f39877e2f6252d65c 100644 --- a/app/views/shared/application/_object_tag.html.erb +++ b/app/views/shared/application/_object_tag.html.erb @@ -1,23 +1,44 @@ -<div class="col-md-12"> - <div class="itemhorz"> - <div class="col-md-5"> - <%= link_to image_tag("logo.png", :class => "img-responsive"), :class => "thumbnail" %> +<!--<%=debug obj%>--> + +<div class="row"> + <div class="col-sm-3"> + <a href="app/assets/images/logo.png" class="thumbnail"> + <p><%= obj.name %></p> + <!-- <%= link_to image_tag("logo.png", :class => "img-responsive"), :class => "thumbnail" %><img src="app/assets/images/logo.png" alt="" width="284" height="213">--> + <%= image_tag("logo.png") %> + </a> + </div> + <div class="col-sm-9"> + <div class="col-sm-12"> + <%= obj.description %> + </div> + <div class="col-sm-12"> + <div align="right"><%= link_to "Ver mais...", learning_object_path(obj.id_dspace), :class => "btn btn-primary" %></div> + <br/> + <table class="table "> + <tr> + <td class="stars">Avaliações:</td><td>Postado por:</td><td class="visualis">Visualizações:</td> + </tr> + <tr> + <td> + <span class="starRating"> + <input id="rating5" type="radio" name="rating" value="5" disabled> + <label for="rating5">5</label> + <input id="rating4" type="radio" name="rating" value="4" checked disabled> + <label for="rating4">4</label> + <input id="rating3" type="radio" name="rating" value="3" disabled> + <label for="rating3">3</label> + <input id="rating2" type="radio" name="rating" value="2" disabled> + <label for="rating2">2</label> + <input id="rating1" type="radio" name="rating" value="1" disabled> + <label for="rating1">1</label> + </span> + () + </td> + <td><%= link_to "<<Autor>>" %> Há ... anos</td> + <td><%= obj.views %></td> + </tr> + </table> + </div> + </div> </div> - <div class="col-md-7"> - <h1><%= link_to obj["title"][0], learning_object_path(obj["search.resourceid"]) %></h1> - <%= image_tag object_thumbnail(obj["handle"]), :class => "img-circle", :width => "32" %> por <%= link_to obj["author"][0] %> -<% -=begin -%> - <p class="pull-right"><span class="glyphicon glyphicon-search" aria-hidden="true"></span>4,5 - <span class="glyphicon glyphicon-star" aria-hidden="true"></span>200 - </p> -<% -=end -%> - <% unless obj["dc.description"].nil? %> - <p><%= obj["dc.description"][0] %></p> - <% end %> - </div> - </div> -</div> diff --git a/app/views/shared/application/_object_tag1.html.erb b/app/views/shared/application/_object_tag1.html.erb deleted file mode 100644 index 27de179726caf4aa86256bd361cadf7f76e49fc9..0000000000000000000000000000000000000000 --- a/app/views/shared/application/_object_tag1.html.erb +++ /dev/null @@ -1,49 +0,0 @@ -<!--<%=debug obj%>--> - -<div class="row"> - <div class="col-sm-3"> - <a href="app/assets/images/logo.png" class="thumbnail"> - <p><% unless obj["title"].nil? %> - <p><%= obj["title"][0] %></p> - <% end %> - </p> - <!-- <%= link_to image_tag("logo.png", :class => "img-responsive"), :class => "thumbnail" %><img src="app/assets/images/logo.png" alt="" width="284" height="213">--> - <%= image_tag("logo.png") %> - </a> - </div> - <div class="col-sm-9"> - <div class="col-sm-12"> - <% unless obj["dc.description"].nil? %> - <%= obj["dc.description"][0] %> - <% end %> - </div> - <div class="col-sm-12"> - <div align="right"><%= link_to "Ver mais...", learning_object_path(obj["search.resourceid"]), :class => "btn btn-primary" %></div> - <br/> - <table class="table "> - <tr> - <td class="stars">Avaliações:</td><td>Postado por:</td><td class="visualis">Visualizações:</td> - </tr> - <tr> - <td> - <span class="starRating"> - <input id="rating5" type="radio" name="rating" value="5" disabled> - <label for="rating5">5</label> - <input id="rating4" type="radio" name="rating" value="4" checked disabled> - <label for="rating4">4</label> - <input id="rating3" type="radio" name="rating" value="3" disabled> - <label for="rating3">3</label> - <input id="rating2" type="radio" name="rating" value="2" disabled> - <label for="rating2">2</label> - <input id="rating1" type="radio" name="rating" value="1" disabled> - <label for="rating1">1</label> - </span> - () - </td> - <td><%= link_to obj["author"][0] %> Há ... anos</td> - <td>333</td> - </tr> - </table> - </div> - </div> - </div> diff --git a/config/routes.rb b/config/routes.rb index efd7312c0098bbf9c7a8565e69bd4d1134d2ac41..a2c3ef04c74df29172b3fd5b7b4f8b879227b3c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,5 +31,5 @@ Rails.application.routes.draw do get '/faq' => 'welcome#faq' get '/contact' => 'welcome#contact', as: 'contact' get '/complaint' => 'welcome#complaint', as: 'complaint' - get '/search' => 'search/solr#search', as: 'search' + get '/search' => 'search#index', as: 'search' end diff --git a/lib/search_engine/dspace_solr.rb b/lib/search_engine/dspace_solr.rb new file mode 100644 index 0000000000000000000000000000000000000000..4ddc03ced7b015e260e28a8c581415017df37bab --- /dev/null +++ b/lib/search_engine/dspace_solr.rb @@ -0,0 +1,64 @@ +module SearchEngine + module DspaceSolr + + def search(qry) + + queryParams = merge_search_parameters( + {:q => '*:*', :fq => qry}, + solr_search_default_parameters + ) + results = solr_client.get 'select', :params => queryParams + objectsFound = select_items_from_results(results) + + return objectsFound + end + + private + + #Resource types: + # 2 = Item + # 3 = Collection + # 4 = Community + def select_items_from_results(results) + resultList = Set.new + results["response"]["docs"].select { |r| + r["search.resourcetype"].to_i.equal? 2 + }.each do |i| + resultList.add i + end + return resultList + end + + def solr_client + return RSolr.connect :url => 'http://portalmecdev.c3sl.ufpr.br:8080/solr/search/' + end + + def merge_search_parameters(p1, p2) + return p1.merge(p2) { |key,oldval,newval| key = [newval,oldval].flatten } + end + + def solr_search_default_parameters + return { + "fl"=>[" + search.resourcetype, search.resourceid, + handle, author, title, dc.description + "], + "type"=>"0", + "start"=>"0", + "rows"=>"1000", + "facet"=>"true", + "facet.offset"=>"0", + "facet.mincount"=>"1", + "f.subject_tax_0_filter.facet.sort"=>"count", + "f.subject_tax_0_filter.facet.limit"=>"11", + "f.author_filter.facet.limit"=>"11", + "f.dateIssued.year.facet.sort"=>"index", + "f.dateIssued.year.facet.limit"=>"10", + "f.author_filter.facet.sort"=>"count", + "facet.field"=>["author_filter","subject_tax_0_filter","dateIssued.year"], + "fq"=>["NOT(withdrawn:true)","NOT(discoverable:false)","read:(g0 OR g0)"] + } + end + + end +end diff --git a/lib/search_engine/orientdb_lucene.rb b/lib/search_engine/orientdb_lucene.rb new file mode 100644 index 0000000000000000000000000000000000000000..565a04963787ea9c8628e0aed87a490dd1256f42 --- /dev/null +++ b/lib/search_engine/orientdb_lucene.rb @@ -0,0 +1,15 @@ +module SearchEngine + module OrientdbLucene + + def search(qry) + learning_object_repository.search(qry) + end + + private + + def learning_object_repository + repository.for(:learning_object) + end + + end +end