Skip to content

Commit 8fb0487

Browse files
committed
Added search term match for the FTS page.
1 parent 0599703 commit 8fb0487

4 files changed

Lines changed: 76 additions & 19 deletions

File tree

data/solr-config.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"APILink": "https://fcdo.iati.cloud/",
3-
"DefaultFieldsToReturn": "iati_identifier, reporting_org_ref, reporting_org_narrative, title_narrative_first, title_narrative, description_narrative, activity_date_iso_date, activity_date_start_actual, activity_date_end_planned, activity_status_code, humanitarian, tag_code, tag, child_aggregation_*, activity_plus_child_aggregation*, activity_aggregation_*, default_currency, activity_date_start_common_f, activity_date_end_common_f, activity_date_start_common, activity_date_end_common",
3+
"DefaultFieldsToReturn": "id, iati_identifier, reporting_org_ref, reporting_org_narrative, title_narrative_first, title_narrative, description_narrative, activity_date_iso_date, activity_date_start_actual, activity_date_end_planned, activity_status_code, humanitarian, tag_code, tag, child_aggregation_*, activity_plus_child_aggregation*, activity_aggregation_*, default_currency, activity_date_start_common_f, activity_date_end_common_f, activity_date_start_common, activity_date_end_common",
44
"DefaultFieldsToSearch":
55
[
66
"title_narrative",
@@ -111,7 +111,7 @@
111111
{
112112
"F":
113113
{
114-
"url": "/search/activity/?q=hierarchy:1 AND participating_org_ref:GB-GOV-* AND ",
114+
"url": "/search/activity/?hl=on&hl.requireFieldMatch=true&hl.fl=title_narrative,title_narrative_first,description_narrative,iati_identifier,transaction_description_narrative,reporting_org_ref,recipient_country_name,document_link_title_narrative_text,transaction,reporting_org_narrative,participating_org_narrative&q=hierarchy:1 AND participating_org_ref:GB-GOV-* AND ",
115115
"fieldDependency": "",
116116
"comments": "This search category is related to the free text search of DevTracker"
117117
},
@@ -163,5 +163,19 @@
163163
"projects":
164164
[
165165
]
166+
},
167+
"HumanReadableTerms":
168+
{
169+
"title_narrative": "Title",
170+
"description_narrative": "Description",
171+
"title_narrative_first": "Title",
172+
"iati_identifier": "Programme Id",
173+
"transaction_description_narrative": "Transaction Description",
174+
"reporting_org_ref": "Reporting Organisation Id",
175+
"recipient_country_name": "Recipient Country Name",
176+
"document_link_title_narrative_text": "Document Title",
177+
"transaction": "Transaction",
178+
"reporting_org_narrative": "Reporting Organisation Title",
179+
"participating_org_narrative": "Participating Organisation Title"
166180
}
167181
}

devtracker.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@
5959
# Developer Machine: set global settings
6060
#set :oipa_api_url, 'https://fcdo-direct-indexing.iati.cloud/search/'#'https://fcdo.iati.cloud/search/'#'https://fcdo-direct-indexing.iati.cloud/search/'#'https://devtracker.fcdo.gov.uk/api/'
6161
# set :oipa_api_url, 'https://devtracker-entry.oipa.nl/api/'
62-
# set :oipa_api_url, 'https://fcdo.iati.cloud/search/'
62+
set :oipa_api_url, 'https://fcdo.iati.cloud/search/'
6363
# set :oipa_api_url, 'https://fcdo-direct-indexing.iati.cloud/search/'
6464
#set :bind, '0.0.0.0' # Allows for vagrant pass-through whilst debugging
6565

6666
# Server Machine: set global settings to use varnish cache
67-
set :oipa_api_url, 'http://127.0.0.1:6081/search/'
67+
#set :oipa_api_url, 'http://127.0.0.1:6081/search/'
6868
set :prod_api_url, 'https://fcdo.iati.cloud'
6969
set :dev_api_url, 'https://fcdo-staging.iati.cloud'
7070

