jaeger/e-search

Elasticsearch5 PHP Api

dev-master 2016-12-09 07:45 UTC

This package is auto-updated.

Last update: 2024-11-09 16:16:52 UTC


README

Elasticsearch5 PHP Api

问:为什么官方已经有了Elasticsearch的PHP包,我还要写一个? 答:任性

Install

composer require jaeger/e-search:dev-master

Code Example

$es = new \Jaeger\Es([
    //服务器地址
    'server_url'=>'http://localhost:9200',
    //索引
    'index' => 'news',
    //类型
    'type' => 'article'
]);

//or

$es = (new \Jaeger\Es())->setIndex('news')->setType('article');

Mapping 设置映射

$result = $es->setMapping([
   'title' => [
       'type' => 'text',
       'analyzer' => 'ik_smart'
   ],
   'content' => [
          'type' => 'text',
          'analyzer' => 'ik_smart'
      ]
]);

Index/Update 索引数据/更新数据

$result = $es->index(1,[
    'id' => 1,
    'title' => 'This is title',
    'content' => 'This is content'
]);

Delete 删除数据

//delete document for id 1
$result = $es->delete(1);
//delete all documents of  current type
$result = $es->delete();

Count 获取当前类型的文档总数量

$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

$query = [
    'query' => [
        'match' => [
            'content' => 'this is content'
        ]
    ],
    'highlight' => [
        'fields' => [
            //此处有坑
            'content' => (object)[]
        ]
    ]
];

2. JSON String

$query = '{
              "query" : {
                  "match" : {
                      "content" : "this is content"
                  }
              },
              "highlight": {
                  "fields" : {
                      "content" : {}
                  }
              }
          }';

2. String

$query = 'this is content';
//or
$query = 'content:this is content';

Other Command 其它命令

/**
 * 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 JaegerCode@gmail.com