From 042e111bc44a83e250c36b7bd58e038bdd10054b Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Mon, 30 Oct 2017 14:48:23 +0100 Subject: [PATCH] created proper solr call for filtered search --- .../materiobasemod/materio_search_api.module | 16 ++-- .../materio_search_api.pages.inc | 83 ++++++++++++++++++- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/sites/all/modules/gui/materiobasemod/materio_search_api.module b/sites/all/modules/gui/materiobasemod/materio_search_api.module index 9fc842cd..0c510242 100755 --- a/sites/all/modules/gui/materiobasemod/materio_search_api.module +++ b/sites/all/modules/gui/materiobasemod/materio_search_api.module @@ -682,7 +682,7 @@ function materio_search_api_search_form($form, &$form_state){ // add a back to search/base home bouton if on explore/foo if ($path == 'explore' || $path == 'bookmarks' || $path == 'lists' ) { - + // buttons form container $form['rightcol'] = array( '#type' => 'container', @@ -763,17 +763,17 @@ function materio_search_api_advanced_search_form($form, &$form_state){ $values = isset($form_state['values']) ? $form_state['values'] : null; // dsm($values, "values"); - - $args = arg(); // dsm($args, 'args'); // get the first elemt of url args $path = array_shift($args); - + if (isset($args[0]) && $args[0] == "filters") { + $path .= '/'.array_shift($args); + } // get the keywords from args and reconstruct the filter lines with default values $args_values = array(); - if($path == 'explore' && $args[0] !== "ajax"){ + if($path == 'explore/filters' && $args[0] !== "ajax"){ $keywords = explode("+", $args[0]); // TODO: what if a keyword contains a + // dsm($keywords, 'keywords'); @@ -837,7 +837,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){ if(strpos($trigger['#name'], 'rm-filter-') === 0){ // $form_state['filterlines'] = $form_state['filterlines']+1; $rmline = $trigger['#return_value']; - dsm($rmline, "rmline"); + // dsm($rmline, "rmline"); } } @@ -1057,7 +1057,7 @@ function materio_search_api_advanced_search_rmline_callback($form, &$form_state) function materio_search_api_advanced_search_form_validate($form, &$form_state){ if($form_state['triggering_element']['#name'] == 'search'){ // dsm($form, '$form'); - dsm($form_state, 'form_state'); + // dsm($form_state, 'form_state'); if (!isset($form_state['values']['filter-0-1']) || $form_state['values']['filter-0-1'] == '') { form_set_error('filters', 'Please choose at least 1 keyword.'); @@ -1091,7 +1091,7 @@ function materio_search_api_advanced_search_form_submit($form, &$form_state){ // dsm($keywords_str); - $form_state['redirect'] = 'explore/'.$keywords_str; //rawurlencode($keywords_str); + $form_state['redirect'] = 'explore/filters/'.$keywords_str; //rawurlencode($keywords_str); } diff --git a/sites/all/modules/gui/materiobasemod/materio_search_api.pages.inc b/sites/all/modules/gui/materiobasemod/materio_search_api.pages.inc index c1d4951e..8e606704 100755 --- a/sites/all/modules/gui/materiobasemod/materio_search_api.pages.inc +++ b/sites/all/modules/gui/materiobasemod/materio_search_api.pages.inc @@ -130,7 +130,12 @@ function materio_search_api_results_search(){ // retreive typed words separated by slashes as a sentence $args = func_get_args(); - // dsm($args, 'args'); + // dsm($args, 'materio_search_api_results_search :: args'); + + $filters_search = false; + if(isset($args[0]) && $args[0] == "filters"){ + $filters_search = array_shift($args); + } $keys = $args; // $keys = explode(' ',implode(' ',$args)); @@ -169,7 +174,12 @@ function materio_search_api_results_search(){ # Normal search if(user_access('use materio search api')){ // dsm('normal search'); - $results = msa_solrquery_materiauxbreves($typed, $language, $user, $offset, $limit); + // TODO: this two following functions calls should be merged, it's not clean as it is + if(!$filters_search){ + $results = msa_solrquery_materiauxbreves($typed, $language, $user, $offset, $limit); + }else{ + $results = msa_solrquery_filterssearch($typed, $language, $user, $offset, $limit); + } } # only breves search (+ related materials) # not used anymore as free users not exists anymore @@ -224,6 +234,7 @@ function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit) # define default bundle option (materiaux, breves) $default_bundles = array(); + // TODO: this can't work as $taxo_index is not defined yet if(isset($taxo_index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'])){ $indexed_bundles = $taxo_index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles']; foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; } @@ -307,6 +318,10 @@ function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit) $fulltxt_results = $fulltxt_query->execute(); // dsm($fulltxt_results, "fulltxt_results"); + + // -- -- merge taxo and full text search -- -- // + + # add the fulltext search results to the global results $return['results'] += $fulltxt_results['results']; @@ -331,6 +346,70 @@ function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit) return $return; } +function msa_solrquery_filterssearch($keys, $language, $user, $offset, $limit){ + // 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"); + + $return = array( + "results" => $taxo_total_results['results'] + ); + + # count global results + $return['result count'] = count($return['results']); + + # create items array with the good range + $return['items'] = array(); + + $o = -1; + foreach ($return['results'] as $nid => $value) { + $o++; + if( $o < $offset ) continue; + if ($o >= $offset+$limit) break; + + $return['items'][$nid] = node_load($nid); + } + + # TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful + + // dsm($return, 'return'); + return $return; + +} + + # not used anymore as free users not exists anymore function msa_solrquery_breves($typed, $language, $user, $offset, $limit){