@@ -879,7 +879,8 @@ def getCacheData(fileName)
879879
if (!params['query'])
880880
query= ''
881881
filters = []
882-
response =
882+
response = {}
883+
response['response'] =
883884
{
884885
'numFound' => -1,
885886
'docs' => []
@@ -893,8 +894,10 @@ def getCacheData(fileName)
893894
activityStatuses = 'AND activity_status_code:(2)'
894895
filters = prepareFilters(query.to_s, 'F')
895896
response = solrResponse(query, activityStatuses, 'F', 0, '', '')
896-
if(response['numFound'].to_i > 0)
897-
response = addTotalBudgetWithCurrency(response)
897+
puts response
898+
if(response['response']['numFound'].to_i > 0)
899+
response['response'] = addTotalBudgetWithCurrency(response['response'])
900+
response = addHighlightingToFTSTerms(response)
898901
end
899902
settings.devtracker_page_title = 'Search Results For : ' + query
900903
didYouMeanQuery = sanitize_input(params['query'],"a")
@@ -907,7 +910,7 @@ def getCacheData(fileName)
907910
oipa_api_url: settings.oipa_api_url,
908911
query: query,
909912
filters: filters,
910-
response: response,
913+
response: response['response'],
911914
solrConfig: Oj.load(File.read('data/solr-config.json')),
912915
activityStatus: Oj.load(File.read('data/activity_status.json')),
913916
searchType: 'F',
@@ -934,8 +937,9 @@ def getCacheData(fileName)
934937
end
935938
filters = prepareFilters(query.to_s, 'F')
936939
response = solrResponse(query, activityStatuses, 'F', 0, '', '')
937-
if(response['numFound'].to_i > 0)
938-
response = addTotalBudgetWithCurrency(response)
940+
if(response['response']['numFound'].to_i > 0)
941+
response['response'] = addTotalBudgetWithCurrency(response['response'])
942+
response = addHighlightingToFTSTerms(response)
939943
end
940944
settings.devtracker_page_title = 'Search Results For : ' + query
941945
didYouMeanQuery = sanitize_input(params['query'],"a")
@@ -948,7 +952,7 @@ def getCacheData(fileName)
948952
oipa_api_url: settings.oipa_api_url,
949953
query: query,
950954
filters: filters,
951-
response: response,
955+
response: response['response'],
952956
solrConfig: Oj.load(File.read('data/solr-config.json')),
953957
activityStatus: Oj.load(File.read('data/activity_status.json')),
954958
searchType: 'F',
@@ -970,8 +974,16 @@ def getCacheData(fileName)
970974
searchType = sanitize_input(params['data']['queryType'],"newId")
971975
startPage = sanitize_input(params['data']['page'],"newId")
972976
response = solrResponse(query, filters, searchType, startPage, sanitize_input(params['data']['dateRange'],"newId"), sanitize_input(params['data']['sortType'],"newId"))
973-
if(response['numFound'].to_i > 0)
974-
response = addTotalBudgetWithCurrency(response)
977+
if searchType == 'F'
978+
if(response['response']['numFound'].to_i > 0)
979+
response['response'] = addTotalBudgetWithCurrency(response['response'])
980+
response = addHighlightingToFTSTerms(response)
981+
response = response['response']
982+
end
983+
else
984+
if(response['numFound'].to_i > 0)
985+
response = addTotalBudgetWithCurrency(response)
986+
end
975987
end
976988
json :output => response
977989
end

helpers/solr_helper.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ def filterDriver(filter, apiUrl, mappingFileUrl)
110110
reportingOrgs = response['facet_counts']['facet_fields'][filter]
111111
reportingOrgs.each_with_index do |value, index|
112112
if index.even?
113-
puts value
114113
if(mappedOGDs.has_key?(value))
115114
begin
116115
finalData.push(populateKeyVal(mappedOGDs[value]['name'], value))
@@ -230,8 +229,9 @@ def solrResponse(query, filters, queryType, startPage, dateRange, sortType)
230229
mainQueryString = mainQueryString + '&sort=' + sortType
231230
end
232231
# Get response based on the API responses
232+
puts apiLink + queryCategory['url'] + mainQueryString +'&rows='+solrConfig['PageSize'].to_s+'&fl='+solrConfig['DefaultFieldsToReturn']+'&start='+startPage.to_s
233233
response = Oj.load(RestClient.get api_simple_log(apiLink + queryCategory['url'] + mainQueryString +'&rows='+solrConfig['PageSize'].to_s+'&fl='+solrConfig['DefaultFieldsToReturn']+'&start='+startPage.to_s))
234-
response['response']
234+
response
235235
elsif(queryType == 'R')
236236
queryCategory = solrConfig['QueryCategories'][queryType]
237237
preparedFilters = filters
@@ -248,7 +248,6 @@ def solrResponse(query, filters, queryType, startPage, dateRange, sortType)
248248
if(sortType != '')
249249
mainQueryString = mainQueryString + '&sort=' + sortType
250250
end
251-
puts apiLink + queryCategory['url'] + mainQueryString +'&rows='+solrConfig['PageSize'].to_s+'&fl='+solrConfig['DefaultFieldsToReturn']+'&start='+startPage.to_s
252251
response = Oj.load(RestClient.get api_simple_log(apiLink + queryCategory['url'] + mainQueryString +'&rows='+solrConfig['PageSize'].to_s+'&fl='+solrConfig['DefaultFieldsToReturn']+'&start='+startPage.to_s))
253252
response['response']
254253
elsif(queryType == 'S')
@@ -363,4 +362,23 @@ def addTotalBudgetWithCurrency(response)
363362
end
364363
response
365364
end
365+
366+
def addHighlightingToFTSTerms(response)
367+
solrConfig = Oj.load(File.read('data/solr-config.json'))
368+
hrt = solrConfig['HumanReadableTerms']
369+
highlightedTexts = response['highlighting']
370+
response['response']['docs'].each do |r|
371+
puts r['id']
372+
if highlightedTexts.has_key?(r['id'].to_s)
373+
if !highlightedTexts[r['id']].empty?
374+
firstElement = highlightedTexts[r['id'].to_s].first
375+
key = firstElement[0]
376+
val = firstElement[1]
377+
r['highlightedKey'] = hrt[key]
378+
r['highlightedValue'] = val
379+
end
380+
end
381+
end
382+
response
383+
end
366384
end

views/search/solrTemplate.html.erb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,14 @@
226226
<%=item['reporting_org_narrative'].first%>
227227
<%end%>
228228
</p>
229-
229+
<%if item.has_key?('highlightedKey')%>
230+
<span class="text-highlighting" style="color: grey">
231+
<%begin%>
232+
<p>Found a match in field: <%=item['highlightedKey']%></p>
233+
<%rescue%>
234+
<%end%>
235+
</span>
236+
<%end%>
230237
<p class="govuk-body-s description">
231238
<%begin%>
232239
<%=item['description_narrative'].first%>
@@ -633,17 +640,23 @@
633640
var prepareContainer = '';
634641
$.each(response.output.docs,function(i, result){
635642
var startDate = new Date(result.activity_date_start_common);
643+
var highlightedKey = '';
644+
var highlightedVal = '';
645+
if (result.hasOwnProperty('highlightedKey')) {
646+
highlightedKey = result.highlightedKey;
647+
highlightedVal = result.highlightedValue[0];
648+
}
636649
if (result.reporting_org_narrative[0] != 'UK - Department for International Development (DFID)') {
637650
var reporting_org = result.reporting_org_narrative[0];
638651
}
639652
else{
640653
var reporting_org = 'UK - Foreign, Commonwealth and Development Office';
641654
}
642655
if(typeof result.description_narrative === 'undefined'){
643-
prepareContainer = prepareContainer + '<div class="app-search-result"><h3 class="govuk-heading-s app-search-result-title"><a href="/projects/'+encodeURIComponent(result.iati_identifier).toString()+'/summary" class="govuk-link--no-visited-state">' + result.title_narrative_first + '</a></h3><p class="govuk-body-s app-search-result-department">' + reporting_org + '</p><p class="govuk-body-s description">' + 'No description available' + '</p><div class="app-search-result-info"><div><span class="govuk-body-s app-search-result-info__title">Programme Id</span><span class="govuk-body-s app-search-result-info__value">' + result.iati_identifier + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Start date</span><span class="govuk-body-s app-search-result-info__value"> ' + startDate.getFullYear() + '-' + (startDate.getMonth()+1) + '-' + startDate.getDate() + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Activity status</span><span class="govuk-body-s app-search-result-info__value">' + activityStatus[parseInt(result.activity_status_code) - 1].name + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Total budget</span><span class="govuk-body-s app-search-result-info__value">'+result.totalBudgetWithCurrency+'</span></div></div></div>';
656+
prepareContainer = prepareContainer + '<div class="app-search-result"><h3 class="govuk-heading-s app-search-result-title"><a href="/projects/'+encodeURIComponent(result.iati_identifier).toString()+'/summary" class="govuk-link--no-visited-state">' + result.title_narrative_first + '</a></h3><p class="govuk-body-s app-search-result-department">' + reporting_org + '</p><span class="text-highlighting" style="color: grey"><p>Found a match in field: '+highlightedKey+'</p></span><p class="govuk-body-s description">' + 'No description available' + '</p><div class="app-search-result-info"><div><span class="govuk-body-s app-search-result-info__title">Programme Id</span><span class="govuk-body-s app-search-result-info__value">' + result.iati_identifier + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Start date</span><span class="govuk-body-s app-search-result-info__value"> ' + startDate.getFullYear() + '-' + (startDate.getMonth()+1) + '-' + startDate.getDate() + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Activity status</span><span class="govuk-body-s app-search-result-info__value">' + activityStatus[parseInt(result.activity_status_code) - 1].name + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Total budget</span><span class="govuk-body-s app-search-result-info__value">'+result.totalBudgetWithCurrency+'</span></div></div></div>';
644657
}
645658
else{
646-
prepareContainer = prepareContainer + '<div class="app-search-result"><h3 class="govuk-heading-s app-search-result-title"><a href="/projects/'+encodeURIComponent(result.iati_identifier).toString()+'/summary" class="govuk-link--no-visited-state">' + result.title_narrative_first + '</a></h3><p class="govuk-body-s app-search-result-department">' + reporting_org + '</p><p class="govuk-body-s description">' + result.description_narrative[0] + '</p><div class="app-search-result-info"><div><span class="govuk-body-s app-search-result-info__title">Programme Id</span><span class="govuk-body-s app-search-result-info__value">' + result.iati_identifier + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Start date</span><span class="govuk-body-s app-search-result-info__value"> ' + startDate.getFullYear() + '-' + (startDate.getMonth()+1) + '-' + startDate.getDate() + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Activity status</span><span class="govuk-body-s app-search-result-info__value">' + activityStatus[parseInt(result.activity_status_code) - 1].name + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Total budget</span><span class="govuk-body-s app-search-result-info__value">'+result.totalBudgetWithCurrency+'</span></div></div></div>';
659+
prepareContainer = prepareContainer + '<div class="app-search-result"><h3 class="govuk-heading-s app-search-result-title"><a href="/projects/'+encodeURIComponent(result.iati_identifier).toString()+'/summary" class="govuk-link--no-visited-state">' + result.title_narrative_first + '</a></h3><p class="govuk-body-s app-search-result-department">' + reporting_org + '</p><span class="text-highlighting" style="color: grey"><p>Found a match in field: '+highlightedKey+'</p></span><p class="govuk-body-s description">' + result.description_narrative[0] + '</p><div class="app-search-result-info"><div><span class="govuk-body-s app-search-result-info__title">Programme Id</span><span class="govuk-body-s app-search-result-info__value">' + result.iati_identifier + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Start date</span><span class="govuk-body-s app-search-result-info__value"> ' + startDate.getFullYear() + '-' + (startDate.getMonth()+1) + '-' + startDate.getDate() + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Activity status</span><span class="govuk-body-s app-search-result-info__value">' + activityStatus[parseInt(result.activity_status_code) - 1].name + '</span></div><div><span class="govuk-body-s app-search-result-info__title">Total budget</span><span class="govuk-body-s app-search-result-info__value">'+result.totalBudgetWithCurrency+'</span></div></div></div>';
647660
}
648661
})
649662
$('#response-container').html(prepareContainer);

0 commit comments

Comments
 (0)