Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGOV-784 SPIKE: convert custom date boost to processor in search api … #2495

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions web/modules/custom/portland/portland.module
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 49 additions & 38 deletions web/sites/default/config/search_api.index.full_index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -1074,6 +1075,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 13
resolution: NOW
m: '3.16e-11'
a: 0.1
b: 0.05
solr_date_range:
weights:
preprocess_index: 0
Expand Down