php 操作Elasticsearch6版本带有type的教程

1.用composer下载elasticsearch

	es库地址:https://packagist.org/packages/elasticsearch/elasticsearch

2.连接es

use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';
//code...
$host = array(
                '127.0.0.1:9200',
                '127.0.0.1:9201',
                '127.0.0.1:9202',
);
$esClient = ClientBuilder::create()->setHosts($host)->build();

3.创建es索引以及type

$params = [
            'index'=>'article_index'
];
$response = $esClient->indices()->create($params);
$params = [
            'index' => 'article_index',
            'type' => 'article_content',
            'body' => [
                'article_content' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'article_content_id' => [
                            'type' => 'integer' //字段类型
                        ],
                        'article_category_id' => [
                            'type' => 'text',
                            'analyzer' => 'keyword', //分词设置
                        ],
                        'article_category_id_arr'=>[
                            'type' => 'nested'
                        ],
                        'article_subject_id'=>[
                            'type'=>'text',
                            'analyzer' => 'keyword',
                        ],
                        'article_subject_id_arr'=>[
                            'type'=>'nested'
                        ],
                        'content_order' => [
                            'type' => 'integer',
                        ],
                        'content_is_image' => [
                            'type' => 'integer'
                        ],
                        'content_is_video'=>[
                            'type'=>'integer'
                        ],
                        'content_focus_image'=>[
                            'type'=>'integer'
                        ],
                        'content_title_image'=>[
                            'type'=>'integer'
                        ],
                        'content_time_publish'=>[
                            'type'=>'integer'
                        ],
                        'content_count_access'=>[
                            'type'=>'integer',
                        ],
                        'content_thief' =>[
                            'type'=>'integer',
                        ]
                    ]
                ]
            ]
        ];
$response = $esClient->indices()->putMapping($params); 

4.增删改查操作

//添加一条记录
$params = [
            'index'=>'article_index',
            'type'=>'article_content',
            'body'=>[
                'id'=>'1',
                'article_content_id'=>1,
                'article_category_id'=>'00100400g008000000000000000000000000000000000',
                'article_category_id'=>[
                    '00100400g008000000000000000000000000000000000',
                    '001003001008000000000000000000000000000000000',
                    '001002001008000000000000000000000000000000000'
                ],
                'content_order' => 10,
                'content_is_image' => 0,
                'content_is_video' => 0,
                'content_focus_image' => 0,
                'content_title_image' => 0,
                'content_time_publish' => 1582710322,
                'content_count_access' => 50,
                'content_thief' => 0,
    
            ]
];
$response = $esClient->index($params);
//删除一条记录
$params = [
            'index'=>'article_index',
            'type' =>'article_content',
            'body' => [
                'query'=>[
                    'match'=>[
                        'article_content_id'=>1,
                    ]
                ]
            ],
];
$response = $esClient->deleteByQuery($params);
//查找记录
$condition = [
    'article_category_id' =>['00100400g008000000000000000000000000000000000']
    'has_image' => 1,
    'title_image' => 0,
    'focus_image' => 1,
    'flv_video' => 0,
    'content_thief' => 0,
]
$where = [
            'match'=>[
                'article_category_id'=>$condition['article_category_id'][0],
            ]
];
$notWhere = [];
$query = [
            'bool'=>[
                    'must'=>$where,
                    'must_not'=>$notWhere,
                    'filter'=>[]
            ],
];
$condition['has_image'] ? array_push($query['bool']['filter'],['term'=>['content_is_image'=>1]]):"";
$condition['title_image'] ? array_push($query['bool']['filter'],['term'=>['content_title_image'=>1]]):"";
$condition['focus_image'] ? array_push($query['bool']['filter'],['term'=>['content_focus_image'=>1]]):"";
$condition['flv_video'] ? array_push($query['bool']['filter'],['term'=>['content_is_video'=>1]]):"";
$condition['content_thief'] ? array_push($query['bool']['filter'],['term'=>['content_thief'=>1]]):"";
$params = [
    'index' => 'article_index', //索引名称
    'type' =>  'article_content', //type名称
    '_source_include'=> 'article_content_id', //返回指定字段
    'body' =>[
        'from'=>0, //从第几条开始
        'size'=>2, //条数
        'query'=>$query,//查询语句
    ]
];
$response = $esClient->search($params);
if ($response){
    $data = [];
    foreach($response['hits']['hits'] as $k => $v){
        $data[] = $v['_source'];
    }
}
var_dump($data)
//更新操作
$where = [
            'match'=>[
                'article_content_id'=>1,
            ]
];
$params = [
        'index' => 'article_index',
        'type' => 'article_content',
        'body' => [
            'query' => [
                'bool' => [
                    'must' => $where
                ],
            ],
            'script'=>[
            //将content_title_long字段更新为params里传入的值
                "inline"=> "ctx._source.content_title_long=params.content_title_long",
            //这里可以实现某个字段自增等需求,比如: 
            // "inline"=> "ctx._source.online_time= ctx._source.online_time params.online_time",
                'params'=>['content_title_long'=>"更新测试标题"],
                'lang'=>'painless'
 
            ]
        ]
  ];
  $response= $esClient->updateByQuery($params);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值