trustie2 open source projects search

This commit is contained in:
zhanyun 2016-11-24 15:25:32 +08:00
parent 0d397c158a
commit 3fd7b0540b
2 changed files with 54 additions and 74 deletions

View File

@ -175,6 +175,7 @@ class OpenSourceProjectsController < ApplicationController
format.json { head :no_content }
end
else
#关键词预处理
@name = params[:name].downcase #+ '~2'
@app_dir = params[:app_dir]
@language = params[:language].to_s.downcase
@ -189,34 +190,35 @@ class OpenSourceProjectsController < ApplicationController
#search_words << ' ' + @language if @language
# 请求starlee服务
require 'net/http'
star_projects = []
parse_string = "http://172.16.128.35:5000/correlation_search?q=" + @name
# parse_string = "http://172.20.10.3:5000/correlation_search?q=" + @name
parse_string.gsub! " ","%20"
url = URI.parse(parse_string)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
star_items = res.body
require 'json'
star_items = JSON.parse(star_items)["items"]
star_project_ids = star_items[0,5]
star_projects = []
# 查询出id对应的项目
star_project_ids.each do |id|
id = id.to_i
project = OpenSourceProject.find_by_id(id)
star_projects << project
end
# require 'net/http'
#
# star_projects = []
# parse_string = "http://172.16.128.35:5000/correlation_search?q=" + @name
#
# # parse_string = "http://172.20.10.3:5000/correlation_search?q=" + @name
# parse_string.gsub! " ","%20"
# url = URI.parse(parse_string)
# req = Net::HTTP::Get.new(url.to_s)
# res = Net::HTTP.start(url.host, url.port) {|http|
# http.request(req)
# }
# star_items = res.body
# require 'json'
# star_items = JSON.parse(star_items)["items"]
# star_project_ids = star_items[0,5]
# star_projects = []
# # 查询出id对应的项目
# star_project_ids.each do |id|
# id = id.to_i
# project = OpenSourceProject.find_by_id(id)
# star_projects << project
# end
#合成solr请求
search = OpenSourceProject.search do
without(:filtration,0)
with(:created_time, Date.new(params[:created_time].to_i, 01, 01)..Date.new(params[:created_time].to_i+1, 01, 01)) if params[:created_time].present? && !('earlier'.eql? params[:created_time])
with(:created_time, Date.new(1970, 01, 01)..Date.new(2010, 01, 01)) if params[:created_time].present? && ('earlier'.eql? params[:created_time])
# with(:created_time, Date.new(params[:created_time].to_i, 01, 01)..Date.new(params[:created_time].to_i+1, 01, 01)) if params[:created_time].present? && !('earlier'.eql? params[:created_time])
# with(:created_time, Date.new(1970, 01, 01)..Date.new(2010, 01, 01)) if params[:created_time].present? && ('earlier'.eql? params[:created_time])
all do
fulltext language do
fields(:tags)
@ -227,13 +229,13 @@ class OpenSourceProjectsController < ApplicationController
#highlight(:name)
fields(:name)
fields(:tags_for_search)
#fields(:description)
#fields(:tags)
fields(:description)
fields(:tags)
fields(:language)
boost_fields :name => 4.0
boost_fields :tags_for_search => 3.0
#boost_fields :tags => 1.0
#boost_fields :description => 0.5
boost_fields :tags => 1.0
boost_fields :description => 0.5
boost_fields :language => 1.0
#boost(function { log(sum(:composite_score, 1))^2000 })
boost(function { log(:composite_score)^1000 })
@ -255,56 +257,32 @@ class OpenSourceProjectsController < ApplicationController
@open_source_projects = search.results
@projects_count = search.total #get total count of search
#合并 star_projects和solr_projects
open_source_projects_index = 0
while open_source_projects_index<@open_source_projects.length do
project = @open_source_projects[open_source_projects_index]
id = project.id
star_projects.each do |star_project|
if id.to_i == star_project.id
@open_source_projects.delete(project)
open_source_projects_index -= 1
break
end
end
open_source_projects_index += 1
end
if params[:page]==nil || params[:page]==1
star_index = star_projects.length-1
while star_index>=0 do
star_project = star_projects[star_index]
@open_source_projects.unshift(star_project)
star_index -= 1
end
end
# if @projects_count <= 10
# search_words1 = search_words.unpack("U*").select{ |p| (0x4e00..0x9fa5).member? p }.pack("U*")
# if search_words.include?('客户端')
# search_words = search_words.gsub('客户端',' APP')
# end
# search_1 = OpenSourceProject.search do
# fulltext search_words do
# fields(:name)
# fields(:tags_for_search)
# fields(:tags)
# fields(:language)
# boost_fields :name => 4.0
# boost_fields :tags_for_search => 3.0
# boost_fields :tags => 1.0
# boost_fields :language => 1.0
# boost(function { log(:composite_score)^1000 })
# while open_source_projects_index<@open_source_projects.length do
# project = @open_source_projects[open_source_projects_index]
# id = project.id
# star_projects.each do |star_project|
# if id.to_i == star_project.id
# @open_source_projects.delete(project)
# open_source_projects_index -= 1
# break
# end
# order_by(:score, :desc)
# paginate :page => params[:page], :per_page => 10
# end
# @hits = search.hits + search_1.hits
# @open_source_projects = search.results + search_1.results
# @projects_count = search.total + search_1.total
# open_source_projects_index += 1
# end
#
# if params[:page]==nil || params[:page]==1
# star_index = star_projects.length-1
# while star_index >= 0 do
# star_project = star_projects[star_index]
# @open_source_projects.unshift(star_project)
# star_index -= 1
# end
# end
@projects_pages = Paginator.new @projects_count, per_page_option, params['page'] #custom paginator,to show the 10 records the default paginator of solr gets
respond_to do |format|
format.html { render :layout => "base" }
end

View File

@ -34,11 +34,13 @@ class OpenSourceProject < ActiveRecord::Base
acts_as_taggable
#配置solr索引字段
searchable do
text :name, :stored => false
text :tags, :stored => false
text :tags_for_search, :stored => false
text :language, :stored => false
text :description, :stored => false
integer :filtration
integer :composite_score