Repository: ckan/ckan

Issue:
Dataset counts incorrect on Groups listing
After upgrading to CKAN 2.8.1, the dataset counts are incorrect on the group listing screen.

On the group listing screen (/group), the dataset count for each group is the full dataset count for the whole site.  The correct count is shown on the screen for each individual group (/group/somegroup)

CKAN 2.8.1
/group
/group/somegroup




Discussion:
Hello @inghamn I was able to reproduce the issue as per the details given by you. However, I find that when the "visibility" option of the data set is "private", then only it doesn't show the correct count.
Also, if you change the visibility status of the data set to "public", you will be able to see the correct count on `/group`.

![image](https://user-images.githubusercontent.com/36654563/49846638-4406a800-fdf2-11e8-8626-7dd9449092de.png)
-------------------------------------------------------------------
-----------------------------------------------
![image](https://user-images.githubusercontent.com/36654563/49846670-6ac4de80-fdf2-11e8-9921-2ceb11558dc4.png)


All of our datasets are Public.

The problem is not that each group count is zero.  Rather each group count is the full count of all datasets on the server.  The query does not seem to actually be filtering by group.

https://data.bloomington.in.gov/group
I've tried to reproduce this issue a number of times during these months but was unable to get reported behavior even once, on different CKAN versions. Could you try to disable all custom extensions (harvest, datajson, cob), rebuild Solr index and check, whether the problem still exists? 
PS and could you also check solr logs when performing re-index?
I've disabled harvest, datasjon, and cob (our custom theme).  I've also re-indexed the solr index, and attached the solr log.  The result is the same, though.  There are no errors in the solr logs.

I didn't realize the groups page is info coming from Solr.  It must be an error in the solr queries for each of the groups.
[solr.log](https://github.com/ckan/ckan/files/2901411/solr.log)

With the knowledge that this is a Solr query error, I reviewed the logs and manually queried our Solr core.  I think this an error in the code generating the solr queries for this screen.  What's getting generated is:
```
fq=%2Bcapacity:public+groups:"operations"
```
and what should be generated is:
```
fq=capacity:public&fq=groups:"operations"
```

(I even typed it incorrectly in this comment.  Solr queries are hard to read and type!)
I suspect it is caused by the code in /ckan/logic/action/get.py at line 1883.
```python
        data_dict.setdefault('fq', '')
        if not include_private:
            data_dict['fq'] = '+capacity:public ' + data_dict['fq']
```
However, someone more familiary with ckan's building of Solr queries would know better than I.  It looks like attempting to build up the filter queries as a single fq parameter string, rather than multiple fq parameters.

Resolving patch:
diff --git a/ckan/lib/dictization/model_dictize.py b/ckan/lib/dictization/model_dictize.py
--- a/ckan/lib/dictization/model_dictize.py
+++ b/ckan/lib/dictization/model_dictize.py
@@ -313,9 +313,9 @@ def get_packages_for_this_group(group_, just_the_count=False):
             }
 
             if group_.is_organization:
-                q['fq'] = 'owner_org:"{0}"'.format(group_.id)
+                q['fq'] = '+owner_org:"{0}"'.format(group_.id)
             else:
-                q['fq'] = 'groups:"{0}"'.format(group_.name)
+                q['fq'] = '+groups:"{0}"'.format(group_.name)
 
             # Allow members of organizations to see private datasets.
             if group_.is_organization:


Test patch:
diff --git a/ckan/tests/lib/dictization/test_model_dictize.py b/ckan/tests/lib/dictization/test_model_dictize.py
--- a/ckan/tests/lib/dictization/test_model_dictize.py
+++ b/ckan/tests/lib/dictization/test_model_dictize.py
@@ -256,7 +256,9 @@ def test_group_dictize_with_package_list_limited_by_config(self):
     def test_group_dictize_with_package_count(self):
         # group_list_dictize calls it like this by default
         group_ = factories.Group()
+        other_group_ = factories.Group()
         factories.Dataset(groups=[{'name': group_['name']}])
+        factories.Dataset(groups=[{'name': other_group_['name']}])
         group_obj = model.Session.query(model.Group).filter_by().first()
         context = {'model': model, 'session': model.Session,
                    'dataset_counts': model_dictize.get_group_dataset_counts()
@@ -295,7 +297,9 @@ def test_group_dictize_for_org_with_package_list(self):
     def test_group_dictize_for_org_with_package_count(self):
         # group_list_dictize calls it like this by default
         org_ = factories.Organization()
+        other_org_ = factories.Organization()
         factories.Dataset(owner_org=org_['id'])
+        factories.Dataset(owner_org=other_org_['id'])
         org_obj = model.Session.query(model.Group).filter_by().first()
         context = {'model': model, 'session': model.Session,
                    'dataset_counts': model_dictize.get_group_dataset_counts()