1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\Elastica; |
3
|
|
|
|
4
|
|
|
class SearchableHelper { |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
public static function addIndexedFields($name, &$spec, $ownerClassName) { |
8
|
|
|
// in the case of a relationship type will not be set |
9
|
|
|
if(isset($spec['type'])) { |
10
|
|
|
if($spec['type'] == 'string') { |
11
|
|
|
$unstemmed = array(); |
12
|
|
|
$unstemmed['type'] = "string"; |
13
|
|
|
$unstemmed['analyzer'] = "unstemmed"; |
14
|
|
|
$unstemmed['term_vector'] = "yes"; |
15
|
|
|
$extraFields = array('standard' => $unstemmed); |
16
|
|
|
|
17
|
|
|
$shingles = array(); |
18
|
|
|
$shingles['type'] = "string"; |
19
|
|
|
$shingles['analyzer'] = "shingles"; |
20
|
|
|
$shingles['term_vector'] = "yes"; |
21
|
|
|
$extraFields['shingles'] = $shingles; |
22
|
|
|
|
23
|
|
|
//Add autocomplete field if so required |
24
|
|
|
$autocomplete = \Config::inst()->get($ownerClassName, 'searchable_autocomplete'); |
25
|
|
|
|
26
|
|
|
if(isset($autocomplete) && in_array($name, $autocomplete)) { |
27
|
|
|
$autocompleteField = array(); |
28
|
|
|
$autocompleteField['type'] = "string"; |
29
|
|
|
$autocompleteField['index_analyzer'] = "autocomplete_index_analyzer"; |
30
|
|
|
$autocompleteField['search_analyzer'] = "autocomplete_search_analyzer"; |
31
|
|
|
$autocompleteField['term_vector'] = "yes"; |
32
|
|
|
$extraFields['autocomplete'] = $autocompleteField; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$spec['fields'] = $extraFields; |
36
|
|
|
// FIXME - make index/locale specific, get from settings |
37
|
|
|
$spec['analyzer'] = 'stemmed'; |
38
|
|
|
$spec['term_vector'] = "yes"; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string &$name |
46
|
|
|
* @param boolean $storeMethodName |
47
|
|
|
* @param boolean $recurse |
48
|
|
|
*/ |
49
|
|
|
public static function assignSpecForRelationship(&$name, $resultType, &$spec, $storeMethodName, $recurse) { |
50
|
|
|
$resultTypeInstance = \Injector::inst()->create($resultType); |
51
|
|
|
$resultTypeMapping = array(); |
52
|
|
|
// get the fields for the result type, but do not recurse |
53
|
|
|
if($recurse) { |
54
|
|
|
$resultTypeMapping = $resultTypeInstance->getElasticaFields($storeMethodName, false); |
55
|
|
|
} |
56
|
|
|
$resultTypeMapping['ID'] = array('type' => 'integer'); |
57
|
|
|
if($storeMethodName) { |
58
|
|
|
$resultTypeMapping['__method'] = $name; |
59
|
|
|
} |
60
|
|
|
$spec = array('properties' => $resultTypeMapping); |
61
|
|
|
// we now change the name to the result type, not the method name |
62
|
|
|
$name = $resultType; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $name |
68
|
|
|
*/ |
69
|
|
|
public static function assignSpecForStandardFieldType($name, $class, &$spec, &$html_fields, &$mappings) { |
70
|
|
|
if(($pos = strpos($class, '('))) { |
71
|
|
|
// Valid in the case of Varchar(255) |
72
|
|
|
$class = substr($class, 0, $pos); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if(array_key_exists($class, $mappings)) { |
76
|
|
|
$spec['type'] = $mappings[$class]; |
77
|
|
|
if($spec['type'] === 'date') { |
78
|
|
|
$spec['format'] = SearchableHelper::getFormatForDate($class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if($class === 'HTMLText' || $class === 'HTMLVarchar') { |
82
|
|
|
array_push($html_fields, $name); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public static function getFormatForDate($class) { |
89
|
|
|
$format = 'y-M-d'; // default |
90
|
|
|
switch ($class) { |
91
|
|
|
case 'Date': |
92
|
|
|
$format = 'y-M-d'; |
93
|
|
|
break; |
94
|
|
|
case 'SS_Datetime': |
95
|
|
|
$format = 'y-M-d H:m:s'; |
96
|
|
|
break; |
97
|
|
|
case 'Datetime': |
98
|
|
|
$format = 'y-M-d H:m:s'; |
99
|
|
|
break; |
100
|
|
|
case 'Time': |
101
|
|
|
$format = 'H:m:s'; |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $format; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|