2017 © Pedro Peláez
 

library e-search

Elasticsearch5 PHP Api

image

jaeger/e-search

Elasticsearch5 PHP Api

  • Friday, December 9, 2016
  • by jae
  • Repository
  • 1 Watchers
  • 3 Stars
  • 28 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 17 % Grown

The README.md

e-search

Elasticsearch5 PHP Api, (*1)


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

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);
$result = $es->search($query);

$query can be an array,JSON string, or string., (*3)

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, (*4)

The Versions

09/12 2016

dev-master

9999999-dev

Elasticsearch5 PHP Api

  Sources   Download

MIT

The Requires

 

by Jaeger

elasticsearch