es的基本 *** 作(创建索引,添加数据,删除数据,判断索引是否存在)

es的基本 *** 作(创建索引,添加数据,删除数据,判断索引是否存在),第1张

es的基本 *** 作(创建索引,添加数据,删除数据,判断索引是否存在)

1、创建索引+ik分词

   
    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、添加数据

     
    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、删除索引

    
    public function esDelIndex($indexName)
    {
        $params = ['index' => $indexName];
        return $this->esClient->indices()->delete($params);
    }

4、es搜索+ik分词器 高亮显示

     
    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' => [""],//样式自己写
                    'post_tags' => [""],
                    '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;

    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5573131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存