https://github.com/jae-jae/e-search
Elasticsearch5 PHP Api
https://github.com/jae-jae/e-search
Last synced: 7 days ago
JSON representation
Elasticsearch5 PHP Api
- Host: GitHub
- URL: https://github.com/jae-jae/e-search
- Owner: jae-jae
- Created: 2016-12-04T16:10:30.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-12-09T07:45:51.000Z (over 8 years ago)
- Last Synced: 2025-06-12T23:22:54.379Z (about 1 month ago)
- Language: PHP
- Size: 11.7 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# e-search
Elasticsearch5 PHP Api---
问:为什么官方已经有了Elasticsearch的PHP包,我还要写一个?
答:任性# Install
```
composer require jaeger/e-search:dev-master
```# Code Example
```php
$es = new \Jaeger\Es([
//服务器地址
'server_url'=>'http://localhost:9200',
//索引
'index' => 'news',
//类型
'type' => 'article'
]);//or
$es = (new \Jaeger\Es())->setIndex('news')->setType('article');
```
## Mapping 设置映射
```php
$result = $es->setMapping([
'title' => [
'type' => 'text',
'analyzer' => 'ik_smart'
],
'content' => [
'type' => 'text',
'analyzer' => 'ik_smart'
]
]);
```## Index/Update 索引数据/更新数据
```php
$result = $es->index(1,[
'id' => 1,
'title' => 'This is title',
'content' => 'This is content'
]);
```## Delete 删除数据
```php
//delete document for id 1
$result = $es->delete(1);
//delete all documents of current type
$result = $es->delete();
```## Count 获取当前类型的文档总数量
```php
$result = $es->count();
//or
$result = $es->request('GET','_count');
```## Id 获取指定ID的文档
```
$result = $es->id(1);
//or
$result = $es->request('GET',1);
```## Search 搜索
```
$result = $es->search($query);
````$query` can be an array,JSON string, or string.
### 1.Array
```php
$query = [
'query' => [
'match' => [
'content' => 'this is content'
]
],
'highlight' => [
'fields' => [
//此处有坑
'content' => (object)[]
]
]
];
```
### 2. JSON String
```php
$query = '{
"query" : {
"match" : {
"content" : "this is content"
}
},
"highlight": {
"fields" : {
"content" : {}
}
}
}';
```
### 2. String
```php
$query = 'this is content';
//or
$query = 'content:this is content';
```## Other Command 其它命令
```php
/**
* send command 发送命令
* @param string $method GET,PUT,DELETE,etc
* @param string $command '_search','_count','_mapping',etc
* @param array|jsonString $data send command with data
*/
$result = $es->request($method,$command,$data);//example
$result = $this->request('GET','_search',[
'query' => [
'match' => [
'content' => 'this is content'
]
]
]);```
# Author
Jaeger