php使用Elasticsearch之批量操作(bulk)

Elasticsearch的批量操作就像 mget 允許我們一次性檢索多個文檔一樣, bulk API允許我們使用單一請求來實現多個文檔的 create 、 index 、 update 或 delete 。這對索引類似於日誌活動這樣的數據流非常有用,它們可以以成百上千的數據爲一個批次按序進行索引。
批量操作的行爲(action)必須是以下幾種:
行爲 解釋
create 當文檔不存在時創建之。
index 創建新文檔或替換已有文檔。
update 局部更新文檔。
delete 刪除一個文檔。
注意:在索引、創建、更新或刪除時必須指定文檔的 _index 、 _type 、 _id 、(如果設置了routing,也必須指定)這些元數據
創建一個例子索引的mapping如下:
$mapping = [
    'index' => 'my_index',
    'body' => [
        'mappings' => [
            'my_type' => [
                '_all' => [
                    'enabled' => 'false'
                ],
                '_routing' => [   #設置routing(分片路由)
                    'required' => 'true'
                ],
                'properties' => [
                    'name' => [
                        'type' => 'string',
                    ],
                    'age' => [
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]
];
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->indices()->create($mapping);
批量執行創建文檔例子
$params['body'] = [];
$nameArr = ['趙','錢','孫','李','周','吳','鄭','王'];
for ($i = 1; $i <= 100; $i++) {
    $params['body'][] = [
        'create' => [   #創建
            '_index' => 'my_index',
            '_type' => 'my_type',
            '_id' => $i,
            '_routing' => mt_rand(1,100),
        ]
    ];
    $params['body'][] = [
        'name' => $nameArr[mt_rand(0,7)],
        'age' => mt_rand(18,60)
    ];
}
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->bulk($params);
批量執行更新文檔操作例子
//查找指定文檔
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
	'index' => 'my_index',
	'type' => 'my_type',
	'body' => [
	    'query' => [
	        'bool' => [
	            'must' => [
	                'range' => [
	                    'age' => [
	                        'gt' => '25',
	                        'lt' => '40'
	                    ]
	                ]
	            ]
	        ]
	    ],
	    'from' => 0,
	    'size' => 100,
	    'sort' => [
	    	'age' => 'desc'
	    ]
	]
];
$info = $client->search($params);
$data = $info['hits']['hits'];

// 批量執行更新文檔
$params['body'] = [];
$nameArr = ['趙','錢','孫','李','周','吳','鄭','王'];
foreach ($data as $key => $value) {
	$params['body'][] = [
        'update' => [   #更新文檔
            '_index' => $value['_index'],
            '_type' => $value['_type'],
            '_id' => $value['_id'],
            '_routing' => $value['_routing'],
        ]
    ];
    $params['body'][] = [
    	'doc' => [  #更新指定的字段值
        	'name' => $nameArr[mt_rand(0,7)].$nameArr[mt_rand(0,7)]
        ]
    ];
}
$res = $client->bulk($params);
增刪改批量執行操作例子
$params['body'] = [];

//創建或替換文檔操作
$params['body'][] = [
    'index' => [   #創建或替換
        '_index' => 'my_index',
        '_type' => 'my_type',
        '_id' => 1,
        '_routing' => 1,
    ]
];
$params['body'][] = [
    'name' => '楊',
    'age' => 23
];

//創建文檔操作
$params['body'][] = [
    'create' => [   #創建
        '_index' => 'my_index',
        '_type' => 'my_type',
        '_id' => 2,
        '_routing' => 2,
    ]
];
$params['body'][] = [
    'name' => '郭',
    'age' => 19
];

//局部更新文檔操作
$params['body'][] = [
    'update' => [   #局部更新
        '_index' => 'my_index',
        '_type' => 'my_type',
        '_id' => 3,
        '_routing' => 3,
    ]
];
$params['body'][] = [
	'doc' => [
    	'age' => 19
    ]
];

//刪除文檔操作
$params['body'][] = [
    'delete' => [   #刪除
        '_index' => 'my_index',
        '_type' => 'my_type',
        '_id' => 4,
        '_routing' => 4,
    ]
];
$client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$res = $client->bulk($params);





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章