/** * Create a mapping field from an attribute. * * @param AttributeInterface $attribute Entity attribute. * * @return \Smile\ElasticsuiteCatalog\Model\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData */ private function initField(AttributeInterface $attribute) { $fieldName = $attribute->getAttributeCode(); $fieldConfig = $this->attributeHelper->getMappingFieldOptions($attribute); if ($attribute->usesSource()) { $optionFieldName = $this->attributeHelper->getOptionTextFieldName($fieldName); $fieldType = 'string'; $fieldOptions = ['name' => $optionFieldName, 'type' => $fieldType, 'fieldConfig' => $fieldConfig]; $this->fields[$optionFieldName] = $this->fieldFactory->create($fieldOptions); // Reset parent field values : only the option text field should be used for spellcheck and autocomplete. $fieldConfig['is_used_in_spellcheck'] = false; $fieldConfig['is_used_in_autocomplete'] = false; $fieldConfig['is_searchable'] = false; } $fieldType = $this->attributeHelper->getFieldType($attribute); $fieldOptions = ['name' => $fieldName, 'type' => $fieldType, 'fieldConfig' => $fieldConfig]; $this->fields[$fieldName] = $this->fieldFactory->create($fieldOptions); return $this; }
/** * Parse attribute raw value (as saved in the database) to prepare the indexed value. * For attribute using options the option value is also added to the result which contains two keys : * - one is "attribute_code" and contained the option id(s) * - the other one is "option_text_attribute_code" and contained option value(s) * All value are transformed into arays to have a more simple management of * multivalued attributes merging on composite products). * ES doesn't care of having array of int when it an int is required. * * @param AttributeInterface $attribute Product attribute. * @param integer $storeId Store id. * @param mixed $value Raw value to be parsed. * * @return array */ public function prepareIndexValue(AttributeInterface $attribute, $storeId, $value) { $attributeCode = $attribute->getAttributeCode(); $values = []; $mapperKey = 'simple_' . $attribute->getId(); if (!isset($this->attributeMappers[$mapperKey])) { $this->attributeMappers[$mapperKey] = function ($value) use($attribute) { return $this->prepareSimpleIndexAttributeValue($attribute, $value); }; } if ($attribute->usesSource() && !is_array($value)) { $value = explode(',', $value); } if (!is_array($value)) { $value = [$value]; } $value = array_map($this->attributeMappers[$mapperKey], $value); $value = array_filter($value); $value = array_values($value); $values[$attributeCode] = $value; if ($attribute->usesSource()) { $optionTextFieldName = $this->getOptionTextFieldName($attributeCode); $optionTextValues = $this->getIndexOptionsText($attribute, $storeId, $value); $optionTextValues = array_filter($optionTextValues); $optionTextValues = array_values($optionTextValues); $values[$optionTextFieldName] = $optionTextValues; } return array_filter($values); }