1. 程式人生 > >安裝elasticsearch的php類庫

安裝elasticsearch的php類庫

單獨安裝elasticsearch的php類庫

composer require elasticsearch/elasticsearch

使用類庫:

require_once(  './vendor/autoload.php');
$esclient = Elasticsearch\ClientBuilder::create()
              ->setHosts(["192.168.8.115:9200"])
              ->build();

**

laravel中安裝elasticsearch

**
進入laravel框架後,執行:

composer require elasticsearch/elasticsearch

1.在建立的方法中,使用如下程式碼:

use Elasticsearch\ClientBuilder;
$esclient = ClientBuilder::create()
              ->setHosts(["192.168.8.115:9200"])    //IP:埠號
              ->build();

2.建立索引

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => '10',
    'body' => [
		'testField' => 'abc',
		'title'	=> '你好,從縱向即文件這個維度來看,每列代表文件包含了哪些單詞,。'
		]
];
$res=$esclient->index($params);
echo '<pre/>';
print_r($res);

3.針對資料庫中某一條記錄建立/更新索引
讀取到資料庫的資料為 d a t a

data。 假設某條記錄的值是 data[0] 並且是 物件格式 可以使用如下程式碼 如果是陣列只需要修 $data[0]->id為 $data[0][‘id’]。

  $params = [
          'index' => 'my_index_user',
          'type' => 'my_type_user',
          'id' => $data[0]->id,
          ];
          unset($data[0]->id);
          foreach ($data[0] as $k => $v){
              $params['body'][$k] = $v;
          }
           $res=$esclient->index($params);
            print_r($res);

3-1.讀取所有資料,迴圈構建陣列並且在迴圈中請求

  foreach($data as $kk=>$vv){
            $params = [
                'index' => 'my_index_user',
                'type' => 'my_type_user',
                'id' => $data[$kk]->id,
            ];
            unset($data[$kk]->id);
            foreach ($data[$kk] as $k => $v){
                $params['body'][$k] = $v;
            }
            $res=$esclient->index($params);
            print_r($res);
        }

4.批量索引

 $params = [
            'index' => 'my_indexa',
            'type' => 'my_typea',
            'body' => [
                ['index' => [ '_id' => 1]],
                [
                    'testField' => 'abc',
                    'title' => '你好,從縱向即文件這個維度來看,每列代表文件包含了哪些單詞,。',
                    'content' => '中國非常強大的哈哈哈,不錯,及時這個時代的步伐,研究紅色呢過名生命科學'
                ],
                ['index' => [ '_id' => 2]],
                [
                    'testField' => '青山綠水',
                    'title' => '無名英雄',
                    'content' => '力量是非常強大'
                ],
                ['index' => [ '_id' => 3]],
                [
                    'testField' => 'abc',
                    'title' => '程式碼的力量',
                    'content' => '天天上學'
                ]
            ]
        ];

$res=$esclient->bulk($params);
print_r($res);

4-1.讀取所有資料,迴圈構建一個大陣列,最後請求介面一次

 $params = [
                'index' => 'my_index_user1',
                'type' => 'my_type_user1'
            ];
        $addpar=[];
        foreach($data as $key=>$val){
            $params['body'][]= ['index' => [ '_id' => $val->id]];
            unset($val->id);
            foreach($val as $k=>$v){
                $addpar[$k]=$v;
            }
            $params['body'][]=$addpar;
        }
        $res = $esclient->bulk($params);
        print_r($res);

5.獲取和刪除

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => '10',
];
 $esclient->get($params);
 $esclient->delete($params);

6.單欄位檢索

$search_params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body'=> [
		'query'	=> [
			'match'	=> [
				'testField'	=> 'abc'
				
			]
		]
	]
];  
$res=$esclient->search($search_params);
print_r($res);

7.多欄位檢索

$search_params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body'=> [
		'query'	=> [
			'bool'	=> [
				'should'	=> [
					['match'	=> ['testField'	=> 'abc']],
					['match'	=> ['title'	=> '中']]
				]
			]
			
		]

	]
    
];
$res=$esclient->search($search_params);
print_r($res);