- 0
- 0
- 0
- 打赏
位置: 首页 >文章
通过composer安装
composer require 'elasticsearch/elasticsearch'
<?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' => '测试1',
'last_name' => '测试2',
'age' => 30
]
];
$es->index($params);
<?php
$data = $es->search();
var_dump($data);
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'id' => //你插入数据时候的id
];
$data = $es->get($params);
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'query' => [
'constant_score' => [ //非评分模式执行
'filter' => [ //过滤器,不会计算相关度,速度快
'term' => [ //精确查找,不支持多个条件
'first_name' => '测试1'
]
]
]
]
]
];
$data = $es->search($params);
var_dump($data);
Tags: php Laravel elasticSearch
转载:欢迎来到本站,转载请注明文章出处https://www.ormcc.com ,欢迎加入技术讨论群599606903
ormcc
一个爱捣鼓的程序员
IP访问121771次,运行1480天
微信支付宝