基于Thinkphp3导入数据mysql数据到Elasticsearch中

  1. 准备mysql数据

    

  2导入数据代码

use Elasticsearch\ClientBuilder;
class ElasticsearchController extends Controller
{    private $client;
    // 构造函数
    public function __construct()
    {
        $params = array('127.0.0.1:9200');
        $this->client = ClientBuilder::create()->setHosts($params)->build();
        return $this->client;
    }
    // 创建索引
    public function create_index($index_name = 'haodanku')
    { // 只能创建一次
        $indexParams['index']  = $index_name;
        //$indexParams['type']  = "items";
        $indexParams['body']['settings']['number_of_shards']   = 3;
        $indexParams['body']['settings']['number_of_replicas'] = 2;
        $myTypeMapping = array(
            '_source' => array(
                'enabled' => true
            ),
            'properties' => array(
                'id' => array(
                    'type' => 'integer',
                ),
                'itemid' => array(
                    'type' => 'long',
                ),
                'itemtitle' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'itemshorttitle' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'itemdesc' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'itemprice' => array(
                    'type' => 'double',
                ),
                'itemsale' => array(
                    'type' => 'integer',
                ),
                'itemsale1' => array(
                    'type' => 'integer',
                ),
                'itemsale2' => array(
                    'type' => 'integer',
                ),
                'todaysale' => array(
                    'type' => 'integer',
                ),
                'todaystartsale' => array(
                    'type' => 'integer',
                ),
                'fqcat' => array(
                    'type' => 'integer',
                ),
                'itemendprice' => array(
                    'type' => 'double',
                ),
                'shoptype' => array(
                    'type' => 'keyword',
                ),
                'shoprank' => array(
                    'type' => 'keyword',
                ),
                'shopid' => array(
                    'type' => 'long',
                ),
                'shopname' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'sellernick' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'tkrates' => array(
                    'type' => 'double',
                ),
                'tkmoney' => array(
                    'type' => 'double',
                ),
                'couponmoney' => array(
                    'type' => 'integer',
                ),
                'item_status' => array(
                    'type' => 'integer',
                ),
                'is_brand' => array(
                    'type' => 'keyword',
                ),
                'is_hdk_item' => array(
                    'type' => 'keyword',
                ),
                'activity_type' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'general_index' => array(
                    'type' => 'integer',
                ),
                'grade_avg' => array(
                    'type' => 'double',
                ),
                'brand_name' => array(
                    'type' => 'text',
                    'analyzer' => 'ik_max_word'
                ),
                'is_shipping' => array(
                    'type' => 'keyword',
                ),
                'is_flagship_store' => array(
                    'type' => 'keyword',
                ),
                'tb_brandid' => array(
                    'type' => 'long',
                ),
            )
        );
        $indexParams['body']['mappings'] = $myTypeMapping;
        try {
           return $this->client->indices()->create($indexParams);
        } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg, true);
            return $msg;
        }
    }
    // 删除索引
    public function delete_index($index_name = 'haodanku')
    {
        $params = ['index' => $index_name];
        $response = $this->client->indices()->delete($params);
        dump($response);
        return $response;

    }
    //查看映射
    public function get_mapping($type_name = 'items', $index_name = 'haodanku')
    {
        $params = [
            'index' => $index_name,
        ];
        $response = $this->client->indices()->getMapping($params);
        print_r($response);
        return $response;
    }
    //导入数据
    public function  post_bulk(){
        set_time_limit(0);
        header("Content-type: text/html; charset=utf-8");
        $M_item =M('items')->order("id desc")->limit(100)->select();
        /***********连接数据库****end******/
        //提取数据
        $cl      = curl_init();
        curl_setopt($cl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($cl, CURLOPT_PORT, 9200);
        curl_setopt($cl, CURLOPT_TIMEOUT, 2000);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cl, CURLOPT_FORBID_REUSE, 0);
        curl_setopt($cl, CURLOPT_CUSTOMREQUEST, 'PUT'); //GET 获取 // DELETE 删除 //PUT 插入
        if ($M_item) {
            foreach ($M_item as $key=>&$value){
                $jsonStr            = json_encode($value, JSON_UNESCAPED_UNICODE);
                //修改二,设置es导入
                $baseUri = 'http://localhost:9200/haodanku/_doc/' . $value['id'];
                curl_setopt($cl, CURLOPT_URL, $baseUri);
                curl_setopt($cl, CURLOPT_POSTFIELDS, $jsonStr);
                $response = curl_exec($cl);
            }
        }
        unset($M_item);//销毁数组
        file_put_contents('log.log',$response);
        curl_close($cl); usleep(50000);//自定义延迟
    }
    //写入1条数据
    public function  post_create(){
        set_time_limit(0);
        header("Content-type: text/html; charset=utf-8");
        $items_id = I("id")?I("id"):"17068799";
        $M_item_find =M('items')->field('id,itemtitle')->where(array('id'=>$items_id))->order("id desc")->limit(1000)->find();
        if(empty($M_item)){
            echo "商品信息不存在";
        }
        //提取数据
        $cl      = curl_init();
        curl_setopt($cl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($cl, CURLOPT_PORT, 9200);
        curl_setopt($cl, CURLOPT_TIMEOUT, 2000);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cl, CURLOPT_FORBID_REUSE, 0);
        curl_setopt($cl, CURLOPT_CUSTOMREQUEST, 'PUT'); //GET 获取 // DELETE 删除 //PUT 插入

        $jsonStr            = json_encode($M_item_find, JSON_UNESCAPED_UNICODE);
        //修改二,设置es导入
        $baseUri = 'http://localhost:9200/haodanku/_doc/' . $M_item_find['id'];
        curl_setopt($cl, CURLOPT_URL, $baseUri);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $jsonStr);
        $response = curl_exec($cl);
        unset($M_item);//销毁数组
        file_put_contents('log.log',$response);
        curl_close($cl); usleep(50000);//自定义延迟
    }
    //修改映射
    public function putMappings(){
        $params = [
            'index' => 'haodanku',
            'type' => 'items',
            'body' => [
                'items' => [
                    'properties' => [
                        'itemtitle' => [
                            'type' => 'text'
                        ]
                    ]
                ]
            ]
    ];

    $response = $this->client->indices()->putMapping($params);
    print_r($response);
}

 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值