This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3371b15
commit eaf727d
Showing
10 changed files
with
277 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Bundle/PuceneBundle/Resources/config/zend_search/services.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="pucene.zend_search.client.filesystem" class="Symfony\Component\Filesystem\Filesystem"/> | ||
|
||
<service id="pucene.zend_search.client" class="Pucene\Component\ZendSearch\ZendSearchClient"> | ||
<argument type="string">%pucene.adapter_config.zend_search.directory%</argument> | ||
<argument type="service" id="pucene.zend_search.client.filesystem"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Pucene\Component\ZendSearch; | ||
|
||
use Pucene\Component\Client\ClientInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use ZendSearch\Lucene\Index; | ||
|
||
class ZendSearchClient implements ClientInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $directory; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* @param string $directory | ||
* @param Filesystem $filesystem | ||
*/ | ||
public function __construct($directory, Filesystem $filesystem) | ||
{ | ||
$this->directory = $directory; | ||
$this->filesystem = $filesystem; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($name) | ||
{ | ||
return new ZendSearchIndex($name, new Index($this->directory . DIRECTORY_SEPARATOR . $name)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create($name, array $parameters) | ||
{ | ||
$this->filesystem->mkdir($this->directory . DIRECTORY_SEPARATOR . $name); | ||
|
||
return new ZendSearchIndex(new Index($this->directory . DIRECTORY_SEPARATOR . $name, true)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($name) | ||
{ | ||
$this->filesystem->remove($this->directory . DIRECTORY_SEPARATOR . $name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace Pucene\Component\ZendSearch; | ||
|
||
use Pucene\Component\Client\IndexInterface; | ||
use Pucene\Component\QueryBuilder\Search; | ||
use Ramsey\Uuid\Uuid; | ||
use ZendSearch\Lucene\Analysis\Analyzer\Analyzer; | ||
use ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8\CaseInsensitive; | ||
use ZendSearch\Lucene\Document; | ||
use ZendSearch\Lucene\Index; | ||
use ZendSearch\Lucene\Search\Query\Term; | ||
use ZendSearch\Lucene\Search\QueryHit; | ||
use ZendSearch\Lucene\Search\QueryParser; | ||
|
||
class ZendSearchIndex implements IndexInterface | ||
{ | ||
const ID_FIELD = '_id'; | ||
const TYPE_FIELD = '_type'; | ||
const SOURCE_FIELD = '_source'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var Index | ||
*/ | ||
private $index; | ||
|
||
/** | ||
* @param string $name | ||
* @param Index $index | ||
*/ | ||
public function __construct(string $name, Index $index) | ||
{ | ||
$this->name = $name; | ||
$this->index = $index; | ||
|
||
QueryParser::setDefaultOperator(QueryParser::B_AND); | ||
Analyzer::setDefault(new CaseInsensitive()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function index(array $document, $type, $id = null) | ||
{ | ||
if ($id) { | ||
$this->delete($type, $id); | ||
} | ||
|
||
$zendDocument = new Document(); | ||
$zendDocument->addField(Document\Field::keyword(self::ID_FIELD, $id ?: Uuid::uuid4()->toString())); | ||
$zendDocument->addField(Document\Field::text(self::TYPE_FIELD, $type)); | ||
$zendDocument->addField(Document\Field::unIndexed(self::SOURCE_FIELD, serialize($document))); | ||
|
||
foreach ($document as $key => $value) { | ||
$zendDocument->addField(Document\Field::text($key, $value)); | ||
} | ||
|
||
$this->index->addDocument($zendDocument); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($type, $id) | ||
{ | ||
$hit = $this->getHit($type, $id); | ||
if (!$hit) { | ||
return; | ||
} | ||
|
||
$this->index->delete($hit->id); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function search(Search $search, $type) | ||
{ | ||
$hits = $this->index->find( | ||
new Term(new Index\Term($search->getQuery()->getTerm(), $search->getQuery()->getField())) | ||
); | ||
|
||
$documents = []; | ||
foreach ($hits as $hit) { | ||
/** @var Document $document */ | ||
$document = $hit->getDocument(); | ||
$documents[] = [ | ||
'_id' => $document->getFieldValue(self::ID_FIELD), | ||
'_type' => $document->getFieldValue(self::TYPE_FIELD), | ||
'_index' => $this->name, | ||
'_score' => $hit->score, | ||
'_source' => unserialize($document->getFieldValue(self::SOURCE_FIELD)), | ||
]; | ||
} | ||
|
||
return ['hits' => $documents]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($type, $id) | ||
{ | ||
$document = $this->getHit($type, $id)->getDocument(); | ||
if (!$document) { | ||
return; | ||
} | ||
|
||
return unserialize($document->getFieldValue(self::SOURCE_FIELD)); | ||
} | ||
|
||
/** | ||
* @param string $type | ||
* @param string $id | ||
* | ||
* @return QueryHit | ||
*/ | ||
private function getHit(string $type, string $id) | ||
{ | ||
$hits = $this->index->find(self::ID_FIELD . ':' . $id . ' AND ' . self::TYPE_FIELD . ':' . $type); | ||
if (count($hits) === 0) { | ||
return; | ||
} | ||
|
||
return $hits[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters