|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Lucene\Store; |
|
4
|
|
|
|
|
5
|
|
|
use BestServedCold\LaravelZendSearch\Lucene\Index; |
|
6
|
|
|
use ZendSearch\Lucene\Document; |
|
7
|
|
|
use ZendSearch\Lucene\Document\Field; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Insert |
|
11
|
|
|
* |
|
12
|
|
|
* @package BestServedCold\LaravelZendSearch\Lucene\Store |
|
13
|
|
|
*/ |
|
14
|
|
|
class Insert |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Document |
|
18
|
|
|
*/ |
|
19
|
|
|
private $document; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var float |
|
23
|
|
|
*/ |
|
24
|
|
|
private $defaultBoost = 1.0; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var null|Document |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $lastInsert = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Insert constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Document $document |
|
35
|
|
|
*/ |
|
36
|
23 |
|
public function __construct(Document $document) |
|
37
|
|
|
{ |
|
38
|
23 |
|
$this->document = $document; |
|
39
|
23 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Insert |
|
43
|
|
|
* |
|
44
|
|
|
* @param integer $id |
|
45
|
|
|
* @param Index $index |
|
46
|
|
|
* @param array $fields fields that are indexed |
|
47
|
|
|
* @param boolean|string $uid unique identifier, if required |
|
48
|
|
|
* @param array $boostFields |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function insert(Index $index, $id, array $fields, $uid = false, array $boostFields = []) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->document->addField($this->field('xref_id', (int) $id)); |
|
54
|
1 |
|
$this->document = $this->addUid($this->document, $uid); |
|
55
|
1 |
|
$this->document = $this->addFields($this->document, $fields, $boostFields); |
|
56
|
1 |
|
self::$lastInsert = $this->document; |
|
57
|
1 |
|
$index->open()->get()->addDocument($this->document); |
|
58
|
1 |
|
$index->get()->commit(); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return null|Document |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public static function getLastInsert() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return self::$lastInsert; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string|integer $field |
|
71
|
|
|
* @param string $value |
|
72
|
|
|
* @param string $type |
|
73
|
|
|
* @return Field |
|
74
|
|
|
*/ |
|
75
|
1 |
|
private function field($field, $value, $type = 'keyword', $boost = null) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$field = Field::$type($field, strtolower(strip_tags($value))); |
|
78
|
1 |
|
$field->boost = $boost ?: $this->defaultBoost; |
|
79
|
1 |
|
return $field; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param Document $document |
|
84
|
|
|
* @param boolean|string $uid |
|
85
|
|
|
* @return Document |
|
86
|
|
|
*/ |
|
87
|
1 |
|
private function addUid(Document $document, $uid = false) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$uid && is_string($uid) ? $document->addField($this->field('uid', base64_encode($uid))) : null; |
|
90
|
1 |
|
return $document; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Document $document |
|
95
|
|
|
* @param array $fields |
|
96
|
|
|
* @param array $boostFields |
|
97
|
|
|
* @return Document |
|
98
|
|
|
*/ |
|
99
|
1 |
|
private function addFields(Document $document, array $fields, array $boostFields = []) |
|
100
|
|
|
{ |
|
101
|
1 |
|
foreach ($fields as $field => $text) { |
|
102
|
1 |
|
$document->addField($this->field($field, $text, 'text', $this->boost($field, $boostFields))); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return $document; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param $field |
|
110
|
|
|
* @param array $boostFields |
|
111
|
|
|
* @return mixed|null |
|
112
|
|
|
*/ |
|
113
|
2 |
|
private function boost($field, array $boostFields = []) |
|
114
|
|
|
{ |
|
115
|
2 |
|
if (empty($boostFields)) { |
|
116
|
1 |
|
return null; |
|
117
|
|
|
} |
|
118
|
1 |
|
return array_key_exists($field, $boostFields) ? $boostFields[$field] : null; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|