web_server:install-and-configure-elasticsearch-on-centos-7
Đây là một phiên bản cũ của tài liệu!
Mục lục
Install and Configure Elasticsearch on CentOS 7
1. Elasticsearch là gì
- Elasticsearch là một search engine.
- Elasticsearch được xây dựng để hoạt động như một server cloud theo cơ chế của RESTful.
- Kế thừa và phát triển từ Lucene Apache.
- Phát triển bằng ngôn ngữ Java.
- Là phần mềm open-source được phát hành theo giất phép của Apache License.
- Tương tự : Solr (Apache).
- Những ai đã dùng Elasticsearch :
- Mozilla
- Quora
- SoundCloud
- GitHub
- Stack Exchange
- Center for Open Science
- Reverb
- Netflix.
2. Setup
Disable selinux
In /etc/sysconfig/selinux , change the following lines:
sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/sysconfig/selinux sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/selinux/config
reboot, and verify the selinux status by running sestatus
. It should say:
SELinux status: disabled
Cấu hình firewall
yum install firewalld -y systemctl enable firewalld.service systemctl start firewalld.service firewall-cmd --permanent --new-zone=elasticsearch firewall-cmd --permanent --zone=elasticsearch --add-port=9200/tcp firewall-cmd --permanent --zone=elasticsearch --add-source=10.0.0.124 firewall-cmd --permanent --zone=elasticsearch --add-source=10.0.0.99 firewall-cmd --reload
Trong đó
10.0.0.124 là IP của elasticsearch server
10.0.0.99 là IP của máy cần truy cập
Cài đặt và kiểm tra phiên bản của java
yum install java wget -y java -version wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.3.3/elasticsearch-2.3.3.rpm rpm -ivh elasticsearch-2.3.3.rpm systemctl enable elasticsearch.service
Sửa file:vi /etc/elasticsearch/elasticsearch.yml
network.host: 10.0.0.124 # 10.0.0.124 là IP của elasticsearch server cluster.name: NukeViet_MangVN node.name: node-1
Chạy: elasticsearch
systemctl start elasticsearch.service curl -X GET 'http://10.0.0.124:9200'
Install Elasticsearch-Head Plugin
cd /usr/share/elasticsearch bin/plugin install mobz/elasticsearch-head
http://10.0.0.124:9200/_plugin/head/
Tham khảo
3. PHP kết nối Elasticsearch
Tài liệu chi tiết xem tại: https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html
Để sử dụng cần cài thêm thư viện elasticsearch thông qua composer
composer require elasticsearch/elasticsearch
Các đoạn code dùng trong xử lý tìm kiếm với Elasticsearch
1. Kết nối với Elasticsearch
if(isset($db_config['elas_host'])) { $hosts = array( $db_config['elas_host'] . ':' . $db_config['elas_port'] ); $client = Elasticsearch\ClientBuilder::create( )->setHosts( $hosts )->setRetries( 0 )->build(); }
2. Thêm mới 1 row vào Elasticsearch
$module_data = 'news'; $params = [ 'index' => $db_config['elas_index'], 'type' => NV_PREFIXLANG . '_' . $module_data . '_rows', 'id' =>0, 'body' => [] ]; //Index a document: $params['id'] = $rowcontent['id'];//gán id=id của rowcontent khi vừa thêm $params['body'] = $rowcontent;//gán body=body của rowcontent $response = $client->index($params);
3. Cập nhật Elasticsearch
$params = array( ); $params['index'] = $db_config['elas_index']; $params['type'] = NV_PREFIXLANG . '_' . $module_data . '_rows'; $params['id'] = $rowcontent['id'];//gan id= id cua rowcontent $params['body']['doc'] = $rowcontent;//gan body=body cua rowcontent phai để dạng này $params['body']['doc'] nó mới cho update $result_search = $client->update( $params );
4. Search
// khai bao bien $params = [ 'index' => $db_config['elas_index'], 'type' => NV_PREFIXLANG . '_' . $m_values['module_data'] . '_rows' ]; //bo dau tieng viet $dbkeyword=nv_EncString($dbkeyword); // tìm kiếm $params['body']['query']['bool'] = [ 'should' => [ 'multi_match' => [ // dung multi_match:tim kiem theo nhieu truong 'query' => $dbkeyword, // tim kiem theo tu khoa 'type' => [ 'cross_fields' ], 'fields' => [ 'unsigned_title', 'unsigned_hometext', 'unsigned_bodyhtml' ], // tim kiem theo 3 trương mặc định là hoặc 'minimum_should_match' => [ '50%' ] ] ] ]; $params['body']['size']=$limit; $params['body']['from']=($page - 1) * $limit; $response = $client->search($params); // print_r($response);die('test');
5. Xóa dữ liệu trong Elasticsearch
$params = [ 'index' => $db_config['elas_index'], 'type' => NV_PREFIXLANG . '_' . $module_data . '_rows', 'id' => $id, ]; /*Xóa dữ liệu*/ $response = $client->delete($params);
web_server/install-and-configure-elasticsearch-on-centos-7.1474012956.txt.gz · Sửa đổi lần cuối: 2016/09/16 15:02 bởi vuthao