fixed filters in search

This commit is contained in:
Bachir Soussi Chiadmi 2021-10-27 18:01:57 +02:00
parent 20fea9dce1
commit 9f1e3c4f76
6 changed files with 79 additions and 19 deletions

View File

@ -52,7 +52,9 @@ class Base extends ControllerBase {
$parse_mode->setConjunction('AND');
$this->and_query->setParseMode($parse_mode);
// Set fulltext search keywords and fields.
$this->and_query->keys($this->keys);
if ($this->keys) {
$this->and_query->keys($this->keys);
}
// in case of term id provided restrict the keys to taxo fields
if ($this->term) {
@ -83,6 +85,15 @@ class Base extends ControllerBase {
}
}
$this->and_query->addConditionGroup($filters_conditions);
if(!$this->keys) {
// if no keys but filters switch query to direct and add wildcard solr keys *:*
$direct_and_parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$direct_and_parse_mode->setConjunction('AND');
$this->and_query->setParseMode($direct_and_parse_mode);
// $this->and_query->keys('*:*');
}
}
// else{
$fulltextFields = [];
@ -151,8 +162,9 @@ class Base extends ControllerBase {
$this->or_query->setParseMode($or_parse_mode);
// Set fulltext search keywords and fields.
$this->or_query->keys($this->keys);
// $this->or_query->setFulltextFields(['field_reference']);
if ($this->keys) {
$this->or_query->keys($this->keys);
}
// exclude results from and_query
$exclude_and_results_conditions = $this->or_query->createConditionGroup('AND');
@ -180,6 +192,16 @@ class Base extends ControllerBase {
}
}
$this->or_query->addConditionGroup($or_filters_conditions);
if(!$this->keys) {
// if no keys but filters switch query to direct and add wildcard solr keys *:*
$direct_or_parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$direct_or_parse_mode->setConjunction('OR');
$this->or_query->setParseMode($direct_or_parse_mode);
// $this->or_query->keys('*:*');
}
}
// Restrict the search to specific languages.
@ -204,6 +226,9 @@ class Base extends ControllerBase {
$this->results['uuids'][] = $result->getField('uuid')->getValues()[0];
$this->results['nids'][] = $result->getField('nid')->getValues()[0];
}
// todo you may like / more like this
}
private function defaultQuery(){
@ -264,21 +289,43 @@ class Base extends ControllerBase {
),
];
if ($this->keys) {
if ($this->keys || $this->filters) {
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
$this->sapiQuery();
$resp['keys'] = $this->keys;
$resp['term'] = $this->term;
$resp['filters'] = $this->filters;
// $resp['count'] = $this->results->getResultCount();
$resp['count'] = count($this->results['nids']);
$resp['exactematch_count'] = $this->exactematch_count;
$resp['infos'] = t('The search found @exactmatchcount exact match result(s) for @count total result(s) with keywords @keys', array(
$resp['infos'] = t('The search found @exactmatchcount exact match result(s) for @count total result(s) with', array(
"@exactmatchcount" => $resp['exactematch_count'],
"@count" => $resp['count'],
"@keys" => $this->keys
"@count" => $resp['count']
));
if ($this->keys) {
$resp['infos'] .= t(' keywords @keys', array(
"@keys" => $this->keys
));
}
if ($this->keys && $this->filters) {
$resp['infos'] .= " and";
}
if ($this->filters) {
// get the terms names from tid
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$filters_names = [];
foreach ($this->filters as $tid) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $storage->load($tid);
$entity_trans = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $lang);
$filters_names[] = $entity_trans->getName();
}
$resp['infos'] .= t(' filters @filters', array(
"@filters" => implode(', ', $filters_names)
));
}
// $resp['options'] = $this->query->getOptions();
// $uuids = [];

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -30,19 +30,24 @@ export default {
},
methods: {
submit() {
console.log("search submited", this.typed, this.autocomplete)
console.log("search submited", this.typed, this.autocomplete, this.filters, this.slimFilters)
// unfocus the text input to hide the keyboard on mobile device
this.$input.blur()
// New search is triggered by Base.vue with router (which will also fill the store)
// cleaning slimfilters
let filters = []
this.slimFilters.forEach((filter, index) => {
console.log('foreach filters', filter)
console.log('onsubmit foreach filters', filter)
if(filter){
filters.push(filter)
}
})
console.log('Cleaning filters',this.slimFilters, filters)
// // this is just in case we landed in the page with filters in url params,
// // this.slimFilters is not filled but store/search filters is
// if(!filters.length){
// filters = this.filters
// }
console.log('Cleaning filters',this.slimFilters, this.filters, filters)
// push router
this.$router.push({name:'base', query:{
keys:this.typed,
@ -69,7 +74,7 @@ export default {
},
onSelectFiltersChange(index, info){
console.log('onSelectFiltersChange', index, info, this.filters)
console.log('onSelectFiltersChange', index, info, this.slimFilters, this.filters)
this.slimFilters[index] = info.value
},
onClickFilters(e){
@ -131,6 +136,7 @@ export default {
// customize the select filters
// http://slimselectjs.com/options
const selects = this.$el.querySelectorAll('select')
let slim;
selects.forEach((selectElement, index) => {
// get the first option to make the placeholder then empty it
const placeholder_option = selectElement.querySelector('option:first-child')
@ -139,7 +145,7 @@ export default {
placeholder_option.removeAttribute("value")
placeholder_option.setAttribute("data-placeholder", true)
placeholder_option.innerHTML = ''
new SlimSelect({
slim = new SlimSelect({
select: selectElement,
placeholder: placeholder,
// allowDeselect: true
@ -150,7 +156,10 @@ export default {
this.onSelectFiltersChange(index, info)
}
})
console.log('slimselect selected', slim.selected(), index)
this.slimFilters[index] = slim.selected()
})
console.log('slimSelect after init', this.slimFilters)
},
render(h) {
// console.log('searchForm render')

View File

@ -69,7 +69,7 @@ export default {
},
setFilters (state, filters) {
console.log('store search setFilters', filters)
state.filters = filters
state.filters = typeof filters === 'string' ? filters.split(',') : filters
},
setInfos (state, infos) {
state.infos = infos
@ -89,6 +89,9 @@ export default {
resetOffset (state) {
state.offset = 0
},
resetInfos (state) {
state.infos = false
},
incrementOffset (state) {
state.offset += state.limit
},
@ -106,6 +109,7 @@ export default {
commit('resetCount')
commit('resetNoresults')
commit('resetOffset')
commit('resetInfos')
if (state.keys || state.term) {
this.commit('Common/setPagetitle', state.keys)
} else {
@ -128,7 +132,7 @@ export default {
}
if (state.filters) {
console.log('getResults filters', state.filters)
params.filters = state.filters
params.filters = state.filters.join(',')
}
// console.log('Search getResults params', params)
const q = qs.stringify(params)