ES简单的CURD

在 elasticsearch-php 中,几乎一切操作都是用关联数组来配置。REST 路径(endpoint)、文档和可选参数都是用关联数组来配置。

为了索引一个文档,我们要指定4部分信息:index,type,id 和一个 body。构建一个键值对的关联数组就可以完成上面的内容。body 的键值对格式与文档的数据保持一致性。(译者注:如 [“testField” ⇒ “abc”] 在文档中则为 {“testField” : “abc”}):

在 composer.json 文件中引入 elasticsearch-php:

{
    "require":
        "elasticsearch/elasticsearch": "~6.0"
    }

composer update也可指定更新

 composer update elasticsearch/elasticsearch

在项目中引入自动加载文件(如果还没引入),并且实例化一个客户端 (引入就不需要这步)

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;
//如果更改端口号则需要 setHosts 更改端口号 不写默认9200
//$client= ClientBuilder::create()->setHosts(['127.0.0.1:9400'])->build();
$client = ClientBuilder::create()->build();

创建索引

use Elasticsearch\ClientBuilder;
 
 //简单索引
 $client = ClientBuilder::create()->build();
 $params = [
 'index' => 'my_index'
 ];
// Create the index
$response = $client->indices()->create($params);
//指定分词
 $client = ClientBuilder::create()->build();
 $params = [
 'index' => 'goods',
 'body' => [
 'settings' => [
 'number_of_shards' => 3,
 'number_of_replicas' => 2
 ],
 'mappings' => [
 '_source' => [
 'enabled' => true
 ],
 'properties' => [
 'name' => [
 'type' => 'text',
 "analyzer" => "ik_max_word",
 "search_analyzer" => "ik_max_word"
 ],
 'desc' => [
 'type' => 'text',
 "analyzer" => "ik_max_word",
 "search_analyzer" => "ik_max_word"
 ]
 ]
 ]
 ]
 ];
 // Create the index with mappings and settings now
 $response = $client->indices()->create($params);

添加

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 'type' => '_doc',
 //'id' => '1',
 'body' => ['name' => "iphone7ಋ๢",
 'desc' => "iphone7ಋ๢"]
 ];
$response = $client->index($params);
print_r($response);

搜索

单个id搜索

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 'type' => '_doc',
 'id' => '1'
];
$response = $client->get($params);
print_r($response);

Match搜索

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 //'type' => '_doc',
 'body' => [
 'query' => [
 'match' => [
 'name' => '手机'
 ]
 ],
 'highlight' => [
 'pre_tags' => ["<em>"],
 'post_tags' => ["</em>"],
 'fields' => [
 "name" => new \stdClass()
 			]
 		]
 	]
 ];
 //偏移
 $params["size"] = 1;
 $params["from"] = 1;
 $results = $client->search($params);
 print_r($results);

Bool查询

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 'type' => '_doc',
 'body' => [
 'query' => [
 'bool' => [
 'must' => [
 [ 'match' => [ 'name' => '中华܏' ] ],
 [ 'match' => [ 'name' => '人民' ] ],
 ]
 ]
 ]
 ]
 ];
$results = $client->search($params);
print_r($results);

复杂查询

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 'type' => '_doc',
 'body' => [
 'query' => [
 'bool' => [
 'filter' => [
 'term' => [ 'age' => 12 ]
 ],
 'should' => [
 'match' => [ 'name' => '中国'
 			]
			 ]
		 ]
	 ]
];
$results = $client->search($params);
print_r($results);

高亮显示

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 //'type' => '_doc',
 'body' => [
 'query' => [
 'match' => [
 'name' => '中华人民'
 ]
 ],
 'highlight' => [
 'pre_tags' => ["<em>"],
 'post_tags' => ["</em>"],
 'fields' => [
 "name" => new \stdClass()
 ]
 ]
 ]
 ];
 $results = $client->search($params);
 print_r($results);

删除索引

$client = ClientBuilder::create()->build();
$params = ['index' => 'goods'];
$response = $client->indices()->delete($params)

更新索引

$client = ClientBuilder::create()->build();
$params = [
 'index' => 'goods',
 'type' => '_doc',
 'id' => 'srgiyW0BpEEPDQFsRhIK',
 'body' => [
 'doc' => [
 'name' => 'abc'
 ]
 ]
 ];
 $response = $client->update($params);
 print_r($response);exit;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值