tp5结合es6.x的基本用法

 protected $es;

    public function __construct()
    {
        $params = array(
            '127.0.0.1:9200'
        );
        $this->es = ClientBuilder::create()->setHosts($params)->build();
    }

1.创建索引(分词,拼音,同义词)

  public function setMapping()
    {
        $params = [
            'index' => 'news',
            'body' => [
                'settings' => [
                    'analysis' => [
                        'filter' => [
                            'my_synonym_filter' => [
                                'type' => 'dynamic_synonym',
//                                'synonyms_path' => "analysis/synonym.txt"
                                'synonyms_path' => "http://127.0.0.1/phpproject/tp5es/synonym.txt",
                                "interval" => 30
                            ]
                        ],
                        'analyzer' => [
                            'my_synonyms' => [
                                'tokenizer' => 'ik_max_word',
                                'filter' => [
                                    'lowercase',
                                    'my_synonym_filter'
                                ]
                            ]
                        ]
                    ]
                ],
                'mappings' => [
                    '_doc' => [
                        'properties' => [
                            'name' => [
                                'type' => 'keyword',
                                'fields' => [
                                    'pinyin' => [
                                        'type' => 'text',
                                        'analyzer' => 'pinyin',
                                    ]
                                ]
                            ],
                            'age' => [
                                'type' => 'integer'
                            ],
                            'content' => [
                                'type' => 'text',
                                'analyzer' => 'my_synonyms',
                                'fields' => [
                                    'pinyin' => [
                                        'type' => 'text',
                                        'analyzer' => 'pinyin'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];

        $res = $this->es->indices()->create($params);
        var_dump($res);
    }

2.删除索引

//删除索引
    public function deleteindex()
    {
        $params = [
            'index' => 'test2',
        ];
        $response = $this->es->indices()->delete($params);
        var_dump($response);
    }

3.添加数据(单个添加,批量添加)

 //单个添加
    public function add()
    {
        $params =[
          'index' => 'news',
          'type' => '_doc',
          'id' => 'id_1',
          'body' => [
              'name' => '体育课',
              'age' => 31,
              'content' => '煤化工哦看'
          ]
        ];

        $response = $this->es->index($params);
        var_dump($response);
    }

    //批量添加
    public function addall()
    {
        for($i = 2; $i < 10; $i ++) {
            $params['body'][] = [
                'index' => [
                    '_index' => 'news',
                    '_type' => '_doc',
                    '_id' => 'id_'.$i
     
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值