created sapi processor to unindex unused taxo terms, added term id condition on sapi query

This commit is contained in:
Bachir Soussi Chiadmi 2021-02-24 14:14:56 +01:00
parent 9f0b257c62
commit 182b61b08f
5 changed files with 145 additions and 16 deletions

View File

@ -7,6 +7,7 @@ dependencies:
- taxonomy - taxonomy
- synonyms - synonyms
- search_api - search_api
- materio_sapi
config: config:
- search_api.server.database_search_autocomplete - search_api.server.database_search_autocomplete
third_party_settings: third_party_settings:
@ -60,6 +61,7 @@ field_settings:
datasource_id: 'entity:taxonomy_term' datasource_id: 'entity:taxonomy_term'
property_path: name property_path: name
type: 'solr_text_custom:ngram' type: 'solr_text_custom:ngram'
boost: !!float 2
dependencies: dependencies:
module: module:
- taxonomy - taxonomy
@ -108,6 +110,7 @@ processor_settings:
solr_date_range: solr_date_range:
weights: weights:
preprocess_index: 0 preprocess_index: 0
term_used: { }
type_boost: type_boost:
boosts: boosts:
'entity:taxonomy_term': 'entity:taxonomy_term':

View File

@ -183,7 +183,7 @@ field_settings:
label: 'Tags » Taxonomy term » Term ID' label: 'Tags » Taxonomy term » Term ID'
datasource_id: 'entity:node' datasource_id: 'entity:node'
property_path: 'field_tags:entity:tid' property_path: 'field_tags:entity:tid'
type: solr_text_wstoken type: integer
boost: !!float 8 boost: !!float 8
dependencies: dependencies:
config: config:
@ -217,7 +217,7 @@ field_settings:
label: 'Thesaurus » Taxonomy term » Term ID' label: 'Thesaurus » Taxonomy term » Term ID'
datasource_id: 'entity:node' datasource_id: 'entity:node'
property_path: 'field_thesaurus:entity:tid' property_path: 'field_thesaurus:entity:tid'
type: solr_text_wstoken type: integer
boost: !!float 8 boost: !!float 8
dependencies: dependencies:
config: config:
@ -300,10 +300,8 @@ processor_settings:
- processed - processed
- tag_name - tag_name
- tag_synonyms - tag_synonyms
- tag_tid
- thesaurus_name - thesaurus_name
- thesaurus_synonyms - thesaurus_synonyms
- thesaurus_tid
- title - title
- uuid - uuid
title: true title: true

View File

@ -1047,6 +1047,7 @@ display:
operator: AND operator: AND
groups: groups:
1: AND 1: AND
group_by: false
cache_metadata: cache_metadata:
max-age: 0 max-age: 0
contexts: contexts:
@ -3205,6 +3206,7 @@ display:
filters: false filters: false
filter_groups: false filter_groups: false
title: false title: false
relationships: false
row: row:
type: fields type: fields
options: { } options: { }
@ -3332,7 +3334,7 @@ display:
click_sort_column: value click_sort_column: value
type: number_unformatted type: number_unformatted
settings: { } settings: { }
group_column: value group_column: entity_id
group_columns: { } group_columns: { }
group_rows: true group_rows: true
delta_limit: 0 delta_limit: 0
@ -3808,6 +3810,7 @@ display:
context: '0' context: '0'
menu_name: main menu_name: main
title: Thesaurus title: Thesaurus
relationships: { }
cache_metadata: cache_metadata:
max-age: 0 max-age: 0
contexts: contexts:
@ -3915,6 +3918,7 @@ display:
filters: false filters: false
filter_groups: false filter_groups: false
title: false title: false
sorts: false
row: row:
type: fields type: fields
options: { } options: { }
@ -4353,14 +4357,14 @@ display:
type_custom_false: '' type_custom_false: ''
not: false not: false
plugin_id: boolean plugin_id: boolean
langcode: changed:
id: langcode id: changed
table: content_lock table: taxonomy_term_field_revision
field: langcode field: changed
relationship: none relationship: none
group_type: group group_type: group
admin_label: '' admin_label: ''
label: 'Lock Language' label: Modifié
exclude: false exclude: false
alter: alter:
alter_text: false alter_text: false
@ -4401,9 +4405,25 @@ display:
hide_empty: false hide_empty: false
empty_zero: false empty_zero: false
hide_alter_empty: true hide_alter_empty: true
native_language: false click_sort_column: value
entity_field: langcode type: timestamp
plugin_id: language settings:
date_format: short
custom_date_format: ''
timezone: ''
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
entity_type: taxonomy_term
entity_field: changed
plugin_id: field
filters: filters:
vid: vid:
id: vid id: vid
@ -4518,6 +4538,22 @@ display:
context: '0' context: '0'
menu_name: main menu_name: main
title: Tags title: Tags
sorts:
changed:
id: changed
table: taxonomy_term_field_revision
field: changed
relationship: none
group_type: group
admin_label: ''
order: ASC
exposed: false
expose:
label: Modifié
granularity: second
entity_type: taxonomy_term
entity_field: changed
plugin_id: date
cache_metadata: cache_metadata:
max-age: 0 max-age: 0
contexts: contexts:

