好吧,我終於明白了!
這裏有不同的步驟:
1)創建config.yml一個新的索引與特定映射爲您的關鍵字搜索
in config.yml
indexes:
your_index:
types:
search:
mappings:
value: {type:string}
date : {type:date}
provider: acme\AppBundle\Service\SearchProvider
2)創建服務目錄
一個新的類SearchProvider
in acme\Appbundle\Service\SearchProvider
<?php
namespace acme\AppBundle\Service;
use FOS\ElasticaBundle\Provider\ProviderInterface;
use Elastica\Type;
use Elastica\Document;
class SearchProvider implements ProviderInterface
{
protected $searchType;
private $search;
public function __construct(Type $searchType)
{
$this->searchType = $searchType;
}
// the function you will call from your service
public function add($search)
{
$this->search = $search;
$this->populate();
}
/**
* Insert the repository objects in the type index
*
* @param \Closure $loggerClosure
* @param array $options
*/
public function populate(\Closure $loggerClosure = null, array $options = array())
{
if ($loggerClosure) {
$loggerClosure('Indexing users');
}
$date = time();
$document = new Document();
$document->setData(array('value' => $this->search, 'date' => $date));
$this->userType->addDocuments(array($document));
$this->userType->getIndex()->refresh();
}
}
3)在service.yml創建新的服務聲明
services:
acme.search_provider:
class: acme\AppBundle\Service\SearchProvider
arguments:
- @fos_elastica.index.recetas.search
tags:
- { name: fos_elastica.provider, index: your_index, type: search }
4)打電話給你的服務來存儲新的搜索這樣
$this->get("acme.search_provider")->add("kapoue");
kapoue將被添加到搜索。
5)得到所有的搜索關鍵詞,並聚集它排在
$es = $this->get('fos_elastica.index.acme.search');
$query = new \Elastica\Query();
$aggregation = new \Elastica\Aggregation\Terms("top_hits");
$aggregation->setField('value');
$aggregation->setSize(3);
$query->addAggregation($aggregation);
$result = $es->search($query);
$mostResearched = $result->getAggregation("top_hits");
print_r ($mostResearched); die();