Skip to content

Commit

Permalink
Replace deprecated string fields with text and keywords.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien FOUCRET committed Feb 7, 2018
1 parent 6450515 commit f751ddb
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 109 deletions.
4 changes: 2 additions & 2 deletions src/module-elasticsuite-catalog/Helper/AbstractAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function getMappingFieldOptions(AttributeInterface $attribute)
*/
public function getFieldType(AttributeInterface $attribute)
{
$type = FieldInterface::FIELD_TYPE_STRING;
$type = FieldInterface::FIELD_TYPE_TEXT;

if ($attribute->getSourceModel() == 'Magento\Eav\Model\Entity\Attribute\Source\Boolean') {
$type = FieldInterface::FIELD_TYPE_BOOLEAN;
Expand All @@ -124,7 +124,7 @@ public function getFieldType(AttributeInterface $attribute)
} elseif ($attribute->getBackendType() == 'datetime') {
$type = FieldInterface::FIELD_TYPE_DATE;
} elseif ($attribute->usesSource() && $attribute->getSourceModel() === null) {
$type = FieldInterface::FIELD_TYPE_INTEGER;
$type = $attribute->getSourceModel() ? FieldInterface::FIELD_TYPE_INTEGER : FieldInterface::FIELD_TYPE_KEYWORD;
}

return $type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData as ResourceModel;
use Smile\ElasticsuiteCore\Index\Mapping\FieldFactory;
use Smile\ElasticsuiteCatalog\Helper\AbstractAttribute as AttributeHelper;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;

/**
* Abstract Datasource for EAV entities
Expand Down Expand Up @@ -178,7 +179,7 @@ private function initField(AttributeInterface $attribute)

if ($attribute->usesSource()) {
$optionFieldName = $this->attributeHelper->getOptionTextFieldName($fieldName);
$fieldType = 'string';
$fieldType = FieldInterface::FIELD_TYPE_TEXT;
$fieldOptions = ['name' => $optionFieldName, 'type' => $fieldType, 'fieldConfig' => $fieldConfig];
$this->fields[$optionFieldName] = $this->fieldFactory->create($fieldOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Boolean extends Attribute
*/
public function apply(\Magento\Framework\App\RequestInterface $request)
{
$attributeValue = $request->getParam($this->_requestVar);
$attributeValue = (bool) $request->getParam($this->_requestVar);

if (!empty($attributeValue)) {
if (!is_array($attributeValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function attributeTypeProvider()
['varchar', false, null, 'validate-number', FieldInterface::FIELD_TYPE_DOUBLE],
['datetime', false, null, null, FieldInterface::FIELD_TYPE_DATE],
['varchar', true, null, null, FieldInterface::FIELD_TYPE_INTEGER],
['varchar', false, null, null, FieldInterface::FIELD_TYPE_STRING],
['varchar', false, null, null, FieldInterface::FIELD_TYPE_TEXT],
['varchar', true, null, null, FieldInterface::FIELD_TYPE_INTEGER],
['varchar', true, 'sourceModel', null, FieldInterface::FIELD_TYPE_KEYWORD],
];
}
}
12 changes: 6 additions & 6 deletions src/module-elasticsuite-catalog/etc/elasticsuite_indices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
<field name="required_options" type="boolean" />
<field name="created_at" type="date" />
<field name="updated_at" type="date" />
<field name="type_id" type="string" />
<field name="sku" type="string">
<field name="type_id" type="keyword" />
<field name="sku" type="text">
<isSearchable>1</isSearchable>
<isUsedInSpellcheck>1</isUsedInSpellcheck>
<defaultSearchAnalyzer>whitespace</defaultSearchAnalyzer>
</field>
<field name="visibility" type="integer" />
<field name="children_ids" type="integer" />
<field name="configurable_attributes" type="string" />
<field name="indexed_attributes" type="string" />
<field name="configurable_attributes" type="keyword" />
<field name="indexed_attributes" type="keyword" />

<!-- Static fields handled by the "prices" datasource -->
<field name="price.price" type="double" nestedPath="price" />
Expand All @@ -61,7 +61,7 @@
<field name="category.category_id" type="integer" nestedPath="category" />
<field name="category.position" type="integer" nestedPath="category" />
<field name="category.is_parent" type="boolean" nestedPath="category" />
<field name="category.name" type="string" nestedPath="category">
<field name="category.name" type="text" nestedPath="category">
<isSearchable>1</isSearchable>
<isUsedInSpellcheck>1</isUsedInSpellcheck>
<isFilterable>0</isFilterable>
Expand All @@ -87,7 +87,7 @@
<field name="parent_id" type="integer" />
<field name="created_at" type="date" />
<field name="updated_at" type="date" />
<field name="path" type="string" />
<field name="path" type="keyword" />
<field name="position" type="integer" />
<field name="level" type="integer" />
<field name="children_count" type="integer" />
Expand Down
18 changes: 2 additions & 16 deletions src/module-elasticsuite-core/Api/Index/Mapping/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,17 @@ interface FieldInterface
{
/**
* Field types declaration.
*
*/
const FIELD_TYPE_TEXT = 'text';
const FIELD_TYPE_KEYWORD = 'keyword';
const FIELD_TYPE_DOUBLE = 'double';
const FIELD_TYPE_INTEGER = 'integer';
const FIELD_TYPE_LONG = 'long';
const FIELD_TYPE_DATE = 'date';
const FIELD_TYPE_BOOLEAN = 'boolean';
const FIELD_TYPE_NESTED = 'nested';

const FIELD_TYPE_OBJECT = 'object';

/**
* Deprecated multi_field type.
*
* @deprecated
*/
const FIELD_TYPE_MULTI = 'multi_field';

/**
* Deprecated string type.
*
* @deprecated
*/
const FIELD_TYPE_STRING = 'string';

/**
* Analyzers declarations.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/module-elasticsuite-core/Index/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function getProperties()
$properties = [];

foreach ($this->defaultMappingFields as $fieldName => $analyzers) {
$properties = $this->addProperty($properties, $fieldName, FieldInterface::FIELD_TYPE_STRING, $analyzers);
$properties = $this->addProperty($properties, $fieldName, FieldInterface::FIELD_TYPE_TEXT, $analyzers);
}

foreach ($this->getFields() as $currentField) {
Expand Down Expand Up @@ -262,7 +262,7 @@ private function getDynamicFields(array $dynamicFieldProviders)
*/
private function addProperty(array $properties, $propertyName, $propertyType, $analyzers = [])
{
$property = ['type' => FieldInterface::FIELD_TYPE_STRING, 'analyzer' => FieldInterface::ANALYZER_STANDARD];
$property = ['type' => $propertyType, 'analyzer' => FieldInterface::ANALYZER_STANDARD];

foreach ($analyzers as $analyzer) {
if ($analyzer !== FieldInterface::ANALYZER_STANDARD) {
Expand Down
16 changes: 8 additions & 8 deletions src/module-elasticsuite-core/Index/Mapping/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Field implements FieldInterface
* @param array $fieldConfig Field configuration (see self::$config declaration for
* available values and default values).
*/
public function __construct($name, $type = 'string', $nestedPath = null, $fieldConfig = [])
public function __construct($name, $type = self::FIELD_TYPE_KEYWORD, $nestedPath = null, $fieldConfig = [])
{
$this->name = (string) $name;
$this->type = (string) $type;
Expand Down Expand Up @@ -179,7 +179,7 @@ public function getMappingPropertyConfig()
{
$property = $this->getPropertyConfig();

if ($this->getType() == self::FIELD_TYPE_STRING) {
if ($this->getType() == self::FIELD_TYPE_TEXT) {
$analyzers = $this->getFieldAnalyzers();
$property = $this->getPropertyConfig(current($analyzers));

Expand Down Expand Up @@ -237,12 +237,12 @@ private function checkAnalyzer($property, $expectedAnalyzer)
{
$isAnalyzerCorrect = true;

if ($property['type'] == self::FIELD_TYPE_STRING) {
if ($property['type'] == self::FIELD_TYPE_TEXT || $property['type'] == self::FIELD_TYPE_KEYWORD) {
$isAnalyzed = $expectedAnalyzer !== self::ANALYZER_UNTOUCHED;

if ($isAnalyzed && (!isset($property['analyzer']) || $property['analyzer'] != $expectedAnalyzer)) {
$isAnalyzerCorrect = false;
} elseif (!$isAnalyzed && (!isset($property['index']) || $property['index'] != 'not_analyzed')) {
} elseif (!$isAnalyzed && $property['type'] !== self::FIELD_TYPE_KEYWORD) {
$isAnalyzerCorrect = false;
}
}
Expand Down Expand Up @@ -319,11 +319,11 @@ private function getFieldAnalyzers()
*/
private function getPropertyConfig($analyzer = self::ANALYZER_UNTOUCHED)
{
$fieldMapping = ['type' => $this->getType(), 'doc_values' => true];
$fieldMapping = ['type' => $this->getType()];

if ($this->getType() == self::FIELD_TYPE_STRING && $analyzer == self::ANALYZER_UNTOUCHED) {
$fieldMapping['index'] = 'not_analyzed';
} elseif ($this->getType() == self::FIELD_TYPE_STRING) {
if ($this->getType() == self::FIELD_TYPE_TEXT && $analyzer == self::ANALYZER_UNTOUCHED) {
$fieldMapping['type'] = self::FIELD_TYPE_KEYWORD;
} elseif ($this->getType() == self::FIELD_TYPE_TEXT) {
$fieldMapping['analyzer'] = $analyzer;
$fieldMapping['doc_values'] = false;
$fieldMapping['index_options'] = 'docs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function buildSortOrder(SortOrderInterface $sortOrder)

if ($sortField !== SortOrderInterface::DEFAULT_SORT_FIELD) {
$sortOrderConfig['missing'] = $sortOrder->getDirection() == SortOrderInterface::SORT_ASC ? '_last' : '_first';
$sortOrderConfig['unmapped_type'] = FieldInterface::FIELD_TYPE_STRING;
$sortOrderConfig['unmapped_type'] = FieldInterface::FIELD_TYPE_KEYWORD;
}

if ($sortOrder->getType() == SortOrderInterface::TYPE_NESTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class SearchableFieldFilter implements FieldFilterInterface
*/
public function filterField(FieldInterface $field)
{
return $field->getType() == FieldInterface::FIELD_TYPE_STRING && $field->isSearchable() && $field->isNested() === false;
return $field->getType() == FieldInterface::FIELD_TYPE_TEXT && $field->isSearchable() && $field->isNested() === false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testDefaultValues()
$field = new Field('fieldName');

$this->assertEquals('fieldName', $field->getName());
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $field->getType());
$this->assertEquals(FieldInterface::FIELD_TYPE_KEYWORD, $field->getType());
$this->assertEquals(false, $field->isSearchable());
$this->assertEquals(1, $field->getSearchWeight());
$this->assertEquals(true, $field->isFilterable());
Expand All @@ -47,7 +47,7 @@ public function testDefaultValues()
$this->assertEquals(null, $field->getNestedPath());

$mappingPropertyConfig = $field->getMappingPropertyConfig();
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $mappingPropertyConfig['type']);
$this->assertEquals(FieldInterface::FIELD_TYPE_KEYWORD, $mappingPropertyConfig['type']);
$this->assertEquals('fieldName', $field->getMappingProperty());
$this->assertEquals(null, $field->getMappingProperty(FieldInterface::ANALYZER_STANDARD));
}
Expand All @@ -59,7 +59,7 @@ public function testDefaultValues()
*/
public function testNestedField()
{
$field = new Field('parent.child', FieldInterface::FIELD_TYPE_STRING, 'parent', ['is_searchable' => true]);
$field = new Field('parent.child', FieldInterface::FIELD_TYPE_TEXT, 'parent', ['is_searchable' => true]);

$this->assertEquals(true, $field->isNested());
$this->assertEquals('parent', $field->getNestedPath());
Expand All @@ -80,7 +80,7 @@ public function testNestedField()
*/
public function testInvalidNestedField()
{
new Field('parent.child', FieldInterface::FIELD_TYPE_STRING, 'invalidparent');
new Field('parent.child', FieldInterface::FIELD_TYPE_TEXT, 'invalidparent');
}

/**
Expand All @@ -104,7 +104,6 @@ public function testBasicTypes()

$mappingPropertyConfig = $field->getMappingPropertyConfig();
$this->assertEquals($type, $mappingPropertyConfig['type']);
$this->assertEquals(true, $mappingPropertyConfig['doc_values']);

if ($type === FieldInterface::FIELD_TYPE_DATE) {
$this->assertEquals('yyyy-MM-dd HH:mm:ss||yyyy-MM-dd', $mappingPropertyConfig['format']);
Expand All @@ -122,11 +121,11 @@ public function testBasicTypes()
public function testComplexSearchableStringField()
{
$fieldConfig = ['is_searchable' => true, 'is_used_for_sort_by' => true, 'search_weight' => 2];
$fieldType = FieldInterface::FIELD_TYPE_STRING;
$fieldType = FieldInterface::FIELD_TYPE_TEXT;
$field = new Field('field', $fieldType, null, $fieldConfig);

$mappingPropertyConfig = $field->getMappingPropertyConfig();
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $mappingPropertyConfig['type']);
$this->assertEquals(FieldInterface::FIELD_TYPE_TEXT, $mappingPropertyConfig['type']);

$this->assertEquals(FieldInterface::ANALYZER_STANDARD, $mappingPropertyConfig['analyzer']);
$this->assertEquals(FieldInterface::ANALYZER_WHITESPACE, $mappingPropertyConfig['fields']['whitespace']['analyzer']);
Expand All @@ -148,11 +147,11 @@ public function testComplexSearchableStringField()
public function testSimpleSearchableStringField()
{
$fieldConfig = ['is_searchable' => true, 'is_filterable' => false, 'search_weight' => 1];
$fieldType = FieldInterface::FIELD_TYPE_STRING;
$fieldType = FieldInterface::FIELD_TYPE_TEXT;
$field = new Field('field', $fieldType, null, $fieldConfig);

$mappingPropertyConfig = $field->getMappingPropertyConfig();
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $mappingPropertyConfig['type']);
$this->assertEquals(FieldInterface::FIELD_TYPE_TEXT, $mappingPropertyConfig['type']);

$this->assertEquals(null, $field->getMappingProperty());
$this->assertEquals('field', $field->getMappingProperty(FieldInterface::ANALYZER_STANDARD));
Expand Down
30 changes: 15 additions & 15 deletions src/module-elasticsuite-core/Test/Unit/Index/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ protected function setUp()
// Static fields.
$fields = [
new Field('entity_id', FieldInterface::FIELD_TYPE_INTEGER),
new Field('nested.child1', FieldInterface::FIELD_TYPE_STRING, 'nested'),
new Field('nested.child2', FieldInterface::FIELD_TYPE_STRING, 'nested'),
new Field('object.child1', FieldInterface::FIELD_TYPE_STRING),
new Field('object.child2', FieldInterface::FIELD_TYPE_STRING),
new Field('nested.child1', FieldInterface::FIELD_TYPE_TEXT, 'nested'),
new Field('nested.child2', FieldInterface::FIELD_TYPE_TEXT, 'nested'),
new Field('object.child1', FieldInterface::FIELD_TYPE_TEXT),
new Field('object.child2', FieldInterface::FIELD_TYPE_TEXT),
];

// Stubing a dynanyc data provider.
$dynamicDataProvider = $this->getMockBuilder(DynamicFieldProviderInterface::class)->getMock();
$dynamicDataProviderFields = [
new Field('title', FieldInterface::FIELD_TYPE_STRING, null, ['is_searchable' => true]),
new Field('title', FieldInterface::FIELD_TYPE_TEXT, null, ['is_searchable' => true]),
];
$dynamicDataProvider->method('getFields')
->will($this->returnValue($dynamicDataProviderFields));
Expand All @@ -74,7 +74,7 @@ public function testDefaultSearchProperty()
$properties = $this->mapping->getProperties();

$this->assertArrayHasKey(Mapping::DEFAULT_SEARCH_FIELD, $properties);
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $properties[Mapping::DEFAULT_SEARCH_FIELD]['type']);
$this->assertEquals(FieldInterface::FIELD_TYPE_TEXT, $properties[Mapping::DEFAULT_SEARCH_FIELD]['type']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_WHITESPACE, $properties[Mapping::DEFAULT_SEARCH_FIELD]['fields']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_SHINGLE, $properties[Mapping::DEFAULT_SEARCH_FIELD]['fields']);
}
Expand All @@ -89,7 +89,7 @@ public function testDefaultAutocompleteProperty()
$properties = $this->mapping->getProperties();

$this->assertArrayHasKey(Mapping::DEFAULT_AUTOCOMPLETE_FIELD, $properties);
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $properties[Mapping::DEFAULT_AUTOCOMPLETE_FIELD]['type']);
$this->assertEquals(FieldInterface::FIELD_TYPE_TEXT, $properties[Mapping::DEFAULT_AUTOCOMPLETE_FIELD]['type']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_WHITESPACE, $properties[Mapping::DEFAULT_AUTOCOMPLETE_FIELD]['fields']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_SHINGLE, $properties[Mapping::DEFAULT_AUTOCOMPLETE_FIELD]['fields']);
}
Expand All @@ -104,7 +104,7 @@ public function testDefaultSpellingProperty()
$properties = $this->mapping->getProperties();

$this->assertArrayHasKey(Mapping::DEFAULT_SPELLING_FIELD, $properties);
$this->assertEquals(FieldInterface::FIELD_TYPE_STRING, $properties[Mapping::DEFAULT_SPELLING_FIELD]['type']);
$this->assertEquals(FieldInterface::FIELD_TYPE_TEXT, $properties[Mapping::DEFAULT_SPELLING_FIELD]['type']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_WHITESPACE, $properties[Mapping::DEFAULT_SPELLING_FIELD]['fields']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_SHINGLE, $properties[Mapping::DEFAULT_SPELLING_FIELD]['fields']);
$this->assertArrayHasKey(FieldInterface::ANALYZER_PHONETIC, $properties[Mapping::DEFAULT_SPELLING_FIELD]['fields']);
Expand Down Expand Up @@ -278,40 +278,40 @@ private function getSearchWeightedMapping()
),
new Field(
'ignoredField',
FieldInterface::FIELD_TYPE_STRING
FieldInterface::FIELD_TYPE_TEXT
),
new Field(
'standardField',
FieldInterface::FIELD_TYPE_STRING,
FieldInterface::FIELD_TYPE_TEXT,
null,
['is_searchable' => true]
),
new Field(
'weightedField',
FieldInterface::FIELD_TYPE_STRING,
FieldInterface::FIELD_TYPE_TEXT,
null,
['is_searchable' => true, 'search_weight' => 2]
),
new Field(
'whitespaceField',
FieldInterface::FIELD_TYPE_STRING,
FieldInterface::FIELD_TYPE_TEXT,
null,
['is_searchable' => true, 'search_weight' => 1, 'default_search_analyzer' => Field::ANALYZER_WHITESPACE]
),
new Field(
'whitespaceWeightedField',
FieldInterface::FIELD_TYPE_STRING,
FieldInterface::FIELD_TYPE_TEXT,
null,
['is_searchable' => true, 'search_weight' => 2, 'default_search_analyzer' => Field::ANALYZER_WHITESPACE]
),
new Field(
'nested.subfield',
FieldInterface::FIELD_TYPE_STRING,
FieldInterface::FIELD_TYPE_TEXT,
'nested'
),
new Field(
'object.subfield',
FieldInterface::FIELD_TYPE_STRING
FieldInterface::FIELD_TYPE_TEXT
),
];

Expand Down
Loading

0 comments on commit f751ddb

Please sign in to comment.