Repository: OCHA-DAP/hdx-ckan

Issue:
Search page: search bar doesn't search country (ckan group)
Searching a country name via the text fields doesn't return a dataset assigned to the country (ckan group) unless the country name appears as a tag, title, or description.

Here is an example on staging (though it may be overwritten at any time; I don't want a public test of this on prod, so it isn't created there): 
https://test-data.hdx.rwlabs.org/dataset/a-test-for-country-name-searches



Discussion:
@cjhendrix I created a new Country "TTTTTT" (on my CKAN) and added a dataset to it, then searched for "TTTTTT" and it worked ... see screens below:
![screen2](https://cloud.githubusercontent.com/assets/5638491/3701627/b92eba38-13f4-11e4-85a5-b2c4d0989746.png)
![screen1](https://cloud.githubusercontent.com/assets/5638491/3701628/bbf4e54e-13f4-11e4-90d6-62b705ad9ec2.png)

Interesting, but I can still get the behavior.  On stag, there is this dataset where group = Angola
http://test-data.hdx.rwlabs.org/dataset/a-test-without-a-country-name

Searching for "angola" or "Angola" doesn't return it.   Some difference in your local solr config?

The same behavior is true for organization names as well. Would be good to fix that one while you are at it, but if it's too much for this sprint, please make a new issue for it and we'll tackle it later. 

Good point @cjhendrix about the solr schema.
@teodorescuserban - In my dev VM I'm using the ckan/config/solr/schema-2.0.xml file, are we using the same file on staging? Thanks!

@aalecs schema 2.0 is obsolete according to ckan docs and schema.xml should be used instead, which is the one in use throughout our vms.

moved to Sprint 26

@cjhendrix - I found out why http://test-data.hdx.rwlabs.org/dataset/a-test-without-a-country-name isn't showing up in the search ... it's because it's set as private. 
I did test creating a dataset with a certain country and searching for the country name and it works ok (btw the dataset doesn't have the country name in it's metadata). Also did a similar test with updating a dataset and then searching for a country. All is ok. Please advise :smiley: 

It is still not working for me.  Try this test.

Search for "Democratic Republic of the Congo".  Sort by last modified.  

You should see this dataset: http://data.hdx.rwlabs.org/dataset/idpdata, but it doesn't come up for me in the search result.

Ok, my bad: I tried to do a fool proof test, but I didn't take into account that when I created a new country the CKAN has set the code to be similar to the name so that's why it was working for me.
Now it seems that CKAN only indexes the country code and not the name for the Datasets linked to a Country. 
Two solutions:
1. Update all countries to have code's similar to their names
2. Change how CKAN indexes the datasets in order to include the country name

I think version 2 is the good one.
Pinging @mbellotti @alexandru-m-g @cjhendrix  for any idea

Sorry guys, somehow missed this one until now.  I agree with Dan.  Number 2 seems to be the most durable option and will get our heads a bit more into solr configuration since we have more of that coming down the pipe.


Resolving patch:
diff --git a/ckanext-hdx_search/ckanext/hdx_search/controllers/search_controller.py b/ckanext-hdx_search/ckanext/hdx_search/controllers/search_controller.py
--- a/ckanext-hdx_search/ckanext/hdx_search/controllers/search_controller.py
+++ b/ckanext-hdx_search/ckanext/hdx_search/controllers/search_controller.py
@@ -1,4 +1,3 @@
-
 import logging
 import datetime
 import copy
@@ -159,12 +158,32 @@ def isolate_features(context, facets, q, tab, skip=0, limit=25):
         return (feature_list[skip:skip + limit], len(features))
     return features
 
+def convert_country(q, country_names, countries):
+    print country_names
+    for c in country_names:
+        if c in q.lower():
+            q += ' '+countries[c]
+    print q
+    return q
+
+def build_country_dict(countries):
+    country_names = list()
+    country_dict = dict()
+    for c in countries:
+        country_names.append(c['display_name'].lower())
+        country_dict[c['display_name'].lower()] = c['name']
+    return (country_names, country_dict)
 
 class HDXSearchController(PackageController):
 
     def search(self):
         from ckan.lib.search import SearchError
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author, 'for_view': True,
+                   'with_private': False}
 
+        data_dict = {'all_fields': True}
+        country_names, countries = build_country_dict(get_action('group_list')(context, data_dict))
         params_to_skip = ['_show_filters']
 
         package_type = self._guess_package_type()
@@ -181,6 +200,7 @@ def search(self):
 
         # unicode format (decoded from utf8)
         q = c.q = request.params.get('q', u'')
+        q = convert_country(q, country_names, countries)
         c.query_error = False
         try:
             page = int(request.params.get('page', 1))


Test patch: