Skip to content

Commit

Permalink
Add autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
melaniekung committed Jul 4, 2024
1 parent 34c7126 commit 33f56fe
Showing 1 changed file with 72 additions and 4 deletions.
76 changes: 72 additions & 4 deletions plugins/arSolrPlugin/lib/arSolrPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class arSolrPlugin extends QubitSearchEngine
*/
protected $enabled = true;

public $lang_codes = ['ar','hy','ba','br','bg','ca','cz','da','nl','en','fi','fr','gl','ge','el','hi','hu','id','it','no','fa','pt','ro','ru','es','sv','tr'];

public $langs = [];

/**
* Constructor.
*/
Expand Down Expand Up @@ -317,6 +321,65 @@ protected function initialize()
$this->addFieldsToType(json_encode($addQuery));
$addQuery = ['add-field' => $subProperties];
$this->addFieldsToType(json_encode($addQuery));
$this->log(json_encode($this->langs));

$this->addAutoComplete();
}
}

private function addAutoComplete() {
// list should also include QubitInformationObject.referenceCode but
// since this field does not include a language code, it is added to
// $addCopyField manually
$autocompleteFields = [
"QubitRepository.i18n.en.authorizedFormOfName",
"QubitInformationObject.aip.type.i18n.en.name",
"QubitInformationObject.i18n.en.title",
"QubitActor.i18n.en.authorizedFormOfName",
"QubitActor.places.i18n.en.name",
"QubitActor.subjects.i18n.en.name",
"QubitTerm.i18n.en.name",
"QubitAip.type.i18n.en.name"
];

foreach ($this->langs as $lang) {
$url = $this->solrBaseUrl.'/api/collections/'.$this->solrClientOptions['collection'].'/config/';
$addSearchComponent = '{
"add-searchComponent":{
"name":"autocomplete_'.$lang.'",
"class":"solr.SuggestComponent",
"suggester":{
"name":"autocomplete_'.$lang.'",
"lookupImpl":"FuzzyLookupFactory",
"dictionaryImpl":"DocumentDictionaryFactory",
"suggestAnalyzerFieldType":"text_'.$lang.'"
}
}
}';
$addRequestHandler = '{
"add-requestHandler":{
"name":"/autocomplete_'.$lang.'",
"class":"solr.SearchHandler",
"components":["autocomplete_'.$lang.'"],
"defaults":{
"suggest":true,
"suggest.count":5,
"suggest.dictionary":"autocomplete_'.$lang.'"
}
}
}';
arSolrPlugin::makeHttpRequest($url, 'POST', $addSearchComponent);
arSolrPlugin::makeHttpRequest($url, 'POST', $addRequestHandler);

$addCopyField = '{"add-field":{"name":"autocomplete_'.$lang.'","type":"text_'.$lang.'","stored":true},';
$copyField = '"add-copy-field":{"source":"QubitInformationObject.referenceCode","dest":"autocomplete_'.$lang.'"},';
foreach ($autocompleteFields as $field) {
$field = str_replace('en', $lang, $field);
$copyField .= '"add-copy-field":{"source":"'.$field.'","dest":"autocomplete_'.$lang.'"},';
}
$addCopyField .= substr($copyField, 0, -1).'}';
$url = $this->solrBaseUrl.'/api/collections/'.$this->solrClientOptions['collection'].'/schema/';
arSolrPlugin::makeHttpRequest($url, 'POST', $addCopyField);
}
}

Expand All @@ -325,6 +388,13 @@ private function addSubProperties($properties, &$propertyFields, $parentType = '
$atomicTypes = ['keyword', 'string', 'text', 'text_general', 'date', 'pdate', 'pdates', 'long', 'plongs', 'integer', 'boolean', 'location'];
foreach ($properties as $propertyName => $value) {
$fieldName = $parentType ? "{$parentType}.{$propertyName}" : $propertyName;

$fields = explode('.', $fieldName);
$lang = $fields[count($fields) - 2];
if (in_array($lang, $this->lang_codes) && !in_array($lang, $this->langs)) {
array_push($this->langs, $lang);
}

if (in_array($value['type'], $atomicTypes)) {
if ('text' === $value['type']) {
$typeName = $this->setLanguageType($fieldName);
Expand All @@ -344,13 +414,11 @@ private function addSubProperties($properties, &$propertyFields, $parentType = '
}
}

private function setLanguageType($fieldName)
{
$lang_codes = ['ar', 'hy', 'ba', 'br', 'bg', 'ca', 'cz', 'da', 'nl', 'en', 'fi', 'fr', 'gl', 'ge', 'el', 'hi', 'hu', 'id', 'it', 'no', 'fa', 'pt', 'ro', 'ru', 'es', 'sv', 'tr'];
private function setLanguageType($fieldName) {
$substrings = explode('.', $fieldName);
$lang = $substrings[count($substrings) - 2];

if (in_array($lang, $lang_codes)) {
if (in_array($lang, $this->lang_codes)) {
return 'text_'.$lang;
}
if ('pt_BR' === $lang) {
Expand Down

0 comments on commit 33f56fe

Please sign in to comment.