PHP code example of liuwave / think-elastic

1. Go to this page and download the library: Download liuwave/think-elastic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liuwave / think-elastic example snippets



use think\facade\Env;
return [
  'default' => [
      // 集群连接
    'hosts' => explode(',', Env::get('elasticsearch.hosts', 'localhost:9200')),
      // 重试次数
    'retries' => 0,
      // 公共CA证书
    'SSLVerification' => null,
      // 开启日志
    'logger' => null,
      // 配置 HTTP Handler
    'handler' => null,
      // 设置连接池
    'connectionPool' => Elasticsearch\ConnectionPool\StaticNoPingConnectionPool::class,
      // 设置选择器
    'selector' => Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector::class,
      // 设置序列化器
    'serializer' => Elasticsearch\Serializers\SmartSerializer::class
  ]
];

use think\elastic\facade\Elastic;

$response = Elastic::client()->index([
    'index' => 'test',
    'id' => 'test',
    'body' => [
        'value' => 1
    ]
]);

// ^ array:8 [▼
//   "_index" => "test"
//   "_type" => "_doc"
//   "_id" => "test"
//   "_version" => 1
//   "result" => "created"
//   "_shards" => array:3 [▼
//     "total" => 2
//     "successful" => 1
//     "failed" => 0
//   ]
//   "_seq_no" => 0
//   "_primary_term" => 1
// ]

use think\elastic\facade\Elastic;

$response = Elastic::client()->get([
    'index' => 'test',
    'id' => 'test'
]);

// ^ array:8 [▼
//   "_index" => "test"
//   "_type" => "_doc"
//   "_id" => "test"
//   "_version" => 1
//   "_seq_no" => 0
//   "_primary_term" => 1
//   "found" => true
//   "_source" => array:1 [▼
//     "value" => 1
//   ]
// ]

use think\elastic\facade\Elastic;

$response = Elastic::client()->search([
    'index' => 'test',
    'body' => [
        'query' => [
            'match' => [
                'value' => 1
            ]
        ]
    ]
]);

// ^ array:4 [▼
//   "took" => 4
//   "timed_out" => false
//   "_shards" => array:4 [▼
//     "total" => 1
//     "successful" => 1
//     "skipped" => 0
//     "failed" => 0
//   ]
//   "hits" => array:3 [▼
//     "total" => array:2 [▼
//       "value" => 1
//       "relation" => "eq"
//     ]
//     "max_score" => 1.0
//     "hits" => array:1 [▼
//       0 => array:5 [▼
//         "_index" => "test"
//         "_type" => "_doc"
//         "_id" => "test"
//         "_score" => 1.0
//         "_source" => array:1 [▼
//           "value" => 1
//         ]
//       ]
//     ]
//   ]
// ]

use think\elastic\facade\Elastic;

$response = Elastic::client()->delete([
    'index' => 'test',
    'id' => 'test'
]);

// ^ array:8 [▼
//   "_index" => "test"
//   "_type" => "_doc"
//   "_id" => "test"
//   "_version" => 2
//   "result" => "deleted"
//   "_shards" => array:3 [▼
//     "total" => 2
//     "successful" => 1
//     "failed" => 0
//   ]
//   "_seq_no" => 1
//   "_primary_term" => 1
// ]

use think\elastic\facade\Elastic;

$response = Elastic::client()->indices()->delete([
    'index' => 'test',
]);

// ^ array:1 [▼
//   "acknowledged" => true
// ]

use think\elastic\facade\Elastic;

$response = Elastic::client()->indices()->create([
    'index' => 'test'
]);

// ^ array:3 [▼
//   "acknowledged" => true
//   "shards_acknowledged" => true
//   "index" => "test"
// ]