本客户端旨在降低elasticsearch的上手难度,依赖于官方的客户端插件`elasticsearch/elasticsearch`。直接使用官方客户端需要手动构建复杂的请求体,
稍微有一点错误,操作结果就不对。所以单独构建一个依赖官方客户端的插件,用户只需要传入关键字即可,后面增加了类似于关系型数据库
的链式操作方法,用起来更简单一些。当然,本插件只能满足一些常用的功能需求,较为复杂的需求仍然需要手动构建请求体,你可以使用本插件
直接调用官方客户端的方法。
```bash
composer require xiaosongshu/elasticsearch
```
```bash
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.17.7
```
参考 加入Ik分词器的方法:https:
支持thinkPHP,laravel,webman等常用框架,需要创建配置文件elasticsearch.php ,放到config/目录下。
配置内容如下所示:
```php
return [
'nodes' => ['127.0.0.1:9200'],
'username'=>'',
'password'=>'',
];
```
实例化客户端
```php
$client = new \Xiaosongshu\Elasticsearch\ESClient();
```
```php
<?php
require_once 'vendor/autoload.php';
$client = new \Xiaosongshu\Elasticsearch\ESClient([
'nodes' => ['192.168.101.170:9200'],
'username' => '',
'password' => '',
]);
$result = $client->getMap(['index']);
$client->deleteIndex('index');
if (!$client->IndexExists('index')) {
$client->createIndex('index', '_doc');
}
$result = $client->createMappings('index', '_doc', [
'id' => ['type' => 'long',],
'title' => ['type' => 'text', "fielddata" => true,],
'content' => ['type' => 'text', 'fielddata' => true],
'create_time' => ['type' => 'text'],
'test_a' => ["type" => "integer"],
'test_b' => ["type" => "rank_feature", "positive_score_impact" => false],
'test_c' => ["type" => "rank_feature"],
'name' => ['type' => 'text', "fielddata" => true,],
'age' => ['type' => 'integer'],
'sex' => ['type' => 'integer'],
]);
$result = $client->table('index', '_doc')->insertAll([
[
'id' => rand(1, 99999),
'title' => '天有不测风云',
'content' => '月有阴晴圆缺',
'create_time' => date('Y-m-d H:i:s'),
'test_a' => rand(1, 10),
'test_b' => rand(1, 10),
'test_c' => rand(1, 10),
'name' => '张三',
'age' => 27,
'sex' => 1
]
]);
$params = [
'index' => 'my_index',
'type' =>"_doc",
'id' => "demo",
];
$result = $client->count($params);
$result = $client->exists($params);
$result = $client
->table('index','_doc')
->where(['title','=','测试'])
->whereIn('age',[28])
->whereNotIn('age',[27,29])
->orWhere(['test_a','>',8])
->orderBy('test_a','asc')
->limit(0,10)
->select(['name','age'])
->groupBy(['age','sex'])
->sum(['age'])
->getAll();
$result = $client->table('index','_doc')->max(['age'])->getAll();
$result = $client->table('index','_doc')->getAll();
$result = $client->table('index','_doc')->where(['test_a','>',2])->updateAll(['name'=>'陈圆圆']);
$result = $client->table('index','_doc')->where(['test_a','>',2])->deleteAll();
$result = $client->getIndex(['index']);
$result = $client->table('index','_doc')->updateById('kmXADJEBegXAJ580Qqp6',['content'=>'今天你测试了吗']);
$result = $client->table('index','_doc')->deleteByIds(['kmXADJEBegXAJ580Qqp6']);
$result = $client->table('index','_doc')->findById('kmXADJEBegXAJ580Qqp6');
$result = $client->table('index','_doc')->getByIds(['kmXADJEBegXAJ580Qqp6']);
$script = <<<eof
if (doc.containsKey('content') && doc['content'].size() != 0) {
return doc['content.raw'].value + '_' + '谁不说按家乡好';
} else {
return '字段缺失'; // 或者返回其他适当的默认值
}
eof;
$result = $client->addScript('update_content',$script);
$result = $client->addScript('update_content2',"(doc['content'].value)+'_'+'abcdefg'");
$result = $client->getScript('update_content');
$result = $client->table('index','_doc')->withScript('update_content11')->getAll();
$result = $client->deleteScript('update_content');
$result = $client->query([
'index'=>'index',
'type'=>'_doc',
'body'=>[
'query'=>[
'bool'=>[
'must'=>[
[
'match_phrase'=>[
'title'=>'风云'
],
],
[
'script'=>[
'script'=>"doc['content'].size() != 0"
]
]
]
]
]
]
]);
print_r($result);
```
```bash
php ./vendor/bin/phpunit -c phpunit.xml
```
2723659854@qq.com