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

來源:互聯網
上載者:User

標籤:require   啟動服務   iat   add   logs   sha   外掛程式   padding   download   

1.下載安裝elasticsearch和kibana

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.2.debdpkg -i elasticsearch-5.4.2.debwget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.2-amd64.debdpkg -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 startservice 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.phpphp 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

<?phprequire ‘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用戶端的中文分詞

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.