/** * Get field type information for sort clause * * TODO: handle custom field * TODO: caching (see above) * * @param string $contentTypeIdentifier * @param string $fieldDefinitionIdentifier * @param string $languageCode * * @return array */ public function getSortFieldTypes($contentTypeIdentifier, $fieldDefinitionIdentifier, $languageCode) { $types = array(); foreach ($this->contentTypeHandler->loadAllGroups() as $group) { foreach ($this->contentTypeHandler->loadContentTypes($group->id) as $contentType) { if ($contentType->identifier !== $contentTypeIdentifier) { continue; } foreach ($contentType->fieldDefinitions as $fieldDefinition) { if ($fieldDefinition->identifier !== $fieldDefinitionIdentifier) { continue; } // TODO: find a better way to handle non-translatable fields? if ($languageCode === null || $fieldDefinition->isTranslatable) { $fieldType = $this->fieldRegistry->getType($fieldDefinition->fieldType); foreach ($fieldType->getIndexDefinition() as $name => $type) { $types[$type->type] = $this->nameGenerator->getTypedName($this->nameGenerator->getName($name, $fieldDefinition->identifier, $contentType->identifier), $type); } } break 3; } } } return $types; }
/** * Returns an array of documents containing fields for the given $content. * Each document contains fields for a single language. * * @param \eZ\Publish\SPI\Persistence\Content $content * @param \eZ\Publish\SPI\Persistence\Content\Type $contentType * * @return \eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\Document[] */ protected function mapFields(Content $content, Type $contentType) { $fieldMap = array(); foreach ($content->fields as $field) { $fieldMap[$field->languageCode][] = $field; } $fieldDocuments = array(); foreach (array_keys($fieldMap) as $languageCode) { $fields = array(); $fields[] = new Field('meta_language_code', $languageCode, new FieldType\StringField()); $fields[] = new Field('meta_is_main_translation', $content->versionInfo->contentInfo->mainLanguageCode === $languageCode, new FieldType\BooleanField()); $fields[] = new Field('meta_is_always_available', $content->versionInfo->contentInfo->mainLanguageCode === $languageCode && $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField()); foreach ($fieldMap[$languageCode] as $field) { foreach ($contentType->fieldDefinitions as $fieldDefinition) { if ($fieldDefinition->id !== $field->fieldDefinitionId) { continue; } $fieldType = $this->fieldRegistry->getType($field->type); foreach ($fieldType->getIndexData($field) as $indexField) { $fields[] = new Field($name = $this->fieldNameGenerator->getName($indexField->name, $fieldDefinition->identifier, $contentType->identifier), $indexField->value, $indexField->type); if ($indexField->type instanceof FieldType\StringField || $indexField->type instanceof FieldType\MultipleStringField) { $fields[] = new Field($name . "_meta_all_" . str_replace("-", "_", $languageCode), $indexField->value, $indexField->type); } } } } $fieldDocuments[] = new Document(array("fields" => $fields)); } return $fieldDocuments; }
/** * Converts given $document to a hash format that can be JSON encoded * to get a document _source. * * Implemented in a separate method because of a recursion needed to * handle nested documents. * * @param \eZ\Publish\Core\Persistence\Elasticsearch\Content\Search\Document $document * * @return array */ protected function getDocumentHash(Document $document) { $hash = array(); foreach ($document->fields as $field) { if ($field->type instanceof DocumentField) { $documents = $this->fieldValueMapper->map($field); $values = array(); foreach ($documents as $document) { $values[] = $this->getDocumentHash($document); } } else { $values = (array) $this->fieldValueMapper->map($field); } $name = $this->nameGenerator->getTypedName($field->name, $field->type); if (count($values) === 1) { $hash[$name] = reset($values); } else { $hash[$name] = $values; } } return $hash; }