splited search in two steps, 1 only taxonomy, 2 full text less 1

This commit is contained in:
Bachir Soussi Chiadmi 2014-10-22 20:30:29 +02:00
parent e5b63e455f
commit c36797f2bb
4 changed files with 222 additions and 155 deletions

View File

@ -13,11 +13,18 @@ function materio_search_api_settings(){
// dsm($languages, 'languages');
foreach ($languages as $lcode => $name) {
$form['mainsearchindex_'.$lcode] = array(
$form['fulltextsearchindex_'.$lcode] = array(
'#type'=>'select',
'#options'=>$index_options,
'#default_value' => variable_get('mainsearchindex_'.$lcode, -1),
'#title' => t('Main search api index for %lang contents.', array('%lang'=>$name)),
'#default_value' => variable_get('fulltextsearchindex_'.$lcode, -1),
'#title' => t('Full text search api index for %lang contents.', array('%lang'=>$name)),
);
$form['taxonomysearchindex_'.$lcode] = array(
'#type'=>'select',
'#options'=>$index_options,
'#default_value' => variable_get('taxonomysearchindex_'.$lcode, -1),
'#title' => t('Taxonomy search api index for %lang contents.', array('%lang'=>$name)),
);
$form['brevessearchindex_'.$lcode] = array(

View File

@ -850,7 +850,7 @@ function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
* @see materio-search-api-results.tpl.php
*/
function template_preprocess_materio_search_api_results(array &$vars) {
// dsm($vars, '$vars');
// dsm($vars, 'vars');
$results = $vars['results'];
if(isset($results['result count'])){

View File

@ -132,7 +132,6 @@ function materio_search_api_results_search(){
// dsm($args, 'args');
$typed = implode('/', $keys);
// remove query page params
preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
//dsm($pages, '$pages');
@ -140,161 +139,37 @@ function materio_search_api_results_search(){
$typed = str_replace($pages[0][0], '', $typed);
}
// dsm($typed, 'typed');
global $language;
global $user;
if(user_access('use materio search api')){
// dsm('normal search');
# switch index depending on key words type full text or taxonomy term (autocomplete selection)
$searchmode = isset($user->data['materiosearchapi_searchmode']) ? $user->data['materiosearchapi_searchmode'] : "fulltext";
switch($searchmode){
case "fulltext":
default:
$index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
break;
// case "taxonomy":
// $index_machine_name = variable_get('taxonomysearchindex_'.$language->language, -1);
// break;
}
$index = search_api_index_load($index_machine_name);
}
else if(user_access('use materio search api for breves')){
// dsm('limited search');
$index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
// dsm($index_machine_name, '$index_machine_name');
$index = search_api_index_load($index_machine_name);
}
if(!user_access('use materio search api')){
# potential results index for anonymous and free user
$could_index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
$could_index = search_api_index_load($index_machine_name);
}
if ($typed) {
# TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
try {
global $language;
global $user;
# retrieve viewmode and then use it to define the query range
$viewmode = isset($user->data['materiosearchapi_viewmode'])
? $user->data['materiosearchapi_viewmode']
: variable_get('defaultviewmode', 'full');
# retrieve viewmode and then use it to define the query range
$viewmode = isset($user->data['materiosearchapi_viewmode'])
? $user->data['materiosearchapi_viewmode']
: variable_get('defaultviewmode', 'full');
$limit = variable_get($viewmode.'_limite', '10');
$offset = pager_find_page() * $limit; //$page*$limit;//
$limit = variable_get($viewmode.'_limite', '10');
$offset = pager_find_page() * $limit; //$page*$limit;//
if(isset($index)){
# define default bundle option (materiaux, breves)
$default_bundles = array();
if(isset($index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'])){
$indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; }
}
# choose solr query bundle option
$bundles_filter = isset($user->data['materiosearchapi_bundlesfilter'])
? $user->data['materiosearchapi_bundlesfilter']
: $default_bundles;
# choose solr query options
$query_options = array('conjunction'=>'OR', 'parse mode'=>'direct');
#create the solr query
$query = search_api_query($index_machine_name, $query_options)
->keys($typed)
->range($offset, $limit);
# apply bundle options to solr query if usefull
if(count($bundles_filter)){
$filter = $query->createFilter('OR');
foreach ($bundles_filter as $type)
$filter->condition('type', $type, '=');
$query->filter($filter);
}
// $query->setOption('search_api_bypass_access', true);
# add user access solr query option
$query->setOption('search_api_access_account', $user);
# execute solr query and record results
$results = $query->execute();
}
# in case of free user search, run a real search to indicate how much items you could find
if(isset($could_index)){
$could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
// ->keys(implode(' ', $keys))
->keys($typed)
->range($offset, $limit);
// ->filter($filter);
$could_results = $could_query->execute();
// dsm($could_results, 'could_results');
}
# Normal search
if(user_access('use materio search api')){
// dsm('normal search');
$results = msa_solrquery_materiauxbreves($typed, $language, $user, $offset, $limit);
}
catch (SearchApiException $e) {
$ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
watchdog('materiobasemod', 'search error: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
}
// dsm($results, 'results');
if(user_access('use materio search api for breves')
|| user_access('use materio search api')
{
if( user_access('use materio search api') ){
if(is_array($results['results'])){
$items = $index->loadItems(array_keys($results['results']));
}else{
$items = array();
}
// dsm($items, 'items');
}
else{
$items = array();
$breves = $index->loadItems(array_keys($results['results']));
foreach ($breves as $nid => $breve) {
if(!node_access('view', $breve))
continue;
$items[] = $breve;
$materiaux = field_get_items('node',$breve,'field_materiau_ref');
// dsm($materiaux, 'materiaux');
if($materiaux){
foreach ($materiaux as $value) {
if(!isset($could_results['results'][$value['target_id']]))
continue;
$materiau = node_load($value['target_id']);
if(node_access('view', $materiau))
$items[] = $materiau;
}
}
}
$results['breves count'] = $results['result count'];
$results['result count'] = count($items);
$results['could results'] = $could_results;
}
$ret['results']['#index'] = $index;//search_api_index_load($index_machine_name);
$ret['results']['#items'] = $items;
}
else{
// for anonymous
$results['could results'] = $could_results;
# only breves search (+ related materials)
else if(user_access('use materio search api for breves')){
// dsm('limited search');
$results = msa_solrquery_breves($typed, $language, $user, $offset, $limit);
}
// dsm($typed, 'typed');
// for all case
$ret['results']['#results'] = $results;
$ret['results']['#items'] = $results['items'];
$ret['results']['#index'] = $results['index'];
$ret['results']['#theme'] = 'materio_search_api_results';
$ret['results']['#keys'] = $typed;
$ret['results']['#view_mode'] = $viewmode;
// page title
$page_title = $typed;
drupal_set_title('<i class="icon-materio-search"></i>'.check_plain($page_title), PASS_THROUGH);
@ -325,6 +200,198 @@ function materio_search_api_results_search(){
return $ret;
}
function msa_solrquery_materiauxbreves($typed, $language, $user, $offset, $limit){
// dsm($offset, 'offset');
// dsm($limit, 'limit');
// -- communs --//
# define default bundle option (materiaux, breves)
$default_bundles = array();
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; }
}
# choose solr query bundle option
$bundles_filter = isset($user->data['materiosearchapi_bundlesfilter'])
? $user->data['materiosearchapi_bundlesfilter']
: $default_bundles;
# choose solr query options
$query_options = array('conjunction'=>'OR', 'parse mode'=>'direct');
// -- -- taxo search -- -- //
# 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);
#create the solr query for taxonomy search
$taxo_query = search_api_query($taxo_index_machine_name, $query_options)
->keys($typed);
# 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);
#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");
#add range to retriev the real current results
$taxo_query->range($offset, $limit);
# execute solr query and record results
$taxo_results = $taxo_query->execute();
// dsm($taxo_results, "taxo results");
$taxo_items = $taxo_index->loadItems(array_keys($taxo_results['results']));
// dsm($taxo_items, 'taxo items');
$taxo_results['items'] = $taxo_items;
$taxo_results['index'] = $taxo_index;//search_api_index_load($index_machine_name);
// -- -- full text search -- -- //
#define search api solr index
$fulltxt_index_machine_name = variable_get('fulltextsearchindex_'.$language->language, -1);
$fulltxt_index = search_api_index_load($fulltxt_index_machine_name);
#then calculate the good offset and limit for the complementary search
$fulltxt_offset = $offset - $taxo_results['result count'];
#create the solr query for taxonomy search
$fulltxt_query = search_api_query($fulltxt_index_machine_name, $query_options)
->keys($typed)
->range(($fulltxt_offset > 0 ? $fulltxt_offset : 0), $limit); // change offset to match with the first query (taxonomy)
# apply bundle options to solr query if usefull
if(count($bundles_filter)){
$filter = $fulltxt_query->createFilter('OR');
foreach ($bundles_filter as $type)
$filter->condition('type', $type, '=');
$fulltxt_query->filter($filter);
}
# filter to remove precedent search query
# !!!!!! THIS WILL NOT WORK, IT DOESN'T KNOW THE ITEMS FROM PAGES BEFORE THE CURRENT ONE ...
$nid_filter = $fulltxt_query->createFilter('AND');
foreach ($taxo_total_results['results'] as $nid => $item)
$nid_filter->condition('nid', $nid, '<>');
$fulltxt_query->filter($nid_filter);
# add user access solr query option
$fulltxt_query->setOption('search_api_access_account', $user);
# execute solr query and record results
$fulltxt_results = $fulltxt_query->execute();
// dsm($fulltxt_results, "fulltxt_results");
# add the full text result count to the taxo result to have the total of items
$taxo_results['result count'] += $fulltxt_results['result count'];
# if we are at the end of the first search results list
if($fulltxt_offset >= 0){
$fulltxt_items = $fulltxt_index->loadItems(array_keys($fulltxt_results['results']));
// dsm($fulltxt_items, 'full txt items');
$taxo_results['items'] += $fulltxt_items;
}
# TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
return $taxo_results;
}
function msa_solrquery_breves($typed, $language, $user, $offset, $limit){
$index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
// dsm($index_machine_name, '$index_machine_name');
$index = search_api_index_load($index_machine_name);
# choose solr query options
$query_options = array('conjunction'=>'OR', 'parse mode'=>'direct');
#create the solr query
$query = search_api_query($index_machine_name, $query_options)
->keys($typed)
->range($offset, $limit);
// $query->setOption('search_api_bypass_access', true);
# add user access solr query option
$query->setOption('search_api_access_account', $user);
# execute solr query and record results
$results = $query->execute();
// dsm($results, 'results');
$items = array();
$breves = $index->loadItems(array_keys($results['results']));
foreach ($breves as $nid => $breve) {
if(!node_access('view', $breve))
continue;
$items[] = $breve;
$materiaux = field_get_items('node',$breve,'field_materiau_ref');
// dsm($materiaux, 'materiaux');
if($materiaux){
foreach ($materiaux as $value) {
if(!isset($could_results['results'][$value['target_id']]))
continue;
$materiau = node_load($value['target_id']);
if(node_access('view', $materiau))
$items[] = $materiau;
}
}
}
$results['items'] = $items;
$results['breves count'] = $results['result count'];
$results['result count'] = count($items);
#could
$could_index_machine_name = variable_get('fulltextsearchindex_'.$language->language, -1);
$could_index = search_api_index_load($could_index_machine_name);
# in case of free user search, run a real search to indicate how much items you could find
$could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
// ->keys(implode(' ', $keys))
->keys($typed);
// ->range($offset, $limit);
// ->filter($filter);
$could_results = $could_query->execute();
// dsm($could_results, 'could_results');
$results['could results'] = $could_results;
//$results['#index'] = $index;//search_api_index_load($index_machine_name);
# TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
return $results;
}
/**
* materio_search_api_actuality()
*

View File

@ -1,12 +1,5 @@
<div class="inner">
<?php
print render($searchform);
if(isset($advancedsearchform)){
print render($advancedsearchform);
}
?>
<?php print render($searchform); ?>
</div>