From b9e092354c1b88bc11481038f48cfc745f02a355 Mon Sep 17 00:00:00 2001 From: bach Date: Thu, 17 Aug 2023 19:13:59 +0200 Subject: [PATCH] added content_type filter to search api --- ...yml => search_api.index.concernements.yml} | 8 +- src/config/sync/search_api.index.entites.yml | 164 +++++++++++ .../graphql/ouatt_extension.base.graphqls | 2 +- .../SchemaExtension/OuattSchemaExtension.php | 14 +- .../ouatt_searchapi/src/Controller/Search.php | 275 +----------------- 5 files changed, 195 insertions(+), 268 deletions(-) rename src/config/sync/{search_api.index.enquetes.yml => search_api.index.concernements.yml} (98%) create mode 100644 src/config/sync/search_api.index.entites.yml diff --git a/src/config/sync/search_api.index.enquetes.yml b/src/config/sync/search_api.index.concernements.yml similarity index 98% rename from src/config/sync/search_api.index.enquetes.yml rename to src/config/sync/search_api.index.concernements.yml index ebe4f53..bc570b3 100644 --- a/src/config/sync/search_api.index.enquetes.yml +++ b/src/config/sync/search_api.index.concernements.yml @@ -17,8 +17,8 @@ dependencies: - node - paragraphs - search_api -id: enquetes -name: enquetes +id: concernements +name: Concernements description: '' read_only: false field_settings: @@ -40,6 +40,7 @@ field_settings: datasource_id: 'entity:node' property_path: field_caillou type: text + boost: !!float 2 dependencies: config: - field.storage.node.field_caillou @@ -48,6 +49,7 @@ field_settings: datasource_id: 'entity:node' property_path: field_description type: text + boost: !!float 2 dependencies: config: - field.storage.node.field_description @@ -69,6 +71,7 @@ field_settings: datasource_id: 'entity:node' property_path: field_recit_colophon type: text + boost: 1.3 dependencies: config: - field.storage.node.field_recit_colophon @@ -163,7 +166,6 @@ datasource_settings: default: false selected: - concernement - - entite languages: default: true selected: { } diff --git a/src/config/sync/search_api.index.entites.yml b/src/config/sync/search_api.index.entites.yml new file mode 100644 index 0000000..1588e27 --- /dev/null +++ b/src/config/sync/search_api.index.entites.yml @@ -0,0 +1,164 @@ +uuid: 5fe5a486-3192-4bad-a12a-f93871b512bf +langcode: fr +status: true +dependencies: + config: + - field.storage.node.field_action + - field.storage.node.field_sources + - field.storage.paragraph.field_description + - field.storage.paragraph.field_liens + - field.storage.node.field_menace_maintien + - search_api.server.ouatterrir + module: + - paragraphs + - node + - search_api +id: entites +name: Entités +description: '' +read_only: false +field_settings: + field_action: + label: Action + datasource_id: 'entity:node' + property_path: field_action + type: text + dependencies: + config: + - field.storage.node.field_action + field_liens: + label: 'Experiences vécues » Paragraph » Liens' + datasource_id: 'entity:node' + property_path: 'field_sources:entity:field_liens' + type: string + dependencies: + config: + - field.storage.node.field_sources + - field.storage.paragraph.field_liens + module: + - paragraphs + field_menace_maintien: + label: Menace/Maintien + datasource_id: 'entity:node' + property_path: field_menace_maintien + type: text + dependencies: + config: + - field.storage.node.field_menace_maintien + nid: + label: 'Identifiant (ID)' + datasource_id: 'entity:node' + property_path: nid + type: integer + dependencies: + module: + - node + node_grants: + label: 'Information d''accès du nœud.' + property_path: search_api_node_grants + type: string + indexed_locked: true + type_locked: true + hidden: true + processed: + label: 'Experiences vécues » Paragraph » Experience vécue » Texte traité' + datasource_id: 'entity:node' + property_path: 'field_sources:entity:field_description:processed' + type: text + dependencies: + config: + - field.storage.node.field_sources + - field.storage.paragraph.field_description + module: + - paragraphs + status: + label: status + datasource_id: 'entity:node' + property_path: status + type: boolean + indexed_locked: true + type_locked: true + dependencies: + module: + - node + title: + label: Title + datasource_id: 'entity:node' + property_path: title + type: string + dependencies: + module: + - node + uid: + label: uid + datasource_id: 'entity:node' + property_path: uid + type: integer + indexed_locked: true + type_locked: true + dependencies: + module: + - node + uuid: + label: 'Identifiant universel unique (UUID)' + datasource_id: 'entity:node' + property_path: uuid + type: string + dependencies: + module: + - node +datasource_settings: + 'entity:node': + bundles: + default: false + selected: + - entite + languages: + default: true + selected: { } +processor_settings: + add_url: { } + aggregated_field: { } + content_access: + weights: + preprocess_query: -30 + entity_status: { } + entity_type: { } + html_filter: + weights: + preprocess_index: -15 + preprocess_query: -15 + all_fields: false + fields: + - field_action + - field_menace_maintien + title: true + alt: true + tags: + b: 2 + em: 1 + h1: 5 + h2: 3 + h3: 2 + strong: 2 + u: 1 + ignorecase: + weights: + preprocess_index: -20 + preprocess_query: -20 + all_fields: false + fields: + - field_action + - field_menace_maintien + - title + - uuid + language_with_fallback: { } + rendered_item: { } +tracker_settings: + default: + indexing_order: lifo +options: + cron_limit: 50 + index_directly: true + track_changes_in_references: true +server: ouatterrir diff --git a/src/web/modules/custom/ouatt_graphql/graphql/ouatt_extension.base.graphqls b/src/web/modules/custom/ouatt_graphql/graphql/ouatt_extension.base.graphqls index abd7522..d027f98 100644 --- a/src/web/modules/custom/ouatt_graphql/graphql/ouatt_extension.base.graphqls +++ b/src/web/modules/custom/ouatt_graphql/graphql/ouatt_extension.base.graphqls @@ -41,7 +41,7 @@ type Entite implements NodeInterface { uuid: String! bundle: String! title: String! - author: String + author: User created: String changed: String action: String diff --git a/src/web/modules/custom/ouatt_graphql/src/Plugin/GraphQL/SchemaExtension/OuattSchemaExtension.php b/src/web/modules/custom/ouatt_graphql/src/Plugin/GraphQL/SchemaExtension/OuattSchemaExtension.php index c107cfd..f77dfea 100644 --- a/src/web/modules/custom/ouatt_graphql/src/Plugin/GraphQL/SchemaExtension/OuattSchemaExtension.php +++ b/src/web/modules/custom/ouatt_graphql/src/Plugin/GraphQL/SchemaExtension/OuattSchemaExtension.php @@ -464,14 +464,20 @@ class OuattSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('path', $builder->fromValue('field_menace_maintien.value')) ); + // $registry->addFieldResolver('Entite', 'author', + // $builder->compose( + // $builder->produce('entity_owner') + // ->map('entity', $builder->fromParent()), + // $builder->produce('entity_label') + // ->map('entity', $builder->fromParent()) + // ) + // ); + $registry->addFieldResolver('Entite', 'author', $builder->compose( $builder->produce('entity_owner') - ->map('entity', $builder->fromParent()), - $builder->produce('entity_label') ->map('entity', $builder->fromParent()) - ) - ); + )); $registry->addFieldResolver('Entite', 'sources', $builder->produce('entity_reference_revisions') diff --git a/src/web/modules/custom/ouatt_searchapi/src/Controller/Search.php b/src/web/modules/custom/ouatt_searchapi/src/Controller/Search.php index ecaebf6..33664b4 100644 --- a/src/web/modules/custom/ouatt_searchapi/src/Controller/Search.php +++ b/src/web/modules/custom/ouatt_searchapi/src/Controller/Search.php @@ -27,185 +27,18 @@ class Search extends ControllerBase { // https://www.hashbangcode.com/article/drupal-8-date-search-boosting-search-api-and-solr-search // https://kgaut.net/blog/2018/drupal-8-search-api-effectuer-une-requete-dans-le-code.html - - // Otherwise, if you use Solr, you can also set the parse mode to “Direct query” - // and use something like 'tm_name:alex AND tm_surname:john' as the keywords (field names may vary). - // That will also ensure scoring works for the keywords (the other way just adds filters, which don’t affect scoring). - // https://www.drupal.org/project/search_api/issues/3049097 - - - - // $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); - - $this->index = Index::load('enquetes'); + // todo choose index regarding the content_type param + if ($this->content_type === 'entites') { + $this->index = Index::load('entites'); + }else{ + $this->index = Index::load('concernements'); + } $this->results = [ 'uuids' => [], 'nids' => [] ]; - // // ,---.| ,---. - // // |---'|---.,---.,---.,---.,---. | |. .,---.,---., . - // // | | || ,---|`---.|---' | || ||---'| | | - // // ` ` '` `---^`---'`---' `---\`---'`---'` `---| - // // `---' - // // (to match exact materials names (like "wood-skin")) - // $this->phrase_query = $this->index->query(['offset'=>0,'limit'=>10000]); - // // set parse mode and conjunction - // $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode') - // ->createInstance('phrase'); - // $parse_mode->setConjunction('AND'); - // $this->phrase_query->setParseMode($parse_mode); - // // Set fulltext search keywords and fields. - // if ($this->keys) { - // $this->phrase_query->keys(implode(' ', $this->keys)); - // } - // $this->phrase_query->setFulltextFields(['title']); - - // // Restrict the search to specific languages. - // $this->phrase_query->setLanguages([$lang]); - - // // Do paging. - // // $this->and_query->range($this->offset, $this->limit); - // // retrieve all results - // // $this->and_query->range(0, -1); - - // // Add sorting. - // $this->phrase_query->sort('search_api_relevance', 'DESC'); - - // // Set one or more tags for the query. - // // @see hook_search_api_query_TAG_alter() - // // @see hook_search_api_results_TAG_alter() - // $this->phrase_query->addTag('ouatt_searchapi_search_phrase_query'); - - // $phrase_results = $this->phrase_query->execute(); - - // foreach ($phrase_results as $result) { - // $this->results['uuids'][] = $result->getField('uuid')->getValues()[0]; - // $this->results['nids'][] = $result->getField('nid')->getValues()[0]; - // } - - - - // // ,---. | ,---. - // // |---|,---.,---| | |. .,---.,---., . - // // | || || | | || ||---'| | | - // // ` '` '`---' `---\`---'`---'` `---| - // // `---' - // $this->and_query = $this->index->query(['offset'=>0,'limit'=>10000]); - // // set parse mode and conjunction - // $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode') - // ->createInstance('terms'); - // $parse_mode->setConjunction('AND'); - // $this->and_query->setParseMode($parse_mode); - // // Set fulltext search keywords and fields. - // if ($this->keys) { - // $this->and_query->keys(implode(' ', $this->keys)); - // } - - // // in case of term id provided restrict the keys to taxo fields - // if ($this->terms && is_array($this->terms) && count($this->terms)) { - // $term_conditions = $this->and_query->createConditionGroup('OR'); - // // $term = (int) $this->term; - // foreach ($this->terms as $term) { - // $tid = $term->value; - // foreach (['tag_tid', 'thesaurus_tid'] as $field) { - // $term_conditions->addCondition($field, (int) $tid); - // } - // } - // $this->and_query->addConditionGroup($term_conditions); - - // // INSTEAD TRY TO BOOST THE TAG AND THESAURUS FIELDS - // // foreach ($taxoSolrFieldsName as $fname) { - // // // $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solrField})),3.16e-11,10,0.1)"); - // // $bfparam = "if(gt(termfreq({$fname},{$this->term}),0),^21,0)"; - // // $this->or_query->addParam('bf', $bfparam); - // // } - // // look @ ouatt_searchapi_search_api_solr_query_alter in ouatt_searchapi.module - // // $this->or_query->setOption('termid', $this->term); - // } - - // if ($this->filters) { - // // FILTERS - // $filters_conditions = $this->and_query->createConditionGroup('AND'); - // foreach ($this->filters as $filter) { - // $filter = (int) $filter; - // foreach (['thesaurus_tid'] as $field) { // 'tag_tid', - // $filters_conditions->addCondition($field, $filter); - // } - // } - // $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 = []; - // // Recherche uniquement sur les champ thésaurus et tag - // $fulltextFields += [ - // 'thesaurus_name_0', - // 'thesaurus_synonyms_0', - // 'thesaurus_name_1', - // 'thesaurus_synonyms_1', - // 'thesaurus_name_2', - // 'thesaurus_synonyms_2', - // 'thesaurus_name_3', - // 'thesaurus_synonyms_3', - // 'thesaurus_name_4', - // 'thesaurus_synonyms_4', - // 'thesaurus_name', - // 'thesaurus_synonyms', - // 'tag_name', - // 'tag_synonyms' - // ]; - // if(count($fulltextFields)){ - // $this->and_query->setFulltextFields($fulltextFields); - // } - // // } - - // // Restrict the search to specific languages. - // $this->and_query->setLanguages([$lang]); - - // // Do paging. - // // $this->and_query->range($this->offset, $this->limit); - // // retrieve all results - // // $this->and_query->range(0, -1); - - // // Add sorting. - // $this->and_query->sort('search_api_relevance', 'DESC'); - - // // Set one or more tags for the query. - // // @see hook_search_api_query_TAG_alter() - // // @see hook_search_api_results_TAG_alter() - // $this->and_query->addTag('ouatt_searchapi_search_and_query'); - - // $and_results = $this->and_query->execute(); - - // foreach ($and_results as $result) { - // // !! have to remove duplicates from phrase query - // $nid = $result->getField('nid')->getValues()[0]; - // if ( !in_array($nid, $this->results['nids']) ) { - // $this->results['uuids'][] = $result->getField('uuid')->getValues()[0]; - // $this->results['nids'][] = $result->getField('nid')->getValues()[0]; - // } - // } - - - // $this->exactematch_count = count($this->results['nids']); - - - - // ,---. ,---. - // | |,---. | |. .,---.,---., . - // | || | || ||---'| | | - // `---'` `---\`---'`---'` `---| - // `---' $this->or_query = $this->index->query(['offset'=>0,'limit'=>10000]); // Change the parse mode for the search. @@ -224,23 +57,6 @@ class Search extends ControllerBase { $this->or_query->keys(implode(' ', $this->keys)); } - // // exclude results from previous and_query - // !! trigering solr "too many boolean clauses" error - // $exclude_and_results_conditions = $this->or_query->createConditionGroup('AND'); - // foreach ($this->results['nids'] as $nid) { - // $exclude_and_results_conditions->addCondition('nid', $nid, '<>'); - // } - // $this->or_query->addConditionGroup($exclude_and_results_conditions); - - // if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', implode(' ', $this->keys), $matches)) { - // // in case we search for material references like W0117 - // $ref_conditions = $this->or_query->createConditionGroup('OR'); - // foreach ($matches[0] as $key => $value) { - // $ref_conditions->addCondition('field_reference', $value); - // } - // $this->or_query->addConditionGroup($ref_conditions); - // } - // if ($this->filters) { // // FILTERS // $or_filters_conditions = $this->or_query->createConditionGroup('OR'); @@ -263,14 +79,6 @@ class Search extends ControllerBase { // } - // Restrict the search to specific languages. - // $this->or_query->setLanguages([$lang]); - - // Do paging. - // $this->or_query->range($this->offset, $this->limit); - // retrieve all results - // $this->or_query->range(0, -1); - // Add sorting. $this->or_query->sort('search_api_relevance', 'DESC'); @@ -290,8 +98,6 @@ class Search extends ControllerBase { } } - // todo you may like / more like this - } /** @@ -305,20 +111,11 @@ class Search extends ControllerBase { $this->keys = Tags::explode($this->keys); // \Drupal::logger('ouatt_searchapi')->notice($this->keys); } - // get the exacte term id in case of autocomplete - // $this->terms = $request->query->get('terms'); - $t = $request->query->get('terms'); - // $this->terms = strlen($t) ? explode(',', $t) : null; - $this->terms = $t && strlen($t) ? json_decode($t) : null; + + $this->content_type = $request->query->get('content_type'); + // $this->content_type = $ct && strlen($ct) ? json_decode($ct) : null; - // get the filters of advanced search - $f = $request->query->get('filters'); - $this->filters = $f && strlen($f) ? explode(',', $f) : null; - // $this->allparams = $request->query->all(); - // $request->attributes->get('_raw_variables')->get('filters') - // - // $this->offset = $request->query->get('offset') ?? $this->offset; - // $this->limit = $request->query->get('limit') ?? $this->limit; + } /** @@ -327,58 +124,16 @@ class Search extends ControllerBase { public function getResults(Request $request) { $this->parseRequest($request); - $resp = [ - // 'range' => array( - // 'offset' => $this->offset, - // 'limit' => $this->limit - // ), - ]; - - if ($this->keys || $this->filters) { - // $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); + $resp = []; + if ($this->keys) { + $this->sapiQuery(); - $resp['keys'] = json_encode($this->keys); - $resp['terms'] = json_encode($this->terms); - $resp['filters'] = $this->filters; - // $resp['count'] = $this->results->getResultCount(); + $resp['content_type'] = $this->content_type; + $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', array( - // "@exactmatchcount" => $resp['exactematch_count'], - // "@count" => $resp['count'] - // )); - if ($this->keys) { - $resp['infos'] .= t(' keywords @keys', array( - "@keys" => implode(', ', $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 = []; - // $nids = []; - // foreach ($this->results as $result) { - // $uuids[] = $result->getField('uuid')->getValues()[0]; - // $nids[] = $result->getField('nid')->getValues()[0]; - // } $resp['nids'] = array_slice($this->results['nids'], $this->offset, $this->limit); $resp['uuids'] = array_slice($this->results['uuids'], $this->offset, $this->limit); }