Skip to content

Instantly share code, notes, and snippets.

@benhinchley
Last active October 15, 2018 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhinchley/eb1812481f56891dc1ffc5e746d60f74 to your computer and use it in GitHub Desktop.
Save benhinchley/eb1812481f56891dc1ffc5e746d60f74 to your computer and use it in GitHub Desktop.
<?php
/*
// createLocationFilter generates the location filter based on the
// location matching matrix provided by careerone
func createLocationFilter(locationLevel int, locations []int) string {
filterSegmentFmt := "(location_level_%[1]d = %[2]d OR location_level_%[1]d = 0)"
s := make([]string, 0, len(locations))
for i := locationLevel; i < len(locations)+1; i++ {
s = append(s, fmt.Sprintf(filterSegmentFmt, i, locations[i-1]))
}
return strings.Join(s, " AND ")
}
*/
function createLocationFilter($job_location_level, $location_level_values) {
$filter_segment_fmt = "(location_level_%[1]d = %[2]d OR location_level_%[1]d = 0)";
$filter_segments = [];
// start filter creation from job location search
for ($i=$job_location_level; $i < count($location_level_values); $i++) {
array_push($filter_segments, sprintf($filter_segment_fmt, $i, $location_level_values[$i-1]));
};
return implode(" AND ", $filter_segments);
}
/*
func createFilterBoostFilters(locationLevel int) map[string]string {
s := make([]string, 0, 5)
for i := 1; i <= 5; i++ {
if i < locationLevel {
continue
}
if i == locationLevel {
s = append(s, fmt.Sprintf("job_location_level <= %d", locationLevel))
continue
}
s = append(s, fmt.Sprintf("job_location_level = %d", i))
}
m := make(map[string]string, 5)
for i, f := range s {
m[fmt.Sprintf("location_level_boost_%d", i+1)] = f
}
return m
}
*/
function createFilterBoostFilters($job_location_level) {
$tmp = [];
for ($i = 0; $i <= 5; $i++) {
if ($i < $job_location_level) {
continue;
}
if ($i == $job_location_level) {
array_push($tmp, sprintf("job_location_level <= %d", $job_location_level));
continue;
}
array_push($tmp, sprintf("job_location_level = %d", $i));
}
$filters = [];
foreach ($tmp as $i => $filter) {
$filters[sprintf("location_level_boost_%d", $i)] = $filter;
}
return $filters;
}
?>
<?php
$location_level = 2;
$location_query = [0, 15216, 15279, 15295, 15299];
$location_filter = createLocationFilter($location_level, $location_query);
$values = [
"q" => "", // replace with the query string
"filter" => sprintf("(bucket_code = 'ORGANIC' OR bucket_code ='PRIORITISE') AND (%s)", $location_filter),
];
$response = $pipeline->search(array_merge($values, createFilterBoostFilters($location_level)));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment