From 443949b71eb2e16f5ec1219b4154988128cd0ca0 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 22 Feb 2023 11:10:56 -0800 Subject: [PATCH 1/2] PGOV-784 SPIKE: convert custom date boost to processor in search api solr module --- web/modules/custom/portland/portland.module | 36 -------- .../config/search_api.index.full_index.yml | 86 +++++++++++-------- 2 files changed, 48 insertions(+), 74 deletions(-) diff --git a/web/modules/custom/portland/portland.module b/web/modules/custom/portland/portland.module index ceebf910d7..53a5dbe10e 100644 --- a/web/modules/custom/portland/portland.module +++ b/web/modules/custom/portland/portland.module @@ -570,42 +570,6 @@ function portland_search_api_query_alter(\Drupal\search_api\Query\QueryInterface } } - -/** - * Implements hook_search_api_solr_query_alter() boost based on recently - * published date. - * See https://atendesigngroup.com/blog/drupal-8-apache-solr-boost-search-term-relevance-publish-date - */ -function portland_search_api_solr_query_alter(\Solarium\QueryType\Select\Query\Query $solarium_query, \Drupal\search_api\Query\QueryInterface $query) -{ - // Only modify the query for site search - $search_id = $query->getSearchId(); - if (strpos($search_id, "search_api_page:search_portland_gov") !== 0 && - strpos($search_id, "search_api_autocomplete:search_portland_gov") !== 0) { - return; - } - - $date_field_name = 'aggregated_sorting_date'; // This can be any UNIX timestamp field - $index = $query->getIndex(); - $fields = $index->getServerInstance()->getBackend()->getSolrFieldNames($index); - $solr_field = !empty($fields[$date_field_name]) ? $fields[$date_field_name] : ''; - // See the link above for the effect of magic numbers - if ($solr_field) { - // 1. Calculate the difference between NOW and the Date field in milliseconds - // Two weeks 8.27e-10, One week 1.65e-9, Two days 5.79e-9, One day 1.157e-8 - // 2. Use the reciprocal function y = a / (m * x + b) - // a. m = 8.27e-10: Convert the time difference into 2 weeks - // b. a = 0.15 and b = 0.1: Adjust how quickly the curve decline - // c. Can plot the function at https://www.desmos.com/calculator - // 3. Add the result to query($q) which is the original Solr score. - // https://stackoverflow.com/questions/22017616/stronger-boosting-by-date-in-solr - - // When Start Date of Event is in the future or News since the last month, give a large boost. - // Otherwise, give small boost based on start date or updated date. - $solarium_query->addParam('sort', 'if(or(and(eq(ss_content_type, "event"),gte(ms(ds_aggregated_sorting_date,NOW/DAY), 0)),and(eq(ss_content_type, "news"),gte(ms(ds_aggregated_sorting_date,NOW-1MONTH/DAY),0))),product(query($q), recip(abs(ms(NOW/DAY,ds_aggregated_sorting_date)),1.157e-8,6.5,0.5)),if(or(eq(ss_content_type, "event"),eq(ss_content_type, "news")),product(query($q), recip(abs(ms(NOW/DAY,ds_aggregated_sorting_date)),1.157e-8,2,0.5)),query($q))) desc'); - } -} - /** * Get the solr field name from the index field's machine name. * Return '' if not found diff --git a/web/sites/default/config/search_api.index.full_index.yml b/web/sites/default/config/search_api.index.full_index.yml index 8536393805..e0e2d046aa 100644 --- a/web/sites/default/config/search_api.index.full_index.yml +++ b/web/sites/default/config/search_api.index.full_index.yml @@ -3,79 +3,79 @@ langcode: en status: true dependencies: config: - - field.storage.group.field_certified_advisory_body - - field.storage.group.field_geo_map - - field.storage.group.field_official_title - - field.storage.group.field_neighborhood - - field.storage.group.field_official_organization_name - - field.storage.group.field_parent_group - - field.storage.group.field_end_date - - field.storage.group.field_start_date - - field.storage.group.field_project_status - - field.storage.group.field_project_type - - field.storage.group.field_search_keywords - - field.storage.group.field_shortname_or_acronym - - field.storage.group.field_summary - - field.storage.group.field_topics + - field.storage.taxonomy_term.field_coalition - field.storage.node.field_amended_by_council - - field.storage.node.field_solicitation_date - - field.storage.node.field_bureau - - field.storage.node.field_construction_estimate - - field.storage.node.field_community_actions - field.storage.node.field_body_content + - field.storage.node.field_bureau + - field.storage.group.field_certified_advisory_body - field.storage.node.field_city_section - - field.storage.node.field_project_status + - field.storage.node.field_community_actions + - field.storage.node.field_construction_estimate - field.storage.node.field_construction_type + - field.storage.node.field_contract_scope + - field.storage.node.field_council_document_status + - field.storage.node.field_council_document_type + - field.storage.node.field_date_of_final_disposition - field.storage.node.field_display_groups - field.storage.node.field_end_date + - field.storage.group.field_end_date - field.storage.node.field_end_time - field.storage.node.field_event_type - field.storage.node.field_feature_this_content - field.storage.node.field_feature_until - - field.storage.node.field_history + - field.storage.node.field_federal_funds + - field.storage.node.field_forecasting_description + - field.storage.node.field_full_document_title + - field.storage.node.field_geo_map + - field.storage.group.field_geo_map - field.storage.node.field_geolocation - field.storage.node.field_location + - field.storage.node.field_history + - field.storage.node.field_introduced_by + - field.storage.node.field_is_emergency_ordinance + - field.storage.node.field_load_map_from_portlandmaps - field.storage.node.field_neighborhood + - field.storage.taxonomy_term.field_neighborhood_type - field.storage.node.field_news_type + - field.storage.group.field_official_organization_name + - field.storage.group.field_official_title - field.storage.node.field_page_type + - field.storage.group.field_parent_group - field.storage.node.field_park_amenities_activities - field.storage.node.field_park_location_type - field.storage.node.field_policy_category - field.storage.node.field_policy_type - field.storage.node.field_popular - field.storage.node.field_popular_weight + - field.storage.node.field_portlandmaps_id + - field.storage.node.field_portlandmaps_layer + - field.storage.group.field_project_status + - field.storage.node.field_project_status + - field.storage.group.field_project_type - field.storage.node.field_published_on - field.storage.node.field_reservations_available - field.storage.node.field_search_keywords - field.storage.node.field_section_body + - field.storage.group.field_shortname_or_acronym + - field.storage.node.field_solicitation_date + - field.storage.node.field_solicitation_qualifier - field.storage.node.field_start_date - field.storage.node.field_start_time + - field.storage.group.field_summary - field.storage.node.field_summary - field.storage.node.field_topics - field.storage.node.field_updated_on - - field.storage.node.field_contract_scope - - field.storage.node.field_council_document_status - - field.storage.node.field_council_document_type - - field.storage.node.field_date_of_final_disposition - - field.storage.node.field_is_emergency_ordinance - - field.storage.node.field_federal_funds - - field.storage.node.field_forecasting_description - - field.storage.node.field_full_document_title - - field.storage.node.field_geo_map - - field.storage.node.field_introduced_by - - field.storage.node.field_load_map_from_portlandmaps - - field.storage.node.field_portlandmaps_id - - field.storage.node.field_portlandmaps_layer - - field.storage.node.field_solicitation_qualifier - - field.storage.taxonomy_term.field_coalition - - field.storage.taxonomy_term.field_neighborhood_type + - field.storage.group.field_neighborhood + - field.storage.group.field_search_keywords + - field.storage.group.field_start_date + - field.storage.group.field_topics - search_api.server.pantheon_solr8 module: - search_api_solr - - search_api_location + - node - group - taxonomy - - node + - search_api_location - search_api - portland third_party_settings: @@ -1074,6 +1074,16 @@ processor_settings: portland_group_title_field: { } portland_sorting_date_field: { } rendered_item: { } + solr_boost_more_recent: + weights: + preprocess_query: 0 + boosts: + aggregated_sorting_date: + boost: !!float 5 + resolution: NOW + m: '3.16e-11' + a: 0.1 + b: 0.05 solr_date_range: weights: preprocess_index: 0 From 2683a13a4b1e4d4ab0add7ddfe120bbe7b8795ca Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 22 Feb 2023 16:14:21 -0800 Subject: [PATCH 2/2] Add Updated to index --- web/sites/default/config/search_api.index.full_index.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/sites/default/config/search_api.index.full_index.yml b/web/sites/default/config/search_api.index.full_index.yml index e0e2d046aa..d44014447b 100644 --- a/web/sites/default/config/search_api.index.full_index.yml +++ b/web/sites/default/config/search_api.index.full_index.yml @@ -143,6 +143,7 @@ field_settings: fields: - 'entity:group/changed' - 'entity:node/changed' + - 'entity:node/field_updated_on' - 'entity:taxonomy_term/changed' aggregated_topic: label: 'Aggregated topic' @@ -1079,7 +1080,7 @@ processor_settings: preprocess_query: 0 boosts: aggregated_sorting_date: - boost: !!float 5 + boost: !!float 13 resolution: NOW m: '3.16e-11' a: 0.1