一个可以充当数据的全文搜索引擎工具。

ES底层原理剖析

配置

推荐阅读博客

docker部署

拉去es镜像到本地。

利用docker-compose部署
配置:

myes:
    image: elasticsearch
    container_name: "myes"
    restart: always
    expose:
      - "9200"
    ports:
      - "9200:9200"
    volumes:
      - ~/server/elasticsearch/conf/elasticsearch.yml:/usr/share/elasticsearch/conf/elasticsearch.yml # 配置文件
      - ~/server/elasticsearch/data:/usr/share/elasticsearch/data # 数据
      - ~/server/elasticsearch/plugins:/usr/share/elasticsearch/plugins # 拓展
      - ~/server/elasticsearch/logs:/usr/share/elasticsearch/logs # 日志
    environment:  # 配置环境变量
      - TZ=Asia/Shanghai  # 设置时区
      - discovery.type=single-node # 单节点
# elasticsearch.yml

http.host 0.0.0.0 # 配置可访问ip,表示不限制ip
http.max_content_length: 200mb

php镜像进行关联即可。

概念

  • index:可以看做一个数据库
  • type:可以看做库中的一张表
  • id:可以看做表中某条数据的主键

基本使用

下面的使用是以Laravel Framework 7.28.1框架进行操作。

安装composer包

composer require "elasticsearch/elasticsearch:~6.0"

使用
安装完成后在控制器中引入

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create() // 实例化
    ->setHost([
        // es的ip地址端口 默认9200端口
        'myes' // 以docker关联的 可以直接写容器名字
    ])
    ->build(); // 构建客户端对象

索引一个文档

如果索引和文档不存在会同时创建。
同一也可以用来增加文档数据

$params = [
    'index' => 'my_index', // 库
    'type' => 'my_type', // 表
    'id' => 'my_id', // 主键
    'body' => ['field1'=>'test', 'field2'=>'value'], // 数据
];
$response = $client->index($params);
dump($response);

成功返回:

^ array:7 [▼
  "_index" => "my_index"
  "_type" => "my_type"
  "_id" => "my_id"
  "_version" => 1
  "result" => "created"
  "_shards" => array:3 [▼
    "total" => 2
    "successful" => 1
    "failed" => 0
  ]
  "created" => true
]

获取一个文档

$response = $client->get([
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'client' => ['ignore' => 404] // 忽略的相应code,未查询到文档时,返回值中的found为false
]);
dump($response);

成功返回:

^ array:6 [
  "_index" => "my_index"
  "_type" => "my_type"
  "_id" => "my_id"
  "_version" => 1
  "found" => true
  "_source" => array:2 [
    "field1" => "test"
    "field2" => "value"
  ]
]

根据条件查询文档

body如果是空,会返回该文档所有的数据

$response = $client->search([
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'field1' => 'test'
            ],
        ],
    ],
]);
dump($response);

成功返回

^ array:4 [
  "took" => 3
  "timed_out" => false
  "_shards" => array:4 [
    "total" => 5
    "successful" => 5
    "skipped" => 0
    "failed" => 0
  ]
  "hits" => array:3 [
    "total" => 1
    "max_score" => 0.2876821
    "hits" => array:1 [
      0 => array:5 [
        "_index" => "my_index"
        "_type" => "my_type"
        "_id" => "my_id"
        "_score" => 0.2876821
        "_source" => array:2 [
          "field1" => "test"
          "field2" => "value"
        ]
      ]
    ]
  ]
]

删除一个文档

$response = $client->delete([
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
]);

dump($response);

成功返回

^ array:7 [
  "found" => true
  "_index" => "my_index"
  "_type" => "my_type"
  "_id" => "my_id"
  "_version" => 2
  "result" => "deleted"
  "_shards" => array:3 [
    "total" => 2
    "successful" => 1
    "failed" => 0
  ]
]

删除索引

$response = $client->indices()->delete(['index' => 'my_index']);
dump($response);

成功返回

^ array:1 [
  "acknowledged" => true
]

创建索引

$response = $client->indices()->create(['index' => 'my_index']);
dump($response);

成功返回

^ array:3 [
  "acknowledged" => true
  "shards_acknowledged" => true
  "index" => "my_index"
]

配置日志

官方推荐日志composer包:monolog/monolog

use Elasticsearch\ClientBuilder;
use Monolog\Logger;

$logger =new Logger("name") // name是日志前缀
$logger->pushHandler(new StreamHandler('/path/filename', Logger::INFO)); // 配置日志路径,记录日志级别
$client = ClientBuilder::create() // 实例化
    ->setHost([
        // es的ip地址端口 默认9200端口
        'myes' // 以docker关联的 可以直接写容器名字
    ])
    ->setLogger($logger) // 日志依赖入驻
    ->build(); // 构建客户端对象

配置中文分词

安装es拓展analysis-ik。

查看es版本,直接访问localhost:9200即可

{
    "name": "name",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "Q0yAnW8ITviAz4PiBYQtKw",
    "version": {
        "number": "5.6.12", // 版本号
        "build_hash": "cfe3d9f",
        "build_date": "2018-09-10T20:12:43.732Z",
        "build_snapshot": false,
        "lucene_version": "6.6.1"
    },
    "tagline": "You Know, for Search"
}

5.6.12版本对应的拓展也需要是5.6.12

下载地址

下载zip文件后解压在配置的拓展目录中。


注意
版本不同会报如下错误:
IllegalArgumentException: plugin [analysis-ik] is incompatible with version [5.6.12]; was designed for version [6.6.1]

/usr/share/elasticsearch/plugins/.DS_Store/plugin-descriptor.properties: Not a directory
上面的错误删除.DS_Store即可

增加文档和文档中的字段信息

$params = [
    'index' => 'data', // 索引
    'type' => 'logs', // 文档
    'body' => [
        'logs' => [
            'properties' => [ // 文档中对应的属性
                'desc' => [
                    'type' => 'text', // 字段类型
                    'analyzer' => 'ik_max_word', // 字段文本分词器
                    'search_analyzer' => 'ik_max_word', // 搜索信息分词器
                ],
            ],
        ],
    ],
];
$response = $client->indices()->putMapping($params);
return $response;

成功返回

{
    "acknowledged": true
}

其他操作

# 验证节点是否正常
$client->ping();
最后修改:2020 年 09 月 24 日 12 : 30 PM
如果觉得我的文章对你有用,请随意赞赏