From ea2fd59c63a358ac61b3b612915fdb48babd57c7 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 29 May 2019 13:41:34 +0200 Subject: [PATCH] draft of materio_sapi autocomplete --- composer.json | 2 + composer.lock | 114 +++++++++++++++++- config/sync/search_api.index.autocomplete.yml | 108 +++++++++++++++++ .../materio_sapi/materio_sapi.routing.yml | 7 ++ .../src/Controller/FormAutocomplete.php | 88 ++++++++++++++ .../src/Form/MaterioSapiSearchForm.php | 10 +- 6 files changed, 325 insertions(+), 4 deletions(-) create mode 100644 config/sync/search_api.index.autocomplete.yml create mode 100644 web/modules/custom/materio_sapi/src/Controller/FormAutocomplete.php diff --git a/composer.json b/composer.json index 71d48ea6..bf21f1a2 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,8 @@ "drupal/migrate_tools": "4.x-dev", "drupal/mimemail": "1.x-dev", "drupal/restui": "^1.16", + "drupal/search_api_autocomplete": "^1.2", + "drupal/search_api_page": "1.x-dev", "drupal/search_api_solr": "^3.0", "drupal/select_translation": "1.x-dev", "drupal/simplenews": "1.x-dev", diff --git a/composer.lock b/composer.lock index 7a2cf8db..b41bb254 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5b5c2c51bef1aa5e0078a95dafa3e5b5", + "content-hash": "8a8cacad3f133bac4dc306fda59b40d7", "packages": [ { "name": "alchemy/zippy", @@ -8554,6 +8554,63 @@ "irc": "irc://irc.freenode.org/drupal-search-api" } }, + { + "name": "drupal/search_api_autocomplete", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://git.drupalcode.org/project/search_api_autocomplete.git", + "reference": "8.x-1.2" + }, + "dist": { + "type": "zip", + "url": "https://ftp.drupal.org/files/projects/search_api_autocomplete-8.x-1.2.zip", + "reference": "8.x-1.2", + "shasum": "606908583c1e0aff57a4215355d1d3e07a1d50d2" + }, + "require": { + "drupal/core": "~8.0", + "drupal/search_api": "1.x" + }, + "require-dev": { + "drupal/search_api_page": "@dev" + }, + "type": "drupal-module", + "extra": { + "branch-alias": { + "dev-1.x": "1.x-dev" + }, + "drupal": { + "version": "8.x-1.2", + "datestamp": "1552333980", + "security-coverage": { + "status": "covered", + "message": "Covered by Drupal's security advisory policy" + } + } + }, + "notification-url": "https://packages.drupal.org/8/downloads", + "license": [ + "GPL-2.0+" + ], + "authors": [ + { + "name": "Thomas Seidl", + "homepage": "https://www.drupal.org/u/drunken-monkey" + }, + { + "name": "See other contributors", + "homepage": "https://www.drupal.org/node/1142202/committers" + } + ], + "description": "Adds autocomplete functionality to searches.", + "homepage": "https://www.drupal.org/project/search_api_autocomplete", + "support": { + "source": "http://git.drupal.org/project/search_api_autocomplete.git", + "issues": "https://www.drupal.org/project/issues/search_api_autocomplete", + "irc": "irc://irc.freenode.org/drupal-search-api" + } + }, { "name": "drupal/search_api_db", "version": "1.13.0", @@ -8599,6 +8656,60 @@ "source": "https://git.drupalcode.org/project/search_api" } }, + { + "name": "drupal/search_api_page", + "version": "dev-1.x", + "source": { + "type": "git", + "url": "https://git.drupalcode.org/project/search_api_page.git", + "reference": "b0b4e96d85077722aaece1e2e48f1312352a90c4" + }, + "require": { + "drupal/core": "~8.0", + "drupal/search_api": "*" + }, + "require-dev": { + "drupal/facets": "*" + }, + "type": "drupal-module", + "extra": { + "branch-alias": { + "dev-1.x": "1.x-dev" + }, + "drupal": { + "version": "8.x-1.0-alpha12+4-dev", + "datestamp": "1557232081", + "security-coverage": { + "status": "not-covered", + "message": "Dev releases are not covered by Drupal security advisories." + } + } + }, + "notification-url": "https://packages.drupal.org/8/downloads", + "license": [ + "GPL-2.0-or-later" + ], + "authors": [ + { + "name": "borisson_", + "homepage": "https://www.drupal.org/user/2393360" + }, + { + "name": "drunken monkey", + "homepage": "https://www.drupal.org/user/205582" + }, + { + "name": "swentel", + "homepage": "https://www.drupal.org/user/107403" + } + ], + "description": "Create search pages using Search API indexes.", + "homepage": "https://www.drupal.org/project/search_api_page", + "support": { + "source": "https://git.drupalcode.org/project/search_api_page" + }, + "time": "2019-05-07T18:19:46+00:00" + }, { "name": "drupal/search_api_solr", "version": "3.0.0-beta4", @@ -17074,6 +17185,7 @@ "drupal/migrate_plus": 20, "drupal/migrate_tools": 20, "drupal/mimemail": 20, + "drupal/search_api_page": 20, "drupal/select_translation": 20, "drupal/simplenews": 20, "drupal/telephone_formatter": 10, diff --git a/config/sync/search_api.index.autocomplete.yml b/config/sync/search_api.index.autocomplete.yml new file mode 100644 index 00000000..f0496816 --- /dev/null +++ b/config/sync/search_api.index.autocomplete.yml @@ -0,0 +1,108 @@ +uuid: 89507e1b-4a2c-4e27-9e90-e3278f805e3b +langcode: en +status: true +dependencies: + module: + - search_api_solr + - taxonomy + - search_api + config: + - field.storage.taxonomy_term.field_synonyms + - search_api.server.materio +third_party_settings: + search_api_solr: + finalize: false + commit_before_finalize: false + commit_after_finalize: false + multilingual: + limit_to_content_language: true + include_language_independent: true + highlighter: + maxAnalyzedChars: 51200 + fragmenter: gap + regex: + slop: 0.5 + pattern: blank + maxAnalyzedChars: 10000 + usePhraseHighlighter: true + highlightMultiTerm: true + preserveMulti: false + highlight: + mergeContiguous: false + requireFieldMatch: false + snippets: 3 + fragsize: 0 + advanced: + index_prefix: '' +id: autocomplete +name: autocomplete +description: '' +read_only: false +field_settings: + field_synonyms: + label: Synonyms + datasource_id: 'entity:taxonomy_term' + property_path: field_synonyms + type: 'solr_text_custom:ngram' + dependencies: + config: + - field.storage.taxonomy_term.field_synonyms + langcode: + label: Language + datasource_id: 'entity:taxonomy_term' + property_path: langcode + type: string + dependencies: + module: + - taxonomy + name: + label: Name + datasource_id: 'entity:taxonomy_term' + property_path: name + type: 'solr_text_custom:ngram' + dependencies: + module: + - taxonomy + status: + label: Published + datasource_id: 'entity:taxonomy_term' + property_path: status + type: boolean + dependencies: + module: + - taxonomy + tid: + label: 'Term ID' + datasource_id: 'entity:taxonomy_term' + property_path: tid + type: integer + dependencies: + module: + - taxonomy +datasource_settings: + 'entity:taxonomy_term': + bundles: + default: false + selected: + - tags + - thesaurus + languages: + default: false + selected: + - en + - fr +processor_settings: + add_url: { } + aggregated_field: { } + entity_status: { } + rendered_item: { } + solr_date_range: + weights: + preprocess_index: 0 +tracker_settings: + default: + indexing_order: fifo +options: + index_directly: true + cron_limit: 50 +server: materio diff --git a/web/modules/custom/materio_sapi/materio_sapi.routing.yml b/web/modules/custom/materio_sapi/materio_sapi.routing.yml index b9275129..88e8831b 100644 --- a/web/modules/custom/materio_sapi/materio_sapi.routing.yml +++ b/web/modules/custom/materio_sapi/materio_sapi.routing.yml @@ -1,3 +1,10 @@ +materio_sapi.search_autocomplete: + path: '/materio_sapi/form/autocomplete' + defaults: + _controller: '\Drupal\materio_sapi\Controller\FormAutocomplete::Autocomplete' + _format: json + requirements: + _permission: 'access materio search' # # materio_sapi.materio_sapi_search_form: # path: '/materio_sapi/form/materio_sapi_search' diff --git a/web/modules/custom/materio_sapi/src/Controller/FormAutocomplete.php b/web/modules/custom/materio_sapi/src/Controller/FormAutocomplete.php new file mode 100644 index 00000000..ba07da26 --- /dev/null +++ b/web/modules/custom/materio_sapi/src/Controller/FormAutocomplete.php @@ -0,0 +1,88 @@ +query->get('q')) { + $typed_string = Tags::explode($input); + $typed_string = Unicode::strtolower(array_pop($typed_string)); + // \Drupal::logger('materio_sapi')->notice($typed_string); + + $index = Index::load('autocomplete'); + $query = $index->query(); + + // Change the parse mode for the search. + $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode') + ->createInstance('direct'); + $parse_mode->setConjunction('OR'); + $query->setParseMode($parse_mode); + + // Set fulltext search keywords and fields. + $query->keys($typed_string); + $query->setFulltextFields(['name']); + + // Set additional conditions. + // $query->addCondition('status', 1) + // ->addCondition('author', 1, '<>'); + + // Restrict the search to specific languages. + // $query->setLanguages(['de', 'it']); + + // Do paging. + $query->range(0, 10); + + // Add sorting. + $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() + $query->addTag('materio_sapi_autocomplete'); + + // Execute the search. + $results = $query->execute(); + + // echo "Result count: {$results->getResultCount()}\n"; + // $ids = implode(', ', array_keys($results->getResultItems())); + // echo "Returned IDs: $ids.\n"; + + // $items = $results->getResultItems(); + // \Drupal::logger('materio_sapi')->notice($results->getResultCount()); + // \Drupal::logger('materio_sapi')->notice(implode(', ', array_keys($items))); + + $response = []; + foreach ($results as $result) { + // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('tid')->getValues(),true)); + // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('name')->getValues(),true)); + $tid = $result->getField('tid')->getValues()[0]; + $term_name = $result->getField('name')->getValues()[0]->getText(); + $response[] = [ + 'value' => $tid, + 'label' => $term_name, + ]; + } + } + + return new JsonResponse($response); + } + +} diff --git a/web/modules/custom/materio_sapi/src/Form/MaterioSapiSearchForm.php b/web/modules/custom/materio_sapi/src/Form/MaterioSapiSearchForm.php index 2f657187..ef12e220 100644 --- a/web/modules/custom/materio_sapi/src/Form/MaterioSapiSearchForm.php +++ b/web/modules/custom/materio_sapi/src/Form/MaterioSapiSearchForm.php @@ -24,14 +24,18 @@ class MaterioSapiSearchForm extends FormBase { public function buildForm(array $form, FormStateInterface $form_state) { $form['search'] = [ '#type' => 'textfield', - '#title' => $this->t('Search'), + // '#title' => $this->t('Search'), '#maxlength' => 64, - '#size' => 64, + '#size' => 25, '#weight' => '0', + '#attributes' => [ + "placeholder" => $this->t('Search'), + ], + '#autocomplete_route_name' => 'materio_sapi.search_autocomplete', ]; $form['submit'] = [ '#type' => 'submit', - '#value' => $this->t('Submit'), + '#value' => $this->t('Search'), ]; return $form;