目录一、安装二、使用三、新建ES数据库四、创建表五、插入数据六、 查询所有数据七、查询单条数据八、搜索九、测试代码〝 古人学问遗无力,少壮功夫老始成 〞 如果这篇文章能给你带来一点帮助,希望给飞兔小哥哥一键三连,表示支持,谢谢各位小伙伴们。
〝 古人学问遗无力,少壮功夫老始成 〞
如果这篇文章能给你带来一点帮助,希望给飞兔小哥哥一键三连,表示支持,谢谢各位小伙伴们。
通过composer安装
composer require 'elasticsearch/elasticsearch'
创建ES类
<?PHP
require 'vendor/autoload.php';
//如果未设置密码
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['xxx.xxx.xxx.xxx'])->build();
//如果es设置了密码
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['Http://username:passWord@xxx.xxx.xxx.xxx:9200'])->build()
index 对应关系型数据(以下简称Mysql)里面的数据库,而不是对应mysql里面的索引
<?php
$params = [
'index' => 'autofelix_db', #index的名字不能是大写和下划线开头
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 0
]
]
];
$es->indices()->create($params);
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'mytype' => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'first_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'last_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer'
]
]
]
]
];
$es->indices()->putMapping($params);
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
//'id' => 1, #可以手动指定id,也可以不指定随机生成
'body' => [
'first_name' => '飞',
'last_name' => '兔',
'age' => 26
]
];
$es->index($params);
<?php
$data = $es->search();
var_dump($data);
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'id' => //你插入数据时候的id
];
$data = $es->get($params);
ES精髓的地方就在于搜索
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'query' => [
'constant_score' => [ //非评分模式执行
'filter' => [ //过滤器,不会计算相关度,速度快
'term' => [ //精确查找,不支持多个条件
'first_name' => '飞'
]
]
]
]
]
];
$data = $es->search($params);
var_dump($data);
基于Laravel环境,包含删除数据库,删除文档等操作
<?php
use Elasticsearch\ClientBuilder;
use Faker\Generator as Faker;
class EsDemo
{
private $EsClient = null;
private $faker = null;
private $index = 'autofelix_db';
private $type = 'autofelix_table';
public function __construct(Faker $faker)
{
$this->EsClient = ClientBuilder::create()->setHosts(['xxx.xxx.xxx.xxx'])->build();
$this->faker = $faker;
}
public function generateDoc($num = 100) {
foreach (range(1,$num) as $item) {
$this->putDoc([
'first_name' => $this->faker->name,
'last_name' => $this->faker->name,
'age' => $this->faker->numberBetween(20,80)
]);
}
}
public function delDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id
];
return $this->EsClient->delete($params);
}
public function search($query = [], $from = 0, $size = 5) {
// $query = [
// 'query' => [
// 'bool' => [
// 'must' => [
// 'match' => [
// 'first_name' => 'Cronin',
// ]
// ],
// 'filter' => [
// 'range' => [
// 'age' => ['gt' => 76]
// ]
// ]
// ]
//
// ]
// ];
$params = [
'index' => $this->index,
// 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至这两个参数都是可选的
'type' => $this->type,
'_source' => ['first_name','age'], // 请求指定的字段
'body' => array_merge([
'from' => $from,
'size' => $size
],$query)
];
return $this->EsClient->search($params);
}
public function getDocs($ids) {
$params = [
'index' => $this->index,
'type' => $this->type,
'body' => ['ids' => $ids]
];
return $this->EsClient->mget($params);
}
public function getDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id
];
return $this->EsClient->get($params);
}
public function updateDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id,
'body' => [
'doc' => [
'first_name' => '张',
'last_name' => '三',
'age' => 99
]
]
];
return $this->EsClient->update($params);
}
public function putDoc($body = []) {
$params = [
'index' => $this->index,
'type' => $this->type,
// 'id' => 1, #可以手动指定id,也可以不指定随机生成
'body' => $body
];
$this->EsClient->index($params);
}
public function delAllIndex() {
$indexList = $this->esStatus()['indices'];
foreach ($indexList as $item => $index) {
$this->delIndex();
}
}
public function esStatus() {
return $this->EsClient->indices()->stats();
}
public function createIndex() {
$this->delIndex();
$params = [
'index' => $this->index,
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
]
]
];
$this->EsClient->indices()->create($params);
}
public function checkIndexExists() {
$params = [
'index' => $this->index
];
return $this->EsClient->indices()->exists($params);
}
public function delIndex() {
$params = [
'index' => $this->index
];
if ($this->checkIndexExists()) {
$this->EsClient->indices()->delete($params);
}
}
public function getMapping() {
$params = [
'index' => $this->index
];
return $this->EsClient->indices()->getMapping($params);
}
public function createMapping() {
$this->createIndex();
$params = [
'index' => $this->index,
'type' => $this->type,
'body' => [
$this->type => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'first_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'last_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer'
]
]
]
]
];
$this->EsClient->indices()->putMapping($params);
$this->generateDoc();
}
}
到此这篇关于php操作ElasticSearch搜索引擎流程详解的文章就介绍到这了,更多相关php ElasticSearch搜索引擎内容请搜索编程界以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程界!
--结束END--
本文标题: php操作ElasticSearch搜索引擎流程详解
本文链接: https://lsjlt.com/news/50.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0