Sorl search based on term tid and scored is functional
This commit is contained in:
@@ -346,49 +346,96 @@ function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit)
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
function msa_solrquery_filterssearch($keys, $language, $user, $offset, $limit){
|
||||
// dsm($keys, "keys");
|
||||
$keys = explode("+", $keys);
|
||||
foreach ($keys as $value) {
|
||||
$tids[] = intval($value);
|
||||
}
|
||||
// dsm($tids, "tids");
|
||||
// dsm($offset, 'offset');
|
||||
// dsm($limit, 'limit');
|
||||
|
||||
|
||||
# define search api solr index
|
||||
$taxo_index_machine_name = variable_get('taxonomysearchindex_'.$language->language, -1);
|
||||
$taxo_index = search_api_index_load($taxo_index_machine_name);
|
||||
|
||||
# choose solr query options
|
||||
$query_options = array('conjunction'=>'AND', 'parse mode'=>'terms');
|
||||
|
||||
#create the solr query for taxonomy search
|
||||
$taxo_query = search_api_query($taxo_index_machine_name, $query_options)
|
||||
->keys($keys);
|
||||
// ->range($offset, $limit);
|
||||
|
||||
# apply bundle options to solr query if usefull
|
||||
if(count($bundles_filter)){
|
||||
$filter = $taxo_query->createFilter('OR');
|
||||
foreach ($bundles_filter as $type)
|
||||
$filter->condition('type', $type, '=');
|
||||
|
||||
$taxo_query->filter($filter);
|
||||
}
|
||||
|
||||
// $query->setOption('search_api_bypass_access', true);
|
||||
|
||||
# add user access solr query option
|
||||
$taxo_query->setOption('search_api_access_account', $user);
|
||||
|
||||
// $taxo_query->setOption('q.op', 'OR');
|
||||
|
||||
#execute first time to get the all items, to be able to filter the full text research
|
||||
$taxo_total_results = $taxo_query->execute();
|
||||
// dsm($taxo_total_results, "taxo total results");
|
||||
|
||||
// init empty results array in wich we will concaten different queries
|
||||
$return = array(
|
||||
"results" => $taxo_total_results['results']
|
||||
"results" => array()
|
||||
);
|
||||
|
||||
# define search api solr index
|
||||
$advancedsearch_index_machine_name = variable_get('advancedsearchindex_'.$language->language, -1);
|
||||
// dsm($advancedsearch_index_machine_name);
|
||||
$taxo_index = search_api_index_load($advancedsearch_index_machine_name);
|
||||
|
||||
# choose solr query options
|
||||
$query_options = array('conjunction'=>'AND');
|
||||
|
||||
// get searchable field names and run one by one the search
|
||||
// eventualy concat all results
|
||||
$fields = msa_get_custom_onto_taxo_searchable_fields();
|
||||
// run a search for each tag (results will be intersected later)
|
||||
foreach ($tids as $i => $tid) {
|
||||
$tmps_results[$i] = array();
|
||||
// run a search for a tag for each field
|
||||
foreach ($fields['tid'] as $field) {
|
||||
# create the solr query for taxonomy search
|
||||
$query = search_api_query($advancedsearch_index_machine_name, $query_options);
|
||||
// ->keys("*:*");
|
||||
// ->range($offset, $limit);
|
||||
|
||||
// https://www.drupal.org/node/2596523
|
||||
$filter = $query->createFilter('AND');
|
||||
$filter->condition($field['name'], $tid);
|
||||
$query->filter($filter);
|
||||
|
||||
# add user access solr query option
|
||||
$query->setOption('search_api_access_account', $user);
|
||||
// $query->setOption('search_api_bypass_access', true);
|
||||
|
||||
# execute
|
||||
$results = $query->execute();
|
||||
// dsm($results, "results");
|
||||
|
||||
// concat each field results for one tag
|
||||
$tmps_results[$i] += $results['results'];
|
||||
}
|
||||
}
|
||||
// dsm($tmps_results, "tmp_resuslts");
|
||||
|
||||
|
||||
// do the intersect and score routine
|
||||
if (count($tmps_results) > 1) {
|
||||
// if we have more than one tag
|
||||
$results_intersected = array_intersect_key($tmps_results[0], $tmps_results[1]);
|
||||
if(count($tmps_results > 2)){
|
||||
// if we have more than 2 tags
|
||||
for ($i=2; $i < count($tmps_results); $i++) {
|
||||
$results_intersected = array_intersect_key($results_intersected, $tmps_results[$i]);
|
||||
}
|
||||
}
|
||||
// dsm($results_intersected, 'results_intersected');
|
||||
foreach ($results_intersected as $tid => $v) {
|
||||
$scores = array();
|
||||
for ($j=0; $j < count($tmps_results); $j++) {
|
||||
$i = 0;
|
||||
foreach ($tmps_results[$j] as $key => $vv) {
|
||||
if ($tid === $key) {
|
||||
$scores[] = $i;
|
||||
break;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$results_intersected[$tid]['score'] = array_sum($scores) / count($scores);
|
||||
}
|
||||
uasort($results_intersected, "msa_cmp_filtered_results");
|
||||
// dsm($results_intersected, 'results_intersected scored');
|
||||
}
|
||||
|
||||
$return['results'] = $results_intersected;
|
||||
|
||||
# count global results
|
||||
$return['result count'] = count($return['results']);
|
||||
$return['result count'] = count($results_intersected);
|
||||
|
||||
# create items array with the good range
|
||||
$return['items'] = array();
|
||||
@@ -409,6 +456,15 @@ function msa_solrquery_filterssearch($keys, $language, $user, $offset, $limit){
|
||||
|
||||
}
|
||||
|
||||
function msa_cmp_filtered_results($a, $b){
|
||||
// dsm($a, "a");
|
||||
// dsm($b, "b");
|
||||
if ($a['score'] == $b['score']) {
|
||||
return 0;
|
||||
}
|
||||
return ($a['score'] < $b['score']) ? -1 : 1;
|
||||
}
|
||||
|
||||
|
||||
# not used anymore as free users not exists anymore
|
||||
function msa_solrquery_breves($typed, $language, $user, $offset, $limit){
|
||||
|
||||
Reference in New Issue
Block a user