Closed (fixed)
Project:
Search API
Version:
8.x-1.x-dev
Component:
Plugins
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
6 Jan 2012 at 11:23 UTC
Updated:
17 Jun 2021 at 14:24 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
marcoka commentedyou know that views has a sample glossary view? just use that as a starting point
Comment #2
jeff.maes commentedWhen I build a view of 'nodes' (like the sample), there is no issue and I can use the regular 'Glossary mode' that is build-in into views.
The problem occurs when I build a view of 'Search API node index'. There is no Glossary option available for this...
I've fixed it by invoking hook_search_api_alter_callback_info() and creating a new 'field' that includes the first letter of the title. Now I can use that for filtering my results on the first letter...
Comment #3
rjacobs commentedHi Jeff,
I'm wondering if you would be willing to share your specific implementation of hook_search_api_alter_callback_info() and the related controller(s)? This could also be a nice case example of this hook's usage for reference.
I discovered that adding a glossary filter can also be accomplished with cck computed fields and facet API (so long as you index your custom field in the database), but I believe that the use of hook_search_api_alter_callback_info() would be preferred to cck computed fields.
Comment #4
scottsperry commentedComment #5
rjacobs commentedExcellent, thanks much!
Comment #6
ssoulless commentedCan someone explain me how to use the code above?
Comment #7
drunken monkeyCreate a custom module called
bwm.module(if you call it something else, you have to rename the function and class accordingly), put the function intobwm.moduleand the class into a separate file which you include inbwm.info. When you then enable the module, you'll get a new data alteration on your search index's "Filters" tab; and when you then enable that, you'll get a new field which only contains the first letter of thefield_last_namefield. (If you want to use a different field, you have to change that, too – and probably also the properties of the added field, to avoid confusion.)If you aren't a developer, you should probably get one to do that for you. Or go through the tutorials in the handbook – this is mostly copy-paste anyways.
Comment #9
drupak commentedI did it a bit differently and works great. I got idea from https://www.drupal.org/project/search_api_title2 Search API Title 2 module. I changed it to my version of BWM module...module file attached...What it does is adds an extra field to the index containing only first letter of the node title. You can then use its facets as well like any other facets and can work as your glossary block.
Comment #10
drupak commentedComment #11
ludo.rIn my current project I'm not allowed to use custom code.
Is it possible to do that without any custom module and without an additional module (cck computed fields) ?
To summarize, is it possible to do that with Search API + Facet API only?
Comment #12
ludo.rComment #13
ludo.rSubsidiary question: has #9 code any chance to be included in Search API module (or related)?
Comment #14
drunken monkeyIn this or a similiar form, no – but we could add this as a mode to the "Aggregated fields" data alteration, which should work just as well.
Patch attached, please see if this would work for you and makes sense in your opinion!
Comment #15
drunken monkeySo? Anybody in favor of adding this, and able to do a quick test whether it works as intended?
Comment #16
fengtanTested #14 and it works like a charm - many thanks !
Maybe someone else can confirm ?
Comment #17
fengtanActually, maybe we could also make the facets case insensitive -- like, instead of having 2 facets for each letter ('a' and 'A'), we could have a single one ('A').
Patch attached. Though maybe there is a better way to do that ?
Comment #18
fengtanIf anyone wishes to use this functionality without patching search_api, this sandbox module may help: http://drupal.org/node/2428963
Comment #19
drunken monkeyIf people want that, they can just use the "Ignore case" processor. No need to hardcode that into this data alteration.
But, in any case, thanks a lot for testing, good to hear it works!
Committed.
Comment #22
drunken monkeyWe forgot to port this to Drupal 8 (see #2765317-18: Aggregation Type - Last).
Comment #23
loziju commented@drunken monkey, the patch was created against 8.x-1.17, but it should work with 1.x-dev. I'm not sure yet how to write the test for this though, somehow majority of the tests still fail even though I already configure simpletest. Rushing for using the patch now, so I just submit this first.
##Outstanding task
reviewing the patch and writing the test are the remaining outstanding tasks.
Comment #24
loziju commentedAlright, I just focus on adding the unit test for the new first_char aggregation function and assume that the other tests should work.
And this patch is now generated against 1.x-dev.
Kindly review. Thanks!
Comment #25
aaronpinero commentedI can verify that the patch in #24 works for me when applied to 8.x-1.17 in Drupal 8.9.3.
Thank you for this. I had been hunting around for something like this that would work with Search API.
Comment #26
drunken monkeyHuge thanks for picking this up, very much appreciated!
However, in D7 this always just returned a single value (so, just the first character of the first value) and also uppercased it. I think it makes sense to keep that consistent?
See attached patch and please test/review!
Anyways, thanks a lot again!
Comment #27
drunken monkeyAny feedback on this? Can this be committed in the current form (#26)?
Comment #28
loziju commentedSorry, I didn't have time to check the patch in #26. The interdiff looks alright.
I agree with you to keep the previous behaviour for consistency / backward compatibility. However, can I suggest that we change the description to include this detail? I didn't interpret the term "first encountered field value" as the first delta. :)
How about something like this?
The "First letter" aggregation uses just the first letter of the first encountered field value as the aggregated value. For a multi-value field, only the first letter of the first field value is indexed. This can, for example, be used to build a Glossary view.
Comment #29
drunken monkeyWe already use the same phrasing for “First” and “Last”, so it would be confusing in all those cases. Maybe to clarify we should just add something to the general description for the aggregation type? For instance, how about this:
Added in the attached patch, please review!
Also, it would be great to get at least a single real-world test before committing this.
Comment #30
capysara commentedNot exactly "real-world", but it works on simplytest.me
Launch sandbox
Comment #31
drunken monkeyThanks a lot for that thorough description, looks good.
Any opinion on the added description line? Would you say the patch is RTBC?
Comment #32
capysara commentedMakes sense to me! Most of the types imply that they result in a single value, but the description makes it clearer. Very minor note: The "First letter" is the only type that puts the aggregation type in quotes. I don't think it's necessary, but it's also not really important either way.
RTBC as far as I'm concerned.
Comment #34
drunken monkeyGreat to hear, thanks a lot for your feedback.
(Fixed some code style issues and) Committed.
Thanks again, everyone!
Comment #36
artusamakIf the feedback may help, i used this patch in an IRL situation and it helps indexing a value first letter.
Two warnings though: first, you won't be able to do it on every property of the index (eg: if you want to aggregate the first letter of a title of an entity referenced field, it won't be accessible). Second, you still won't have views glossary feature accessible for your exposed filter.
I built this by using the aggregated first letter as a grouping value and duplicated this logic in an attached display (similarly to the glossary example view) to generate the glossary header. I also had to override templates to build the anchors and removed extra output unwanted.
This said, it still would be interesting to have the bridge built to benefit from core Glossary (in \Drupal\views\Plugin\views\argument\StringArgument).
I didn't have time to dig into it but i'm wondering if it would be hard to let SAPI string properties exploit this handler.
My 2 cents.