Elasticsearch:如何获得搜索次数最多的词
Elastic Search : How to get most researched terms
我正在使用 fos_elastica.
在 symfony2 项目中实现 elasticsearch
一切正常(索引数据、更新等)
我目前正在寻找用户行为分析:我想获得用户搜索最多的10个或关键字以便重新查询它。
例如:
如果 45% 的搜索与黄色气球有关,而 45% 与红色气球有关,我想在我的主页上推荐一些黄色或红色气球
首先,我正在考虑创建一个 symfony2 实体来保存带有时间戳的用户搜索,然后计算最后 1000 次搜索以获得最著名的关键字。虽然它肯定会起作用,但这将是资源杀手。
我想知道 elasticsearch 是否能够提供这些以及如何实现它。
我读过我可以创建一个索引来存储我的用户查询(那会很棒,因为我可以使用分面来非常轻松地计算它们),但我不知道如何将它直接保存在没有专用实体的 symfony2 弹性搜索。
好的,我终于明白了!
以下是不同的步骤:
1) 在 config.yml 中创建一个新索引,并为您的关键字搜索提供特定映射
in config.yml
indexes:
your_index:
types:
search:
mappings:
value: {type:string}
date : {type:date}
provider: acme\AppBundle\Service\SearchProvider
2) 在服务目录
中创建一个新的class 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();
我正在使用 fos_elastica.
在 symfony2 项目中实现 elasticsearch一切正常(索引数据、更新等)
我目前正在寻找用户行为分析:我想获得用户搜索最多的10个或关键字以便重新查询它。
例如:
如果 45% 的搜索与黄色气球有关,而 45% 与红色气球有关,我想在我的主页上推荐一些黄色或红色气球
首先,我正在考虑创建一个 symfony2 实体来保存带有时间戳的用户搜索,然后计算最后 1000 次搜索以获得最著名的关键字。虽然它肯定会起作用,但这将是资源杀手。
我想知道 elasticsearch 是否能够提供这些以及如何实现它。
我读过我可以创建一个索引来存储我的用户查询(那会很棒,因为我可以使用分面来非常轻松地计算它们),但我不知道如何将它直接保存在没有专用实体的 symfony2 弹性搜索。
好的,我终于明白了!
以下是不同的步骤:
1) 在 config.yml 中创建一个新索引,并为您的关键字搜索提供特定映射
in config.yml
indexes:
your_index:
types:
search:
mappings:
value: {type:string}
date : {type:date}
provider: acme\AppBundle\Service\SearchProvider
2) 在服务目录
中创建一个新的class SearchProviderin 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();