Browse Source

created proper solr call for filtered search

Bachir Soussi Chiadmi 7 years ago
parent
commit
042e111bc4

+ 8 - 8
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
   // add a back to search/base home bouton if on explore/foo
   if ($path == 'explore' || $path == 'bookmarks' || $path == 'lists' ) {
   if ($path == 'explore' || $path == 'bookmarks' || $path == 'lists' ) {
-    
+
     // buttons form container
     // buttons form container
     $form['rightcol'] = array(
     $form['rightcol'] = array(
       '#type' => 'container',
       '#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;
   $values = isset($form_state['values']) ? $form_state['values'] : null;
   // dsm($values, "values");
   // dsm($values, "values");
 
 
-
-
   $args = arg();
   $args = arg();
   // dsm($args, 'args');
   // dsm($args, 'args');
 
 
   // get the first elemt of url args
   // get the first elemt of url args
   $path = array_shift($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
   // get the keywords from args and reconstruct the filter lines with default values
   $args_values = array();
   $args_values = array();
-  if($path == 'explore' && $args[0] !== "ajax"){
+  if($path == 'explore/filters' && $args[0] !== "ajax"){
     $keywords = explode("+", $args[0]);
     $keywords = explode("+", $args[0]);
     // TODO: what if a keyword contains a +
     // TODO: what if a keyword contains a +
     // dsm($keywords, 'keywords');
     // dsm($keywords, 'keywords');
@@ -837,7 +837,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     if(strpos($trigger['#name'], 'rm-filter-') === 0){
     if(strpos($trigger['#name'], 'rm-filter-') === 0){
       // $form_state['filterlines'] = $form_state['filterlines']+1;
       // $form_state['filterlines'] = $form_state['filterlines']+1;
       $rmline = $trigger['#return_value'];
       $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){
 function materio_search_api_advanced_search_form_validate($form, &$form_state){
   if($form_state['triggering_element']['#name'] == 'search'){
   if($form_state['triggering_element']['#name'] == 'search'){
     // dsm($form, '$form');
     // 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'] == '') {
     if (!isset($form_state['values']['filter-0-1']) || $form_state['values']['filter-0-1'] == '') {
       form_set_error('filters', 'Please choose at least 1 keyword.');
       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);
   // dsm($keywords_str);
 
 
-  $form_state['redirect'] = 'explore/'.$keywords_str; //rawurlencode($keywords_str);
+  $form_state['redirect'] = 'explore/filters/'.$keywords_str; //rawurlencode($keywords_str);
 }
 }
 
 
 
 

+ 81 - 2
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
   // retreive typed words separated by slashes as a sentence
   $args = func_get_args();
   $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 = $args;
   // $keys = explode(' ',implode(' ',$args));
   // $keys = explode(' ',implode(' ',$args));
@@ -169,7 +174,12 @@ function materio_search_api_results_search(){
     # Normal search
     # Normal search
     if(user_access('use materio search api')){
     if(user_access('use materio search api')){
       // dsm('normal search');
       // 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)
     # only breves search (+ related materials)
     # not used anymore as free users not exists anymore
     # 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)
   # define default bundle option (materiaux, breves)
   $default_bundles = array();
   $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'])){
   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'];
     $indexed_bundles = $taxo_index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
     foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; }
     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();
   $fulltxt_results = $fulltxt_query->execute();
   // dsm($fulltxt_results, "fulltxt_results");
   // dsm($fulltxt_results, "fulltxt_results");
 
 
+
+  // -- -- merge taxo and full text search -- -- //
+
+
   # add the fulltext search results to the global results
   # add the fulltext search results to the global results
   $return['results'] += $fulltxt_results['results'];
   $return['results'] += $fulltxt_results['results'];
 
 
@@ -331,6 +346,70 @@ function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit)
   return $return;
   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
 # not used anymore as free users not exists anymore
 function msa_solrquery_breves($typed, $language, $user, $offset, $limit){
 function msa_solrquery_breves($typed, $language, $user, $offset, $limit){