Elasticsearch

[Elasticsearch] Elasticsearch PHP API

Jack Moon 2015. 8. 26. 15:34

Elasticsearch PHP API (https://www.elastic.co/guide/en/elasticsearch/client/php-api/index.html)

 

PHP API: 1.0 (current) 사용

https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html 보시면 간단하게 설치및 사용법 나옵니다.

 

1. 설치

 

PHP 5.3.9 이상

 

1) composer.json 파일 생성

{
    "require": {
        "elasticsearch/elasticsearch": "~1.0"
    }
}

 

2) composer 설치

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

 

2. 인덱싱

 

insert.php

 

<?
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();

$params = array ();
$params [ 'body' ]   = array ( 'title'  =>  '기사제목', 'media'  =>  '매체명', 'link'  =>  'http://news~~~' );
$params [ 'index' ]  =  '인덱스명' ;
$params [ 'type' ]   =  '타입명' ;
$params [ 'id' ]     =  '문서ID' ;
$ret = $client -> index ( $params );

?>

 

3. 검색

 

search.php

 

<?

require 'vendor/autoload.php'; $client = new Elasticsearch\Client();

 

 $params['body'] = array(
  'query' => array(
   'multi_match' => array(
      'fields' => array('title', 'media'),
      'query' => '검색어'
   )
  )
 );
 $results = $client->search($params);

 

print_r($results);

 

?>