public function testOptionalAttributes() { $this->productMock->shouldReceive('getAttribute')->with('optional_attributes')->andReturn(['name' => 'value', 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault(); $expected = ['name' => ['boost' => 1, 'value' => 'value'], 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']]; $this->assertEquals($expected, $this->config->optionalAttributes($this->productMock)); $this->productMock->shouldReceive('getAttribute')->with('optional_attributes')->andReturn([0 => 'value', 1 => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault(); $expected = ['optional_attributes_0' => ['boost' => 1, 'value' => 'value'], 'optional_attributes_1' => ['boost' => 0.1, 'value' => 'boosted_value']]; $this->assertEquals($expected, $this->config->optionalAttributes($this->productMock)); $this->dummyMock->shouldReceive('getAttribute')->with('custom_optional_attributes')->andReturn(['name' => 'value', 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault(); $expected = ['name' => ['boost' => 1, 'value' => 'value'], 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']]; $this->assertEquals($expected, $this->config->optionalAttributes($this->dummyMock)); $this->dummyMock->shouldReceive('getAttribute')->with('custom_optional_attributes')->andReturn([0 => 'value', 1 => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault(); $expected = ['custom_optional_attributes_0' => ['boost' => 1, 'value' => 'value'], 'custom_optional_attributes_1' => ['boost' => 0.1, 'value' => 'boosted_value']]; $this->assertEquals($expected, $this->config->optionalAttributes($this->dummyMock)); }
/** * Update document in index for model * * @param Model $model */ public function update(Model $model) { // Remove any existing documents for model. $this->delete($model); // Create new document for model. $doc = new Document(); list($name, $value) = $this->config->privateKeyPair($model); // Add private key. $doc->addField(Field::keyword($name, $value)); // Add model's class UID. list($name, $value) = $this->config->classUidPair($model); // Add class uid for identification of model's class. $doc->addField(Field::Keyword($name, $value)); $fields = $this->config->fields($model); // Add fields to document to be indexed (but not stored). foreach ($fields as $field) { $doc->addField(Field::unStored(trim($field), strip_tags(trim($model->{trim($field)})))); } $optionalAttributes = $this->config->optionalAttributes($model); // Add optional attributes to document to be indexed (but not stored). foreach ($optionalAttributes as $fieldName => $fieldValue) { $doc->addField(Field::unStored(trim($fieldName), strip_tags(trim($fieldValue)))); } // Add document to index. $this->index()->addDocument($doc); }
/** * Update document in index for model * * @param Model $model */ public function update(Model $model) { // Remove any existing documents for model. $this->delete($model); // Create new document for model. $doc = new Document(); list($name, $value) = $this->config->primaryKeyPair($model); // Add private key. $doc->addField(Field::keyword($name, $value)); // Add model's class UID. list($name, $value) = $this->config->classUidPair($model); // Add class uid for identification of model's class. $doc->addField(Field::Keyword($name, $value)); // Get base fields. $fields = $this->config->fields($model); // Add fields to document to be indexed (but not stored). foreach ($fields as $fieldName => $options) { $fieldValue = $model->{trim($fieldName)}; $field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue))); $field->boost = array_get($options, 'boost'); $doc->addField($field); } // Get dynamic fields. $optionalAttributes = $this->config->optionalAttributes($model); // Add optional attributes to document to be indexed (but not stored). foreach ($optionalAttributes as $fieldName => $options) { $fieldValue = array_get($options, "value"); $field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue))); $field->boost = array_get($options, "boost"); $doc->addField($field); } // Set boost for model. $doc->boost = $this->config->boost($model); // Add document to index. $this->index()->addDocument($doc); }