- 🇦🇷Argentina drupal-son
Hi,
I would like to combine location fields who have latitude / longitude fields on it to be able to filter by distance.
I can make it work for 1 single field, I just drill down till the coordinates latlon pairs and configure the field type to latitude/longitude.But now I have a main content type with 3 entity reference fields to a location content type.
The location content type contains the geofield.I want to combine the 3 and filter by location (distance) on the 3 at a time.
I tried creating a Processor Plugin but wasn't able to accomplish what I needed.
I tried the Union aggregation but that aggregates the whole referenced entity.
I need to drill down to the coordinates latlon on the combined as an indexed field I can use on my index.
Is there a way to drill down on an aggregated field?What is the best wat to do this?
Thanks.
- 🇦🇹Austria drunken monkey Vienna, Austria
Seems like the “Aggregated field” provided by Search API would already mostly cover that use case, except that the UI currently doesn’t allow you to specify nested properties as the source of the aggregation (as you say, you can only aggregate the whole entities) – see ✨ Allow adding indirect fields/properties in aggregate_fields processor Active . However, you can already get this to work correctly by directly editing the search index’s config (see, e.g., comment #20 in the linked issue for an example). Adding support in the UI for this is, unfortunately, not that simple.
- 🇦🇷Argentina drupal-son
@drunken-monkey that is an interesting approach ...
I tried it and this was the result:
- I created an aggregated field with my 3 location fields.
- I exported the configuration and changed by hand the 3 fields to point to the nested geofield and nested lat/lon value (just like I see when drilling down a single location field).
- I imported the configuration.
- I reindexed again and cleared caches.
- I tested my query using the search_api_location filter aggregated field value.
- Always get no results no matter how big radius I use.This is how the aggregated field looks like after manual tweaking:
field_locations_union: label: 'Locations Union' property_path: aggregated_field type: location configuration: type: union fields: - 'entity:node/field_common_locations:field_ph_coordinates:latlon' - 'entity:node/field_en_locations:field_ph_coordinates:latlon' - 'entity:node/field_ph_locations:field_ph_coordinates:latlon'
This is how the search api query location filter looks like:
$location_options[] = [ 'field' => 'field_locations_union', 'lat' => $geocoding['lat'], 'lon' => $geocoding['lon'], 'radius' => $geocoding['radius'], ]; $query->setOption('search_api_location', $location_options);
I was curious how the UI will behave after reimporting the manual tweaked configuration ... It looked like if no fields were selected to be combined. However if I exported the configuration again, the manual changes remain the same, so they are not destroyed on subsequent config exports.
Any other hint or idea?
- Status changed to Postponed: needs info
7 months ago 9:51am 5 May 2024 - 🇦🇹Austria drunken monkey Vienna, Austria
At a glance, this looks like it should have worked.
After reindexing, can you look into the indexed contents of your Solr server and check whether the
field_locations_union
field (probablylocm_field_locations_union
in Solr) is present, and the contents look as expected?
Then you could try to look at the Solr request generated for your search query (using, e.g., the Solr Devel module) and see whether that contains the appropriate filter parameters.