Elasticsearch 實現簡單搜尋

孟丟丟發表於2019-03-07

Elasticsearch實現簡單搜尋
第一步:安裝配置
composer require elasticsearch/elasticsearch
選擇版本相容的分詞外掛,並將分詞外掛放到elasticsearch中的plugins資料夾下
啟動服務:./bin/elasticsearch
第二步:建庫建表
curl –X PUT ‘localhost:9200/(表名)’--新建資料庫
對應的刪除操作:curl –X DELETE‘localhost:9200/(表名)’
根據自己的需求建立表結構:

curl -H'Content-Type: application/json' -XPUT http://localhost:9200/(表名)/_mapping/doc?pretty -d'{
  "properties": {
        "name": { "type": "text", "analyzer": "ik_smart" },
        "email": { "type": "keyword" },
//一維
        "word1": {
            "type": "nested" ,
            "properties":{
                "word2":{"type": "text", "analyzer": "ik_smart"}, //分詞,可以模糊搜尋
                "word3":{"type":"keyword"}, //關鍵字搜尋
            }
        },
//多維
        "word4": {
            "type": "nested",
            "properties":{
                "word5":{"type":"keyword"},
                "word6":{
                        type": "nested",
                                "properties":{
                                    "word7":{"type":"keyword"}
                                }
                  }
              }
          }
 }
}'

database.php

'elasticsearch' => [
        // Elasticsearch 支援多臺伺服器負載均衡,因此這裡是一個陣列
        'hosts' => explode(',', env('ES_HOSTS','localhost')),
    ],

.env配置

ES_HOSTS=localhost
連線本地就好

第三步:PHP使用
註冊es用例在Providers/AppServiceProvider.php 中

public function register()
    {
            // 註冊一個名為 es 的單例
            $this->app->singleton('es', function () {
            // 從配置檔案讀取 Elasticsearch 伺服器列表
            $builder = ESClientBuilder::create()->setHosts(config('database.elasticsearch.hosts'));
            // 如果是開發環境
            if (app()->environment() === 'local') {
                // 配置日誌,Elasticsearch 的請求和返回資料將列印到日誌檔案中,方便我們除錯
                $builder->setLogger(app('log')->getMonolog());
            }
            return $builder->build();
            });
 }

搜尋:

$queryparams = [
                    'index' => 'resumes',
                    'type' => 'doc',
                    'body' => [
                        "query" => [
                            "bool" => [
                                "must" => [
                                    ["match" => [
                                        'name' => ''
                                    ]],
                                    ["match" => [
                                        'sex' => ''
                                    ]],
                        [
                            "nested"=>[
                            "path"=> "education",
                            "query"=>[
                                "bool"=>[
                                    "must"=>[
                                          [
                                              "match"=>[
                                              "education.school"=> "北京大學"
                                            ]
                                          ],
                                    ]
                                ]
                                 ]
                             ]
                      ]
                                ]]]]];
                $es = app('es');
                $res = $es->search($queryparams);
                return $res;

新增:

$es = app('es');
$es->index([
    'index' => '表名',
    'type'  => 'doc',
    'id' => $id,
    'body' => $data
]);

更新:

$es = app('es');
$es->index([
    'index' => '表名',
    'type'  => 'doc',
    'id' => $id,
    'body' => $data
]);
或者(更改資料後_version值更新)
$es = app('es');
$es->update([
    'index' => '表名',
    'type'  => 'doc',
    'id' => $id,
    'body' =>['doc'=> $data]
]);

刪除:

$es = app('es');
$es->delete([
    'index' => '表名',
    'type'  => 'doc',
    'id' => $id
]);

獲取:

$es = app('es');
$es->get([
    'index' => '表名',
    'type'  => 'doc',
    'id' => $id
]);

第一次寫,歡迎指出錯誤?

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章