Skip to content
This repository has been archived by the owner on Dec 25, 2022. It is now read-only.

Added zend_search adapter #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ install:
- ./tests/bin/console doctrine:database:create -e prod
- ./tests/bin/console pucene:indices:create -e prod
- ./tests/bin/console test:import:json my_index ./tests/app/data.json --adapter pucene -e prod
- ./tests/bin/console zend_search:indices:create -e prod
- ./tests/bin/console test:import:json my_index ./tests/app/data.json --adapter zend_search -e prod

script:
- ./vendor/bin/phpunit $CODE_COVERAGE
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

"elasticsearch/elasticsearch": "^2.0",
"ramsey/uuid": "^3.5",
"doctrine/dbal": "^2.5"
"doctrine/dbal": "^2.5",
"zendframework/zendsearch": "2.0.0rc6"
},
"require-dev": {
"doctrine/doctrine-bundle": "^1.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$treeBuilder->root('pucene')
->children()
->enumNode('adapter')->isRequired()->values(['elasticsearch', 'pucene'])->end()
->enumNode('adapter')->isRequired()->values(['elasticsearch', 'pucene', 'zend_search'])->end()
->append($this->getAdaptersNode())
->append($this->getIndexNode())
->end();
Expand All @@ -39,6 +39,11 @@ private function getAdaptersNode()
->scalarNode('doctrine_dbal_connection')->defaultValue('doctrine.dbal.default_connection')->end()
->end()
->end()
->arrayNode('zend_search')
->children()
->scalarNode('directory')->defaultValue('%kernel.root_dir%/../var/indexes')->end()
->end()
->end()
->arrayNode('elasticsearch')
->addDefaultsIfNotSet()
->children()
Expand Down
16 changes: 16 additions & 0 deletions src/Bundle/PuceneBundle/DependencyInjection/PuceneExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function load(array $configs, ContainerBuilder $container)
$this->loadPucene($config, $container);
} elseif ($adapter === 'elasticsearch') {
$this->loadElasticsearch($config, $container);
} elseif ($adapter === 'zend_search') {
$this->loadZendSearch($config, $container);
}

$container->setAlias('pucene.client', 'pucene.' . $adapter . '.client');
Expand Down Expand Up @@ -95,4 +97,18 @@ private function loadElasticsearch($config, $container)
$pass = new CollectorCompilerPass('pucene.elasticsearch.visitor', 'pucene.elasticsearch.visitor_pool', 'query');
$pass->process($container);
}

/**
* Load specific configuration for zend_search.
*
* @param array $config
* @param ContainerBuilder $container
*/
private function loadZendSearch($config, $container)
{
$container->setParameter(
'pucene.adapter_config.zend_search.directory',
$config['adapters']['zend_search']['directory']
);
}
}
13 changes: 13 additions & 0 deletions src/Bundle/PuceneBundle/Resources/config/zend_search/services.xml
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>
8 changes: 0 additions & 8 deletions src/Component/Elasticsearch/Compiler/ElementInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Component/Elasticsearch/Compiler/VisitorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface VisitorInterface
/**
* @param QueryInterface $query
*
* @return ElementInterface
* @return array
*/
public function visit(QueryInterface $query);
}
37 changes: 37 additions & 0 deletions src/Component/ZendSearch/Compiler/Compiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler;

use Pucene\Component\QueryBuilder\Query\QueryInterface;
use Pucene\Component\Symfony\Pool\PoolInterface;

class Compiler
{
/**
* @var PoolInterface
*/
private $visitors;

/**
* @param PoolInterface $visitors
*/
public function __construct(PoolInterface $visitors)
{
$this->visitors = $visitors;
}

public function compile(QueryInterface $query)
{
return $this->getVisitor($query)->visit($query);
}

/**
* @param QueryInterface $query
*
* @return VisitorInterface
*/
private function getVisitor(QueryInterface $query)
{
return $this->visitors->get(get_class($query));
}
}
33 changes: 33 additions & 0 deletions src/Component/ZendSearch/Compiler/Visitor/Compound/BoolVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor\Compound;

use Pucene\Component\ZendSearch\Compiler\VisitorInterface;
use Pucene\Component\QueryBuilder\Query\Compound\BoolQuery;
use Pucene\Component\QueryBuilder\Query\QueryInterface;
use Pucene\Component\Symfony\Pool\PoolInterface;

