In commit 4fceb0c0b71e286745b12b71336bdb52451083ca during the drupal 7 port, the way multiple values were handled was changed.
@@ -1875,9 +1842,8 @@ function apachesolr_index_key($field_info) {
default:
$type_prefix = 's';
}
- //$sm = $field['multiple'] ? 'm_' : 's_';
- $sm = $field['cardinality'] > 1 ? 'm_' : 's_';
- return $type_prefix . $sm . $field['field_name'];
+ $sm = $field['multiple'] ? 'm_' : 's_';
+ return $type_prefix . $sm . $field['name'];
}
While this was a correct change, a cardinality check was never reintroduced specifically for entity fields, so they always default to multiple even when this makes no sense.
The $field['cardinality'] part in the previous code was wrong (because "no limit" is -1, it would have erroneously been marking unlimited fields as single value), but no fixed check for this was ever reintroduced as far as I can tell, so all entity fields are being stored multivalue, even if it doesn't make sense. (cardinality 1 boolean fields in particular)
I don't know what effect this change has on everything else. It seems to work ok so far on my test site. Granted, a reindex is required for obvious reasons...
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 1379128-12-backport.patch | 11.31 KB | nick_vh |
| #15 | 1379128-15.patch | 5.64 KB | nick_vh |
| #12 | 1379128-12.patch | 11.29 KB | nick_vh |
| #11 | 1379128-11.patch | 11.46 KB | nick_vh |
| #10 | 1379128-10.patch | 11.2 KB | nick_vh |
Comments
Comment #1
pwolanin commentedI can increase the cardinality of a field in the UI at any time, right? So just as the SQL schema supports multiple values for all fields, so should Solr.
I agree that's a little wacky for booleans, but seems to be what Drupal core provides.
What's the use case where this is causing a problem for you?
Comment #2
nick_vhI actually got into this problem when I was trying to make the slider. In order to get stats to return from solr for a specific field it has to be a single. This means that one should programmatically modify the facets to make them "multiple = false" to make this stats property work. Any integer field was a multiple and I had to make sure it wasn't doing setting this field as a multiple.
This is indeed a very tricky problem and you can't change the field in Solr as easily as the drupal field. It might make sense to see if there is only 1 value in the UI and add both a single and a multiple to the index? (Overhead problem...)
Comment #3
nick_vhAfter some discussion we probably need to add a singular field for any numeric type AND for the date. The module will only take the first value from the multi valued field and any component that is using stats or sorting can then use this until Solr solves the problems it has with statistics and sorting on multivalued fields
Comment #4
nick_vhThis patch fixes the cardinality problem somehow. When doing stats queries you can rely on this functionality that there is always a singular field as well.
Attention : Only valid for integer fields
Comment #5
nick_vhA typo + leftover variable. This patch also fixes a small issue in hook_entity_update
Comment #6
nick_vhComment #7
pwolanin commentedWhile we are at it, we need to fix the query used by the term component and might want to handle caching that result
Comment #8
nick_vhComment #9
nick_vhComment #10
nick_vhThe following patch handles very basic/limited caching (we might want to use cache_set and cache_get?) + it will use the singular field for any multivalued integer/float indexed value
Comment #11
nick_vhCache_get and cache_set seem to work way better since it is not run on every page load. I feel this patch is ready for review
Comment #12
nick_vhWhitespace problem
Comment #13
nick_vhCommitted this patch. Reindexing is needed, since this will add new fields to your solr index
Comment #14
pwolanin commentedWhy is the function moved to the .inc file? I think you may end up including it on every page load now.
Comment #15
nick_vhFollow up
Comment #16
nick_vhCommitted the follow up
Comment #17
nick_vhComment #18
nick_vhCommited #12 as backport patch
Comment #19
nick_vhCommitted #15 to 6.x-3.x
Comment #20
martijn houtman commentedI have an other use case for adding single-valued indexes for fields with cardinality=1: sorting. Solr appears to be unable to sort on multivalued fields (correct me if I am wrong), but in my use-case I want to sort on custom string fields that come from a predefined list of strings.
Comment #21
martijn houtman commentedI suppose, then, that my question is: why only add these single-valued fields for numeric and date fields, only?
Comment #22
nick_vhNothing prevents you from doing a mapping_alter and also define singularity for specific text fields?
Comment #23
martijn houtman commentedFair enough, but I think it would be expected behavior to make single-valued indexes for single-valued fields, no? This would take general users a lot of effort by default.
Comment #24
nick_vhThe problem (if you have read this thread) is that in Drupal a single value field can easily be transformed into a multi value field. There is no way we can do that with solr so we decided to put all of them by default in a multi field and the first value of the field also in a single solr field.
I think for the 90% usecase this is more than sufficient so I'm a bit reluctant to open up new options and allow people to break things. text fields and facets are not a good combination because they have been processed/tokenized and do not serve facets very well.
Comment #25
martijn houtman commentedYes, I have read this thread, and I understand the problem. But I still don't know why it's been decided that only numeric and date fields have an extra single field.
By default, a string field is not tokenized:
And when a string field comes from a list or predefined strings, it facets rather well, I think.
I understand your reluctance, but if we do it well, it will not break things. At most, it will add some extra indexes to Solr. I will look into this to see if I can find a more generic solution. I understand the way Drupal stores fields internally, and that it makes sense to translate this storage model to Solr, but I think the Solr index should reflect the widget settings, rather than the field data storage settings, as it has direct implications on the way the facet search works.
Comment #26
j0rd commentedSame problem here.
What I'm personally doing is overriding the default mappers and have my own cardinality check, then set the appropriate single or multiple solr index.
For me, if a field is single in the admin, it should be single in Solr.
Only single fields can be used for sorts, which is important for what I'm doing. Also I would assume they are more performant in general.
Here's my snippet I'm using in the indexing functions to set the appropriate index.