gpt4 book ai didi

symfony - Elasticsearch : How to get most researched terms

转载 作者:行者123 更新时间:2023-11-29 02:50:06 24 4
gpt4 key购买 nike

我正在使用 fos_elastica 在 symfony2 项目中实现 elasticsearch。

一切正常(索引数据、更新等)

我目前正在寻找用户行为分析:我想获得用户搜索最多的 10 个或关键字,以便重新查询它。

例如:

如果 45% 的搜索是关于黄色气球的,45% 是关于红色气球的,我想在我的主页上推荐一些黄色或红色的气球

首先,我正在考虑创建一个 symfony2 实体来保存带有时间戳的用户搜索,然后计算最后 1000 次搜索以获得最著名的关键字。尽管它肯定会起作用,但这将是资源 killer 。

我想知道 elasticsearch 是否能够提供这些以及如何实现它。

我读过我可以创建一个索引来存储我的用户查询(这会很棒,因为我可以使用分面来非常轻松地计算它们),但我不知道如何在 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) 在Service目录下新建类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();

关于symfony - Elasticsearch : How to get most researched terms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com