php+elasticsearch6.x的基本操作(创建索引、添加数据、删除数据、高亮搜索)

1、创建索引+ik分词器

   /**
     * 创建es索引
     * $indexName 索引名称
     */
    public function createEsIndex($indexName)
    {
        $esClient = $this->esClient;
        $params = [
            'index' => $indexName,//索引名称
            'body' => [
                'settings' => [ //配置
                    'number_of_shards' => 3,//主分片数
                    'number_of_replicas' => 2//主分片的副本数
                ],
                'mappings' => [//映射
                    '_source' => [//  存储原始文档
                        'enabled' => true
                    ],
                    'properties' => [//配置数据结构与类型
                        'name' => [//字段1
                            'type' => 'text',//类型 string、integer、float、double、boolean、date
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                 ]
            ]
        ];
        // Create the index with mappings and settings now
        $response = $esClient->indices()->create($params);
 
        return $response;
    }

2、添加数据

 /**
     * es数据的添加
     * $indexName 索引名称
     * $addData 添加的数据
     */
    public function esDataAdd($indexName,$addData)
    {
        $esClient = $this->esClient;
        //判断es索引是否存在
        $getIndexExistRes = $this->esIndexExist($indexName);
        if(!$getIndexExistRes){//不存在
            //创建es索引
            $this->createEsIndex($indexName);
        }
        //参数
        $params = [
            'index' => $indexName,//索引名称
            'type' => '_doc',
            'id' => $addData['id'],
            'body' =>$addData
        ];
 
        $addResponse = $esClient->index($params);
        return $addResponse;
    }

3、删除索引

 /**删除索引
     * @return array
     * $indexName 索引名称
     */
    public function esDelIndex($indexName)
    {
        $params = ['index' => $indexName];
        return $this->esClient->indices()->delete($params);
    }

4、ik分词器+高亮搜索

 /**
     * 搜索索引数据
     * $indexName 索引名称
     * $searchWord 搜索的词
     */
    public function searchEsData($indexName, $searchWord)
    {
        //判断es索引是否存在
        $getIndexExistRes = $this->esIndexExist($indexName);
        if (!$getIndexExistRes) {//不存在
            //创建es索引
            return '索引不存在';
        }
        //设置查询的条件
        $params = [
            'index' => $indexName,//索引(类似于库)
            'type' => '_doc',
            'body' => [
                //查询内容
                'query' => [
                    'match' => [//匹配
                        'house_address' => $searchWord//匹配字段
                    ]
                ],
                'highlight' => [//高亮
                    'pre_tags' => ["<em style='color: red'>"],//样式自己写
                    'post_tags' => ["</em>"],
                    'fields' => [
                        "house_address" => new \stdClass()
                    ]
                ]
            ]
        ];
 
        $results = $this->esClient->search($params);//es搜索
        //处理高亮显示
        foreach ($results['hits']['hits'] as $k => $v) {
            $results['hits']['hits'][$k]['_source']['goods_name'] = $v['highlight']['goods_name'][0];
        }
        //获取数据 从二维数组中取出其中一列
        $getSearchData = array_column($results['hits']['hits'], '_source');
 
        return $getSearchData;
 
    }

5、判断索引是否存在

/**判断索引是否存在

* @return bool

*/

public function exist_index()

{

    $params = ['index' => $this->index_name];//索引名称

    return $this->client->indices()->exists($params);

}

6、获取索引结构 

/**获取索引结构

* @return array

*/

public function get_index()

{

    $params = ['index' => $this->index_name];//索引名称

    return $this->client->indices()->get($params);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值