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

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jun 4, 2017
1 parent eaf727d commit 633ce75
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 14 deletions.
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
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);
}
2 changes: 1 addition & 1 deletion src/Component/ZendSearch/ZendSearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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));
return new ZendSearchIndex($name, new Index($this->directory . DIRECTORY_SEPARATOR . $name, true));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/Component/ZendSearch/ZendSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use Pucene\Component\Client\IndexInterface;
use Pucene\Component\QueryBuilder\Search;
use Pucene\Component\ZendSearch\Compiler\Compiler;
use Pucene\Component\ZendSearch\Compiler\Visitor\VisitorPool;
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;

Expand All @@ -29,6 +30,11 @@ class ZendSearchIndex implements IndexInterface
*/
private $index;

/**
* @var Compiler
*/
private $compiler;

/**
* @param string $name
* @param Index $index
Expand All @@ -38,6 +44,8 @@ public function __construct(string $name, Index $index)
$this->name = $name;
$this->index = $index;

$this->compiler = new Compiler(new VisitorPool());

QueryParser::setDefaultOperator(QueryParser::B_AND);
Analyzer::setDefault(new CaseInsensitive());
}
Expand Down Expand Up @@ -81,9 +89,7 @@ public function delete($type, $id)
*/
public function search(Search $search, $type)
{
$hits = $this->index->find(
new Term(new Index\Term($search->getQuery()->getTerm(), $search->getQuery()->getField()))
);
$hits = $this->index->find($this->compiler->compile($search->getQuery()));

$documents = [];
foreach ($hits as $hit) {
Expand Down

0 comments on commit 633ce75

Please sign in to comment.