溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

elasticsearch 的使用

發(fā)布時(shí)間:2020-07-20 03:53:05 來源:網(wǎng)絡(luò) 閱讀:1161 作者:juggles 欄目:web開發(fā)

ElasticSearch是什么?

ElasticSearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是當(dāng)前流行的企業(yè)級(jí)搜索引擎。設(shè)計(jì)用于云計(jì)算中,能夠達(dá)到實(shí)時(shí)搜索,穩(wěn)定,可靠,快速,安裝使用方便。

簡(jiǎn)單來說就是用來做網(wǎng)站/APP搜索功能

mac下環(huán)境安裝

brew install elasticsearch

還需要安裝logstash和mysql-connector用于同步mysql數(shù)據(jù)

brew install logstash
mysql-connector 在phpstorm目錄下就會(huì)有這個(gè)文件

/Users/wcc/Library/Preferences/PhpStorm2018.1/jdbc-drivers/MySQL Connector/J/5.1.46/mysql-connector-java-5.1.46.ja

所以這里我就不安裝了,需要安裝的自行搜索

安裝logstash之后進(jìn)入logstash的bin目錄執(zhí)行

logstash-plugin install logstash-input-jdbc

搜索

curl -XGET 'http://localhost:9200/request_log/_search?pretty&q=source:WWW'

使用ElasticSearch

配置同步規(guī)則

在bin目錄下新增request_mysql.conf

input {
  jdbc {
    jdbc_driver_library => "/Users/wcc/Library/Preferences/PhpStorm2018.1/jdbc-drivers/MySQL Connector/J/5.1.46/mysql-connector-java-5.1.46.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false"
    jdbc_user => "root"
    jdbc_password => "root"
    statement => "SELECT * FROM pre_request_logs_20180524 where update_time > :sql_last_value"
    jdbc_paging_enabled => "true"
    jdbc_page_size => "50000"
    schedule => "* * * * *"
  }
}

filter {
   json {
        source => "message"
        remove_field => ["message"]
    }
}

output {
  stdout {
    codec => rubydebug
  }
  elasticsearch {
    hosts => "localhost"
    index => "request_log"
  }
}

注意statement對(duì)于的sql語句就是要同步數(shù)據(jù)時(shí)執(zhí)行的sql
update_time > :sql_last_value代表每次同步新增的數(shù)據(jù),只有就不會(huì)導(dǎo)出重復(fù)同步數(shù)據(jù)

執(zhí)行配置文件

logstash -f request_mysql.conf

每隔一分鐘就會(huì)執(zhí)行一次sql用來同步數(shù)據(jù)
elasticsearch 的使用

查看所有索引

curl 'localhost:9200/_cat/indices?v'

在PHP中使用

下載elasticsearch的php擴(kuò)展

composer.json文件中新增

{
    "require": {
        "elasticsearch/elasticsearch": "~6.0",
        "monolog/monolog": "~1.0"
    }
}
composer update

文件目錄如下
elasticsearch 的使用

先試試搜索功能

搜索source=WEB的所有行
use Elasticsearch\ClientBuilder;
require '../vendor/autoload.php';

$client = ClientBuilder::create()
    ->build();

$params = [
    'index' => 'request_log',
    'body' => [
        'query' => [
            'match' => [
                'source' => 'WEB'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);

elasticsearch 的使用

total:搜索到的行數(shù)
hits:搜索到數(shù)據(jù)的具體內(nèi)容

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI