Elasticsearch6

elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 2 (php 설정)

Jack Moon 2018. 10. 16. 16:52

### elasticsearch-php 설치


1. composer.json파일에 elasticsearch-php를 포함시킨다 


1
2
3
4
5
{
    "require": {
        "elasticsearch/elasticsearch": "~6.0"
    }
}
cs


2. composer 로 클라이언트를 설치한다.


1
2
curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev
cs


3. 프로젝트에 autoload.php를 포함하고 인스턴트를 만든다.

1
2
3
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
cs


### elasticsearch-php 특징


elasticsearch-php에서 거의 모든 것은 연관 배열로 구성됩니다


### 문서색인


루비, 파이썬, PHP 모두 REST API 를 기본으로 동작한다.

따라서 REST API 운용이 쉬운 키바나에서 인덱스를 제어하고 (생성, 삭제)

MySql or Mariadb 의 데이터를 PHP 에서 bulk_api를 사용하여 가져오고 검색한다.


### 인덱스 생성

index명: indexnews

type명: typenews

뉴스기사의 title과 summary 는 nori로 형태소 분석을 하고 link, 매체명은 형태소 분석없이 그대로 인덱싱.

만약 아래 코드를 그대로 입력하면 에러가 나며 설치되지 않는다. 사용자사전(userdic_ko.txt)을 생성한 후 실행한다. 에러날때 위치가 나타나므로 참고해도 되고, 통상 config 아래 생성하면 된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
PUT car
{
  "settings": {
    "index": {
      "number_of_shards": "1",
      "number_of_replicas": "0",
      "analysis": {
        "analyzer": {
          "nori": {
            "filter": ["pos_filter", "lowercase", "nori_readingform"],
            "tokenizer": "nori_user_dict"
          }
        },
        "tokenizer":{
          "nori_user_dict": {
            "mode": "MIXED",
            "type": "nori_tokenizer",
            "user_dictionary": "userdic_ko.txt"
          }
        },
        "filter": {
          "pos_filter": {
            "type": "nori_part_of_speech",
            "stoptags": [
              "E", "IC", "J", "MAG", "SP", "SSC", "SSO", "SC", "SE", "XPN", "XSA", "XSN", "XSV", "UNA", "NA", "VSV"
              ]
          }
        }
      }
 
    }
  },
  "mappings" : {
    "typenews" : {
      "properties" : {
        "title" : { 
          "type": "text",
          "analyzer": "nori",
          "fielddata": true                
                },
        "summary" : { 
          "type": "text",
          "analyzer": "nori",
          "fielddata": true                
                },
        "link" : { 
                    "type" : "keyword" 
                },
        "media" : { 
                    "type" : "keyword" 
                },
        "datetime" : { 
                    "type" : "date" 
                },
        "timestamp" : { 
                    "type" : "long" 
                }
      }
    }
  }
}'
cs