1. 程式人生 > >Ubuntu16.04下安裝elasticsearch+kibana實現php客戶端的中文分詞

Ubuntu16.04下安裝elasticsearch+kibana實現php客戶端的中文分詞

lba 實例 exc common adding creat 啟動服務 uid dbms

1.下載安裝elasticsearch和kibana

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.2.deb
dpkg -i elasticsearch-5.4.2.deb
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.2-amd64.deb
dpkg -i kibana-5.4.2-amd64.deb

2.安裝中文分詞插件,包括elasticsearch原生的中文分詞icu和smartcn,以及第三方中文分詞ik、拼音分詞pinyin、繁簡轉換stconvert。

/usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-icu
/usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-smartcn
wget https://github.com/medcl/elasticsearch-analysisi-stconvert/releases/download/v5.4.2/elasticsearch-analysisi-stconvert-5.4.2.zip
/usr/share/elasticsearch/bin/elasticsearch-plugin install
file:///{path}/elasticsearch-analysis-stconvert-5.4.2.zip
wget https://github.com/medcl/elasticsearch-analysisi-ik/releases/download/v5.4.2/elasticsearch-analysis-ik-5.4.2.zip
unzip elasticsearch-analysis-ik-5.4.2.zip -d /usr/share/elasticsearch/plugins/analysis-ik
wget https://github.com/medcl/elasticsearch-analysisi-pinyin/releases/download/v5.4.2/elasticsearch-analysis-pinyin-5.4.2.zip

unzip elasticsearch-analysis-pinyin-5.4.2.zip -d /usr/share/elasticsearch/plugins/analysis-pinyin

3.啟動服務器

service elasticsearch start
service kibana start

4.在kibana的Dev Tools中測試,地址為http://localhost:5601

大體上可以將Elasticsearch理解為一個RDBMS(關系型數據庫,比如MySQL),那麽index 就相當於數據庫實例,type可以理解為表。
Mapping定義了type中的諸多字段的數據類型以及這些字段如何被Elasticsearch處理,比如一個字段是否可以查詢以及如何分詞等。
analyzer=char_filter+tokenizer+token_filter按順序執行

PUT /stconvert/
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "tsconvert" : {
                    "tokenizer" : "tsconvert"
                    }
                "tsconvert_icu" : {
                    "tokenizer" : "icu_tokenizer",
                    "char_filter" : ["tsconvert"],
                    }
            },
            "tokenizer" : {
                "tsconvert" : {
                    "type" : "stconvert",
                    "delimiter" : "#",
                    "keep_both" : false,
                    "convert_type" : "t2s"
                }
            },
            "char_filter" : {
                "tsconvert" : {
                    "type" : "stconvert",
                    "delimiter" : "#",
                    "keep_both" : false,
                    "convert_type" : "t2s"
                }
            }
        }
    },
   "mappings":{
      "test":{
         "properties":{
            "title": {
               "type":"text",
               "analyzer":"tsconvert_icu"
              }
            }
         }
      }
   }
}
GET /stconvert/_analyze?pretty
{
    "analyzer": "tsconvert_icu",
    "text": ["狼藉藉口,北京國際電視檯"]
}
PUT /stconvert/test/1
{
    "title":"狼藉藉口,北京國際電視臺"
}
PUT /stconvert/test/2
{
    "title":"狼藉借口,中央國際電視檯"
}
GET /stconvert/test/_search
{
    "query":{
        "match":{
            "title":"國際"
        }
    }
}

5.安裝composer, elasticsearch-php和php的curl擴展

php -r "copy(‘https://install.phpcomposer.com/installer‘, ‘composer-setup.php‘);"
php composer-setup.php
php composer.phar require "elasticsearch/elasticsearch": "~5.0"
apt-get install php-curl

在/etc/php/7.0/fpm/php.ini中去掉 ;extension=php_curl.dll 前的分號,重啟服務

6.測試php搜索elasticsearch

<?php
require ‘vendor/autoload.php‘;
use Elasticsearch\ClientBuilder;
$hosts=[‘localhost‘];
$client = ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();              // Build the client object
$searchParams = [
    ‘index‘ => ‘stconvert‘,
    ‘type‘ => ‘test‘,
    ‘body‘ => [
        ‘query‘ => [
            ‘match‘ => [
                ‘title‘ => ‘國際‘
            ]
        ]
    ]
]; 
try {
    $results = $client->search($searchParams);
} catch (Elasticsearch\Common\Exceptions\TransportException $e) {
    $previous = $e->getPrevious();
    if ($previous instanceof Elasticsearch\Common\Exceptions\MaxRetriesException) {
        echo "Max retries!";
    }
}
print_r($results);
?>

elasticsearch的幫助文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
elasticsearch-php的幫助文檔:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html

Ubuntu16.04下安裝elasticsearch+kibana實現php客戶端的中文分詞