class BoolVisitor implements VisitorInterface
{
/**
* @var PoolInterface
*/
private $interpreterPool;

/**
* @param PoolInterface $interpreterPool
*/
public function __construct(PoolInterface $interpreterPool)
{
$this->interpreterPool = $interpreterPool;
}

/**
* {@inheritdoc}
*
* @param BoolQuery $query
*/
public function visit(QueryInterface $query)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor\FullText;

use Pucene\Component\Analysis\StandardAnalyzer;
use Pucene\Component\QueryBuilder\Query\FullText\MatchQuery;
use Pucene\Component\QueryBuilder\Query\QueryInterface;
use Pucene\Component\ZendSearch\Compiler\VisitorInterface;
use ZendSearch\Lucene\Index;
use ZendSearch\Lucene\Search\Query\MultiTerm;

class MatchVisitor implements VisitorInterface
{
/**
* {@inheritdoc}
*
* @param MatchQuery $query
*/
public function visit(QueryInterface $query)
{
$analyzer = new StandardAnalyzer();

$multiTerm = new MultiTerm();
foreach ($analyzer->analyze($query->getQuery()) as $token) {
$multiTerm->addTerm(new Index\Term($token->getTerm(), $query->getField()), null);
}

return $multiTerm;
}
}
19 changes: 19 additions & 0 deletions src/Component/ZendSearch/Compiler/Visitor/MatchAllVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor;

use Pucene\Component\ZendSearch\Compiler\VisitorInterface;
use Pucene\Component\QueryBuilder\Query\MatchAllQuery;
use Pucene\Component\QueryBuilder\Query\QueryInterface;

class MatchAllVisitor implements VisitorInterface
{
/**
* {@inheritdoc}
*
* @param MatchAllQuery $query
*/
public function visit(QueryInterface $query)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor\Specialized;

use Pucene\Component\QueryBuilder\Query\QueryInterface;
use Pucene\Component\QueryBuilder\Query\Specialized\MoreLikeThis\MoreLikeThisQuery;
use Pucene\Component\ZendSearch\Compiler\VisitorInterface;

class MoreLikeThisVisitor implements VisitorInterface
{
/**
* {@inheritdoc}
*
* @param MoreLikeThisQuery $query
*/
public function visit(QueryInterface $query)
{
}
}
19 changes: 19 additions & 0 deletions src/Component/ZendSearch/Compiler/Visitor/TermLevel/IdsVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor\TermLevel;

use Pucene\Component\ZendSearch\Compiler\VisitorInterface;
use Pucene\Component\QueryBuilder\Query\QueryInterface;
use Pucene\Component\QueryBuilder\Query\TermLevel\IdsQuery;

class IdsVisitor implements VisitorInterface
{
/**
* {@inheritdoc}
*
* @param IdsQuery $query
*/
public function visit(QueryInterface $query)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor\TermLevel;

use Pucene\Component\QueryBuilder\Query\QueryInterface;
use Pucene\Component\QueryBuilder\Query\TermLevel\TermQuery;
use Pucene\Component\ZendSearch\Compiler\VisitorInterface;
use ZendSearch\Lucene\Search\Query\Term;
use ZendSearch\Lucene\Index;

class TermVisitor implements VisitorInterface
{
/**
* {@inheritdoc}
*
* @param TermQuery $query
*/
public function visit(QueryInterface $query)
{
return new Term(new Index\Term($query->getTerm(), $query->getField()));
}
}
32 changes: 32 additions & 0 deletions src/Component/ZendSearch/Compiler/Visitor/VisitorPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler\Visitor;

use Pucene\Component\QueryBuilder\Query\FullText\MatchQuery;
use Pucene\Component\QueryBuilder\Query\MatchAllQuery;
use Pucene\Component\QueryBuilder\Query\TermLevel\TermQuery;
use Pucene\Component\Symfony\Pool\PoolInterface;
use Pucene\Component\ZendSearch\Compiler\Visitor\FullText\MatchVisitor;
use Pucene\Component\ZendSearch\Compiler\Visitor\TermLevel\TermVisitor;

class VisitorPool implements PoolInterface
{
private $visitors = [];

public function __construct()
{
$this->visitors = [
TermQuery::class => new TermVisitor(),
MatchAllQuery::class => new MatchAllVisitor(),
MatchQuery::class => new MatchVisitor(),
];
}

/**
* {@inheritdoc}
*/
public function get($alias)
{
return $this->visitors[$alias];
}
}
16 changes: 16 additions & 0 deletions src/Component/ZendSearch/Compiler/VisitorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Pucene\Component\ZendSearch\Compiler;

use Pucene\Component\QueryBuilder\Query\QueryInterface;
use ZendSearch\Lucene\Search\Query\AbstractQuery;

interface VisitorInterface
{
/**
* @param QueryInterface $query
*
* @return AbstractQuery
*/
public function visit(QueryInterface $query);
}
56 changes: 56 additions & 0 deletions src/Component/ZendSearch/ZendSearchClient.php
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($name, new Index($this->directory . DIRECTORY_SEPARATOR . $name, true));
}

/**
* {@inheritdoc}
*/
public function delete($name)
{
$this->filesystem->remove($this->directory . DIRECTORY_SEPARATOR . $name);
}
}
Loading