From 3ff3502bbc11621813fdc81f903524ec9d4d2f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxxq250=E2=80=9D?= <“xxq250@qq.com”> Date: Tue, 1 Nov 2022 17:25:28 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=E9=A1=B9=E7=9B=AE=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=85=88es=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/queries/projects/list_query.rb | 9 +++++++-- .../projects/elasticsearch_service.rb | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 app/services/projects/elasticsearch_service.rb diff --git a/app/queries/projects/list_query.rb b/app/queries/projects/list_query.rb index 1994c9c71..140d973ea 100644 --- a/app/queries/projects/list_query.rb +++ b/app/queries/projects/list_query.rb @@ -25,7 +25,7 @@ class Projects::ListQuery < ApplicationQuery def filter_projects(collection) # collection = by_pinned(collection) - collection = by_search(collection) + collection = by_search(collection) if params[:search].present? collection = by_project_type(collection) collection = by_project_category(collection) collection = by_project_language(collection) @@ -33,7 +33,12 @@ class Projects::ListQuery < ApplicationQuery end def by_search(items) - items.visible.by_name_or_identifier(params[:search]) + ids = Projects::ElasticsearchService.call(params[:search]) + if ids.present? + items.visible.where(id: ids) + else + items.visible.by_name_or_identifier(params[:search]) + end end def by_project_type(items) diff --git a/app/services/projects/elasticsearch_service.rb b/app/services/projects/elasticsearch_service.rb new file mode 100644 index 000000000..466e17ffd --- /dev/null +++ b/app/services/projects/elasticsearch_service.rb @@ -0,0 +1,20 @@ +class Projects::ElasticsearchService < ApplicationService + attr_reader :keyword + + def initialize(keyword) + @keyword = keyword + end + + def call + domain = EduSetting.get("search_api_url") || "https://statistics.gitlink.org.cn" + api_url = "#{domain}/search?page=1&size=1000&term=#{@keyword}&type=1" + response = Faraday.get(api_url) + result = JSON.parse(response&.body) + project_ids = result[:data][:rows].map{|d|d[:instanceId]} + project_ids + rescue => e + puts "ElasticsearchService error: #{e.message}" + raise Error, e.message + end + +end