|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Stinger Entity Search package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
|
7
|
|
|
* (c) Florian Meyer <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Services; |
|
13
|
|
|
|
|
14
|
|
|
use StingerSoft\EntitySearchBundle\Model\Document; |
|
15
|
|
|
use StingerSoft\EntitySearchBundle\Model\Query; |
|
16
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetSet; |
|
17
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetSetAdapter; |
|
18
|
|
|
use StingerSoft\EntitySearchBundle\Model\ResultSetAdapter; |
|
19
|
|
|
|
|
20
|
|
|
class DummySearchService extends AbstractSearchService { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var Document[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $index = array(); |
|
27
|
|
|
|
|
28
|
|
|
protected function createDocumentId(Document $document) { |
|
29
|
|
|
$id = $document->getEntityClass(); |
|
30
|
|
|
$id .= '#'; |
|
31
|
|
|
$entityId = $document->getEntityId(); |
|
32
|
|
|
if(is_scalar($entityId)) { |
|
33
|
|
|
$id .= $entityId; |
|
34
|
|
|
} else { |
|
35
|
|
|
$id .= md5(serialize($entityId)); |
|
36
|
|
|
} |
|
37
|
|
|
return $id; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
|
|
* |
|
44
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::clearIndex() |
|
45
|
|
|
*/ |
|
46
|
|
|
public function clearIndex() { |
|
47
|
|
|
$this->index = array(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
* |
|
54
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::saveDocument() |
|
55
|
|
|
*/ |
|
56
|
|
|
public function saveDocument(Document $document) { |
|
57
|
|
|
$this->index[$this->createDocumentId($document)] = $document; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
* |
|
64
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::removeDocument() |
|
65
|
|
|
*/ |
|
66
|
|
|
public function removeDocument(Document $document) { |
|
67
|
|
|
unset($this->index[$this->createDocumentId($document)]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
* |
|
74
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::autocomplete() |
|
75
|
|
|
*/ |
|
76
|
|
|
public function autocomplete($search, $maxResults = 10) { |
|
77
|
|
|
$words = array(); |
|
78
|
|
|
foreach($this->index as $doc) { |
|
79
|
|
|
foreach($doc->getFields() as $content) { |
|
80
|
|
|
if(is_string($content)) { |
|
81
|
|
|
$words = array_merge($words, explode(' ', $content)); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return array_filter($words, function ($word) use ($search) { |
|
87
|
|
|
return stripos($word, $search) === 0; |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
* |
|
95
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::search() |
|
96
|
|
|
*/ |
|
97
|
|
|
public function search(Query $query) { |
|
98
|
|
|
$term = $query->getSearchTerm(); |
|
99
|
|
|
|
|
100
|
|
|
$result = new ResultSetAdapter(); |
|
101
|
|
|
$facets = new FacetSetAdapter(); |
|
102
|
|
|
|
|
103
|
|
|
$hits = array(); |
|
104
|
|
|
foreach($this->index as $key => $doc) { |
|
105
|
|
|
foreach($doc->getFields() as $content) { |
|
106
|
|
|
if(is_string($content) && stripos($content, $term) !== false) { |
|
107
|
|
|
if(!isset($hits[$key])) { |
|
108
|
|
|
$hits[$key] = 0; |
|
109
|
|
|
} |
|
110
|
|
|
$hits[$key] = $hits[$key] + 1; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
arsort($hits); |
|
115
|
|
|
$results = array(); |
|
116
|
|
|
foreach(array_keys($hits) as $docId) { |
|
117
|
|
|
$doc = $this->index[$docId]; |
|
118
|
|
|
$facets->addFacetValue(FacetSet::FACET_ENTITY_TYPE, $doc->getEntityClass()); |
|
119
|
|
|
$facets->addFacetValue(FacetSet::FACET_AUTHOR, $doc->getFieldValue(Document::FIELD_AUTHOR)); |
|
120
|
|
|
$results[] = $doc; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$result->setResults($results); |
|
124
|
|
|
$result->setFacets($facets); |
|
125
|
|
|
|
|
126
|
|
|
return $result; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* |
|
131
|
|
|
* {@inheritDoc} |
|
132
|
|
|
* |
|
133
|
|
|
* @see \StingerSoft\EntitySearchBundle\Services\SearchService::getIndexSize() |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getIndexSize() { |
|
136
|
|
|
return count($this->index); |
|
137
|
|
|
} |
|
138
|
|
|
} |