View File

@ -40,11 +40,25 @@ class Base extends ControllerBase {
// Set additional conditions. // Set additional conditions.
// in case we search for material references like W0117 // in case we search for material references like W0117
if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) { if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) {
$conditions = $this->query->createConditionGroup('OR'); $ref_conditions = $this->query->createConditionGroup('OR');
foreach ($matches[0] as $key => $value) { foreach ($matches[0] as $key => $value) {
$conditions->addCondition('field_reference', $value); $ref_conditions->addCondition('field_reference', $value);
} }
$this->query->addConditionGroup($conditions); $this->query->addConditionGroup($ref_conditions);
}
// in case of term id provided restrict the keys to taxo fields
if ($this->term) {
$term_conditions = $this->query->createConditionGroup('OR');
$term = (int) $this->term;
foreach (['tag_tid', 'thesaurus_tid'] as $field) {
$term_conditions->addCondition($field, $term);
}
// foreach (['tag_name', 'thesaurus_name'] as $field) {
// $term_conditions->addCondition($field, $this->keys);
// }
// $term_conditions->addCondition('tag_name', $this->keys);
// $term_conditions->addCondition('thesaurus_name', $this->keys);
$this->query->addConditionGroup($term_conditions);
} }
// Restrict the search to specific languages. // Restrict the search to specific languages.

View File

@ -0,0 +1,78 @@
<?php
namespace Drupal\materio_sapi\Plugin\search_api\processor;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\user\UserInterface;
/**
* Excludes unused terms from indexes.
*
* @SearchApiProcessor(
* id = "term_used",
* label = @Translation("Term used"),
* description = @Translation("Exclude unused taxonomy terms from being indexed."),
* stages = {
* "alter_items" = 0,
* },
* )
*/
class TermUsed extends ProcessorPluginBase {
/**
* {@inheritdoc}
*/
public static function supportsIndex(IndexInterface $index) {
$interface = EntityPublishedInterface::class;
foreach ($index->getDatasources() as $datasource) {
$entity_type_id = $datasource->getEntityTypeId();
if (!$entity_type_id) {
continue;
}
// We support users and any entities that implement
// \Drupal\Core\Entity\EntityPublishedInterface.
if ($entity_type_id === 'taxonomy_term') {
return TRUE;
}
// $entity_type = \Drupal::entityTypeManager()
// ->getDefinition($entity_type_id);
// if ($entity_type && $entity_type->entityClassImplements($interface)) {
// return TRUE;
// }
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function alterIndexedItems(array &$items) {
// Annoyingly, this doc comment is needed for PHPStorm. See
// http://youtrack.jetbrains.com/issue/WI-23586
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item_id => $item) {
$term = $item->getOriginalObject()->getValue();
$enabled = TRUE;
// $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
// $count_query = $entity_storage->getQuery()
// ->condition('type', 'materiau')
// ->accessCheck(TRUE)
// ->condition('status', '1')
// ->count();
//
// $this->count = $this->count_query->execute();
$query = \Drupal::database()->select('taxonomy_index', 'ti');
$query->fields('ti', array('nid'));
$query->condition('ti.tid', $term->id());
$query->distinct(TRUE);
$result = $query->execute();
$nodeIds = $result->fetchCol();
if (!$nodeIds) {
unset($items[$item_id]);
}
}
}
}