fields[$name] = $field; $this->current = $name; return $this; } public function export() { return ['properties' => $this->exportProperties()]; } public function exportProperties() { $properties = array(); foreach ($this->fields as $name => $field) { $properties[$name] = $field; if ($field['type'] === self::TYPE_OBJECT) { $properties[$name]['properties'] = $field['properties']->exportProperties(); } } return $properties; } public function analyzer($analyzer, $type = null) { $field = &$this->currentField(); if ($field['type'] !== self::TYPE_STRING) { throw new LogicException('Only string fields can be analyzed'); } switch ($type) { case null: $field['analyzer'] = $analyzer; unset($field['index_analyzer'], $field['search_analyzer']); break; case 'indexing': $field['index_analyzer'] = $analyzer; break; case 'searching': $field['search_analyzer'] = $analyzer; break; default: throw new LogicException(sprintf('Invalid analyzer type "%s".', $type)); } $field['index'] = 'analyzed'; return $this; } public function notAnalyzed() { $field = &$this->currentField(); if ($field['type'] !== self::TYPE_STRING) { throw new LogicException('Only string fields can be not analyzed'); } $field['index'] = 'not_analyzed'; return $this; } public function notIndexed() { $field = &$this->currentField(); $field['index'] = 'no'; return $this; } public function addRawVersion() { $field = &$this->currentField(); $field['fields']['raw'] = [ 'type' => $field['type'], 'index' => 'not_analyzed' ]; return $this; } public function addAnalyzedVersion(array $langs) { $field = &$this->currentField(); foreach ($langs as $lang) { $field['fields'][$lang] = [ 'type' => $field['type'], 'analyzer' => sprintf('%s_full', $lang) ]; } $field['fields']['light'] = [ 'type' => $field['type'], 'analyzer' => 'general_light' ]; return $this; } public function format($format) { $field = &$this->currentField(); if ($field['type'] !== self::TYPE_DATE) { throw new LogicException('Only date fields can have a format'); } $field['format'] = $format; return $this; } public function has($name) { return isset($this->fields[$name]); } protected function ¤tField() { if (null === $this->current) { throw new LogicException('You must add a field first'); } return $this->fields[$this->current]; } }