2015-01-21 3 views
4

fos_elastica가있는 symfony2 프로젝트에서 elasticsearch를 구현합니다.탄성 검색 : 가장 많이 연구되는 용어를 얻는 방법

everythings 잘 (색인 데이터 업데이트 등)

내가 현재 사용자의 행동 분석을 찾고 있어요 작동 : 내가 다시 질의를하기를 위해 10 개 대부분의 사용자 검색 또는 키워드를 좀하고 싶습니다.

예를 들어

: 검색의 45 %가 노란색 풍선과 45 %에 대해있는 빨간 풍선에 대한 있습니다

경우, 나는 생각했다,

먼저 약간의 노란색 또는 빨간색 풍선 내 홈페이지에 제안하고 싶습니다 symfony2 엔티티를 생성하여 타임 스탬프로 사용자 검색을 저장 한 다음 가장 최근의 1000 건을 계산하여 가장 유명한 키워드를 얻으십시오. 그것이 확실하게 작동 할지라도 그것은 자원 살인자가 될 것입니다.

나는 elasticsearch가 이것을 제공 할 수 있는지, 어떻게 구현할 수 있는지 궁금합니다.

나는 내 사용자 쿼리를 저장하기위한 색인을 만들 수 있다는 것을 읽었습니다. (그렇다면 정말 쉽게 계산할 수있는 패싯을 사용할 수 있기 때문에 멋지 겠지만) 나는 탄성에 직접 저장하는 법을 모릅니다. 전용 엔티티없이 symfony2에서 검색하십시오.

답변

3

좋아, 나는 마침내 그것을 얻었다!

1) 키워드 검색에 대한 특정 매핑

in config.yml 

indexes: 
    your_index: 
     types: 
      search: 
       mappings: 
        value: {type:string} 
        date : {type:date} 
        provider: acme\AppBundle\Service\SearchProvider 

2) 서비스 디렉토리

에서 새로운 클래스 SearchProvider를 작성하여 config.yml의 새로운 인덱스를 만들 : 여기

는 서로 다른 단계입니다
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에 새로운 서비스 선언을 만들

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(); 
관련 문제