Skip to content

Commit 7afc661

Browse files
Merge to master: Release 2.3.4 - Multilingual (#42)
* Merge pull request #34 from ontoportal-lirmm/feature/paginate-and-filter-ontologies-endpoint Feature: Implement pagination and filters to submissions endpoint * Merge pull request #36 from ontoportal-lirmm/feature/paginate-and-filter-ontologies-endpoint Feature: Add oder by and filters for ontologies endpoint * Merge pull request #32 from ontoportal-lirmm/feature/support-multilingual-read-one-language-from-request-parameter Feature: Support multilingual - Add request_lang middleware * Feature: Add support of multilingual search (#40) * update get_term_search_query to support multilanguages search * rename var * fix search lang suffix to use underscore not @ * add multilangual search test --------- Co-authored-by: Syphax Bouazzouni <gs_bouazzouni@esi.dz> --------- Co-authored-by: HADDAD Zineddine <hz_haddad@esi.dz>
1 parent c791b3f commit 7afc661

9 files changed

Lines changed: 129 additions & 24 deletions

File tree

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ GEM
247247
rack (>= 0.4)
248248
rack-attack (6.6.1)
249249
rack (>= 1.0, < 3)
250-
rack-cache (1.13.0)
250+
rack-cache (1.14.0)
251251
rack (>= 0.4)
252252
rack-cors (1.0.6)
253253
rack (>= 1.6.0)
@@ -270,8 +270,8 @@ GEM
270270
redis-rack-cache (2.2.1)
271271
rack-cache (>= 1.10, < 2)
272272
redis-store (>= 1.6, < 2)
273-
redis-store (1.9.1)
274-
redis (>= 4, < 5)
273+
redis-store (1.9.2)
274+
redis (>= 4, < 6)
275275
representable (3.2.0)
276276
declarative (< 0.1.0)
277277
trailblazer-option (>= 0.1.1, < 0.2.0)

app.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
require_relative 'lib/rack/cube_reporter'
3030
require_relative 'lib/rack/param_translator'
3131
require_relative 'lib/rack/slice_detection'
32+
require_relative 'lib/rack/request_lang'
3233

3334
# Logging setup
3435
require_relative "config/logging"
3536

3637
# Inflector setup
3738
require_relative "config/inflections"
3839

40+
require 'request_store'
41+
3942
# Protection settings
4043
set :protection, :except => :path_traversal
4144

@@ -143,6 +146,9 @@
143146
use Rack::PostBodyToParams
144147
use Rack::ParamTranslator
145148

149+
use RequestStore::Middleware
150+
use Rack::RequestLang
151+
146152
use LinkedData::Security::Authorization
147153
use LinkedData::Security::AccessDenied
148154

helpers/search_helper.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def get_term_search_query(text, params={})
8282
end
8383
end
8484

85+
lang = params["lang"] || params["language"]
86+
lang_suffix = lang && !lang.eql?("all") ? "_#{lang}" : ""
87+
8588
query = ""
8689
params["defType"] = "edismax"
8790
params["stopwords"] = "true"
@@ -98,25 +101,25 @@ def get_term_search_query(text, params={})
98101

99102
if params[EXACT_MATCH_PARAM] == "true"
100103
query = "\"#{solr_escape(text)}\""
101-
params["qf"] = "resource_id^20 prefLabelExact^10 synonymExact #{QUERYLESS_FIELDS_STR}"
102-
params["hl.fl"] = "resource_id prefLabelExact synonymExact #{QUERYLESS_FIELDS_STR}"
104+
params["qf"] = "resource_id^20 prefLabelExact#{lang_suffix }^10 synonymExact#{lang_suffix } #{QUERYLESS_FIELDS_STR}"
105+
params["hl.fl"] = "resource_id prefLabelExact#{lang_suffix } synonymExact#{lang_suffix } #{QUERYLESS_FIELDS_STR}"
103106
elsif params[SUGGEST_PARAM] == "true" || text[-1] == '*'
104107
text.gsub!(/\*+$/, '')
105108
query = "\"#{solr_escape(text)}\""
106109
params["qt"] = "/suggest_ncbo"
107-
params["qf"] = "prefLabelExact^100 prefLabelSuggestEdge^50 synonymSuggestEdge^10 prefLabelSuggestNgram synonymSuggestNgram resource_id #{QUERYLESS_FIELDS_STR}"
110+
params["qf"] = "prefLabelExact#{lang_suffix }^100 prefLabelSuggestEdge^50 synonymSuggestEdge^10 prefLabelSuggestNgram synonymSuggestNgram resource_id #{QUERYLESS_FIELDS_STR}"
108111
params["pf"] = "prefLabelSuggest^50"
109-
params["hl.fl"] = "prefLabelExact prefLabelSuggestEdge synonymSuggestEdge prefLabelSuggestNgram synonymSuggestNgram resource_id #{QUERYLESS_FIELDS_STR}"
112+
params["hl.fl"] = "prefLabelExact#{lang_suffix } prefLabelSuggestEdge synonymSuggestEdge prefLabelSuggestNgram synonymSuggestNgram resource_id #{QUERYLESS_FIELDS_STR}"
110113
else
111114
if text.strip.empty?
112115
query = '*'
113116
else
114117
query = solr_escape(text)
115118
end
116119

117-
params["qf"] = "resource_id^100 prefLabelExact^90 prefLabel^70 synonymExact^50 synonym^10 #{QUERYLESS_FIELDS_STR}"
120+
params["qf"] = "resource_id^100 prefLabelExact#{lang_suffix }^90 prefLabel#{lang_suffix }^70 synonymExact#{lang_suffix }^50 synonym#{lang_suffix }^10 #{QUERYLESS_FIELDS_STR}"
118121
params["qf"] << " property" if params[INCLUDE_PROPERTIES_PARAM] == "true"
119-
params["hl.fl"] = "resource_id prefLabelExact prefLabel synonymExact synonym #{QUERYLESS_FIELDS_STR}"
122+
params["hl.fl"] = "resource_id prefLabelExact#{lang_suffix } prefLabel#{lang_suffix } synonymExact#{lang_suffix } synonym#{lang_suffix } #{QUERYLESS_FIELDS_STR}"
120123
params["hl.fl"] = "#{params["hl.fl"]} property" if params[INCLUDE_PROPERTIES_PARAM] == "true"
121124
end
122125

lib/rack/request_lang.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Rack
2+
class RequestLang
3+
4+
def initialize(app = nil, options = {})
5+
@app = app
6+
end
7+
8+
def call(env)
9+
r = Rack::Request.new(env)
10+
lang = r.params["lang"] || r.params["language"]
11+
lang = lang.upcase.to_sym if lang
12+
RequestStore.store[:requested_lang] = lang
13+
@app.call(env)
14+
end
15+
end
16+
end

test/controllers/test_ontology_submissions_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ def test_download_acl_only
201201
end
202202
end
203203

204+
def test_submissions_pagination
205+
num_onts_created, created_ont_acronyms = create_ontologies_and_submissions(ont_count: 2, submission_count: 2)
206+
207+
get "/submissions"
208+
assert last_response.ok?
209+
submissions = MultiJson.load(last_response.body)
210+
211+
assert_equal 2, submissions.length
212+
213+
214+
get "/submissions?page=1&pagesize=1"
215+
assert last_response.ok?
216+
submissions = MultiJson.load(last_response.body)
217+
assert_equal 1, submissions["collection"].length
218+
end
204219

205220

206221
def test_submissions_default_includes

test/controllers/test_search_controller.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,48 @@ def test_search_provisional_class
213213
assert_equal @@test_pc_child.label, provisional[0]["prefLabel"].first
214214
end
215215

216+
def test_multilingual_search
217+
get "/search?q=Activity&ontologies=BROSEARCHTEST-0"
218+
res = MultiJson.load(last_response.body)
219+
refute_equal 0, res["totalCount"]
220+
221+
doc = res["collection"].select{|doc| doc["@id"].to_s.eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
222+
refute_nil doc
223+
224+
#res = LinkedData::Models::Class.search("prefLabel_none:Activity", {:fq => "submissionAcronym:BROSEARCHTEST-0", :start => 0, :rows => 80}, :main)
225+
#refute_equal 0, res["response"]["numFound"]
226+
#refute_nil res["response"]["docs"].select{|doc| doc["resource_id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
227+
228+
get "/search?q=Activit%C3%A9&ontologies=BROSEARCHTEST-0&lang=fr"
229+
res = MultiJson.load(last_response.body)
230+
refute_equal 0, res["totalCount"]
231+
refute_nil res["collection"].select{|doc| doc["@id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
232+
233+
234+
235+
get "/search?q=ActivityEnglish&ontologies=BROSEARCHTEST-0&lang=en"
236+
res = MultiJson.load(last_response.body)
237+
refute_equal 0, res["totalCount"]
238+
refute_nil res["collection"].select{|doc| doc["@id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
239+
240+
241+
get "/search?q=ActivityEnglish&ontologies=BROSEARCHTEST-0&lang=fr&require_exact_match=true"
242+
res = MultiJson.load(last_response.body)
243+
assert_nil res["collection"].select{|doc| doc["@id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
244+
245+
get "/search?q=ActivityEnglish&ontologies=BROSEARCHTEST-0&lang=en&require_exact_match=true"
246+
res = MultiJson.load(last_response.body)
247+
refute_nil res["collection"].select{|doc| doc["@id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
248+
249+
get "/search?q=Activity&ontologies=BROSEARCHTEST-0&lang=en&require_exact_match=true"
250+
res = MultiJson.load(last_response.body)
251+
assert_nil res["collection"].select{|doc| doc["@id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
252+
253+
get "/search?q=Activit%C3%A9&ontologies=BROSEARCHTEST-0&lang=fr&require_exact_match=true"
254+
res = MultiJson.load(last_response.body)
255+
refute_nil res["collection"].select{|doc| doc["@id"].eql?('http://bioontology.org/ontologies/Activity.owl#Activity')}.first
256+
257+
258+
end
259+
216260
end

test/data/ontology_files/BRO_v3.2.owl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@
631631

632632
<owl:Class rdf:about="&activity;Activity">
633633
<core:prefLabel rdf:datatype="&xsd;string">Activity</core:prefLabel>
634+
<core:prefLabel rdf:datatype="&xsd;string">Activity</core:prefLabel>
635+
<core:prefLabel xml:lang="en">ActivityEnglish</core:prefLabel>
636+
<core:prefLabel xml:lang="fr">Activité</core:prefLabel>
634637
<desc:definition rdf:datatype="&xsd;string">Activity of interest that may be related to a BRO:Resource.</desc:definition>
635638
<core:altLabel>activities</core:altLabel>
636639
</owl:Class>

test/solr/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.8'
2+
3+
services:
4+
op_solr:
5+
image: solr:8.8
6+
volumes:
7+
- ./solr_configsets:/configsets:ro
8+
ports:
9+
- "8983:8983"
10+
command: >
11+
bash -c "precreate-core term_search_core1 /configsets/term_search
12+
&& precreate-core prop_search_core1 /configsets/property_search
13+
&& solr-foreground"

test/solr/generate_ncbo_configsets.sh

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
# generates solr configsets by merging _default configset with config files in config/solr
33
# _default is copied from sorl distribuion solr-8.10.1/server/solr/configsets/_default/
44

5-
pushd solr/configsets
6-
ld_config='../../../../ontologies_linked_data/config/solr/'
7-
#ld_config='../../../../config/solr/'
8-
ls -l $ld_config
9-
pwd
10-
[ -d property_search ] && rm -Rf property_search
11-
[ -d term_search ] && rm -Rf property_search
12-
[ -d $ld_config/property_search ] || echo "cant find ontologies_linked_data project"
13-
mkdir -p property_search/conf
14-
mkdir -p term_search/conf
15-
cp -a _default/conf/* property_search/conf/
16-
cp -a _default/conf/* term_search/conf/
17-
cp -a $ld_config/property_search/* property_search/conf
18-
cp -a $ld_config/term_search/* term_search/conf
19-
popd
5+
#cd solr/configsets
6+
ld_config='config/solr'
7+
configsets='test/solr/configsets'
8+
[ -d ${configsets}/property_search ] && rm -Rf ${configsets}/property_search
9+
[ -d ${configsets}/term_search ] && rm -Rf ${configsets}/term_search
10+
if [[ ! -d ${ld_config}/property_search ]]; then
11+
echo 'cant find ld solr config sets'
12+
exit 1
13+
fi
14+
if [[ ! -d ${configsets}/_default/conf ]]; then
15+
echo 'cant find default solr configset'
16+
exit 1
17+
fi
18+
mkdir -p ${configsets}/property_search/conf
19+
mkdir -p ${configsets}/term_search/conf
20+
cp -a ${configsets}/_default/conf/* ${configsets}/property_search/conf/
21+
cp -a ${configsets}/_default/conf/* ${configsets}/term_search/conf/
22+
cp -a $ld_config/property_search/* ${configsets}/property_search/conf
23+
cp -a $ld_config/term_search/* ${configsets}/term_search/conf
24+

0 commit comments

Comments
 (0)