-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathContentDocumentFulltextFields.php
More file actions
142 lines (122 loc) · 4.52 KB
/
ContentDocumentFulltextFields.php
File metadata and controls
142 lines (122 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* This file is part of the eZ Platform Solr Search Engine package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentTranslationFieldMapper;
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\BoostFactorProvider;
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentTranslationFieldMapper;
use eZ\Publish\Core\Search\Common\FieldNameGenerator;
use eZ\Publish\Core\Search\Common\FieldRegistry;
use eZ\Publish\SPI\Persistence\Content;
use eZ\Publish\SPI\Persistence\Content\Type as ContentType;
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
use eZ\Publish\SPI\Search\Field;
use eZ\Publish\SPI\Search\FieldType;
/**
* Maps Content fulltext fields to Content document.
*/
class ContentDocumentFulltextFields extends ContentTranslationFieldMapper
{
/**
* Field name, untyped.
*
* @var string
*/
private static $fieldName = 'meta_content__text';
/**
* @var \eZ\Publish\SPI\Persistence\Content\Type\Handler
*/
protected $contentTypeHandler;
/**
* @var \eZ\Publish\Core\Search\Common\FieldRegistry
*/
protected $fieldRegistry;
/**
* @var \eZ\Publish\Core\Search\Common\FieldNameGenerator
*/
protected $fieldNameGenerator;
/**
* @var \EzSystems\EzPlatformSolrSearchEngine\FieldMapper\BoostFactorProvider
*/
protected $boostFactorProvider;
/**
* @var string
*/
private $invalidCharactersPattern;
/**
* @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler
* @param \eZ\Publish\Core\Search\Common\FieldRegistry $fieldRegistry
* @param \eZ\Publish\Core\Search\Common\FieldNameGenerator $fieldNameGenerator
* @param \EzSystems\EzPlatformSolrSearchEngine\FieldMapper\BoostFactorProvider $boostFactorProvider
* @param $invalidCharactersPattern
*/
public function __construct(
ContentTypeHandler $contentTypeHandler,
FieldRegistry $fieldRegistry,
FieldNameGenerator $fieldNameGenerator,
BoostFactorProvider $boostFactorProvider,
$invalidCharactersPattern
) {
$this->contentTypeHandler = $contentTypeHandler;
$this->fieldRegistry = $fieldRegistry;
$this->fieldNameGenerator = $fieldNameGenerator;
$this->boostFactorProvider = $boostFactorProvider;
$this->invalidCharactersPattern = $invalidCharactersPattern;
}
public function accept(Content $content, $languageCode)
{
return true;
}
public function mapFields(Content $content, $languageCode)
{
$fields = [];
$contentType = $this->contentTypeHandler->load(
$content->versionInfo->contentInfo->contentTypeId
);
foreach ($content->fields as $field) {
if ($field->languageCode !== $languageCode) {
continue;
}
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
if ($fieldDefinition->id !== $field->fieldDefinitionId) {
continue;
}
$fieldType = $this->fieldRegistry->getType($field->type);
$indexFields = $fieldType->getIndexData($field, $fieldDefinition);
foreach ($indexFields as $indexField) {
if ($indexField->value === null) {
continue;
}
if (!$indexField->type instanceof FieldType\FullTextField || !$fieldDefinition->isSearchable) {
continue;
}
$fields[] = new Field(
self::$fieldName,
preg_replace($this->invalidCharactersPattern, '', $indexField->value),
$this->getIndexFieldType($contentType)
);
}
}
}
return $fields;
}
/**
* Return index field type for the given $contentType.
*
* @param \eZ\Publish\SPI\Persistence\Content\Type $contentType
*
* @return \eZ\Publish\SPI\Search\FieldType
*/
private function getIndexFieldType(ContentType $contentType)
{
$newFieldType = new FieldType\TextField();
$newFieldType->boost = $this->boostFactorProvider->getContentMetaFieldBoostFactor(
$contentType,
'text'
);
return $newFieldType;
}
}