php 操作 elasticsearch

安装插件
composer elasticsearch/elasticsearch

引用

use Elasticsearch\ClientBuilder;
public function esinit()
    {
        //建立es连接
        $client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();

        $params = [
            'index' => 'fang', #index的名字不能是大写和下划线开头
            'body' => [
                'settings' => [
                    'number_of_shards' => 2,
                    'number_of_replicas' => 0
                ],
                /*'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'fang_name' => [
                            'type' => 'text',
                            //'analyzer' => 'ik_max_word'
                        ],
                        'fang_xiaoqu' => [
                            'type' => 'text',
                            //'analyzer' => 'ik_max_word'
                        ]

                    ]
                ]*/
            ]
        ];

        $res = $client->indices()->create($params);

        dd($res);
    }

    public function esindex()
    {

        $client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();

        $params = [
            'index' => 'fang',
            'type' => '_doc',
            'include_type_name' => true,
            'body' => [
                '_doc' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'fang_name' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word'
                        ],
                        'fang_xiaoqu' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word'
                        ],
                        'fang_desn' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word'
                        ]
                    ]
                ]
            ]
        ];

        $res = $client->indices()->putMapping($params);
        dd($res);
    }


    public function esdoc()
    {
        $client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();

        $params = [
            'index' => 'fangs',
            'type' => '_doc',
            //'id' => 100, #可以手动指定id,也可以不指定随机生成
            'body' => [
                'fang_name' => '兰香湖壹号',
                'fang_xiaoqu' => '闵行区兰香湖西路168弄',
                'fang_desn' => '项目价格调整,2022年09月06日为2050.0万/套,最新价格为2200.0万/套'
            ]
        ];

        $res = $client->index($params);
        dd($res);
    }

    public function essearch()
    {
        $client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();


        $query = [
            'query' => [
                'bool' => [
                    'must' => [
                        'match' => [
                            'fang_name' => '中国'
                        ],
                        'match' => [
                            'fang_xiaoqu' => '向上'
                        ]
                    ]
                ]

            ],

            /*'query' => [
                'match_phrase' => [
                    'fang_name' => [
                        'query' => '底色锃亮',
                        //'slop' => 1
                    ]
                ]
            ],*/

            /* 'query' => [
                 'match' => [
                     'fang_name' => [
                         'query' => '中国网络文明大会'
                     ]
                 ]
             ],*/

            /*'query' => [
                'multi_match' => [
                    'query' => '中国网络文明大会',
                    'fields' => [
                        'fang_name',
                        'fang_xiaoqu'
                    ]
                ]
            ],


            'highlight' => [
                'pre_tags' => ["<em>"],
                'post_tags' => ["</em>"],
                'fields' => [
                    "fang_name" => new \stdClass()
                ]
            ]*/
        ];
        $params = [
            'index' => 'fangs',
            // 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至这两个参数都是可选的
            'type' => '_doc',
            //'_source' => ['fang_name','fang_xiaoqu', 'fang_desn'], // 请求指定的字段
            'body' => array_merge([
                'from' => 0,
                'size' => 5
            ], $query)
        ];

        $res = $client->search($params);

        print_r($res);

    }


    public function delDoc()
    {

        $id = 'U2szN30BFnSrfszRzPqr';
        $client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
        $params = [
            'index' => 'fangs',
            'type' => '_doc',
            'id' => $id
        ];
        $res =  $client->delete($params);

        dd($res);
    }

    public function updateDoc() {

        $client = ClientBuilder::create()->setHosts([config('setting.es.host')])->build();
        $id = 'VGs4N30BFnSrfszRivo5';
        $params = [
            'index' => 'fangs',
            'type' => '_doc',
            'id' =>$id,
            'body' => [
                'doc' => [
                    'fang_name' => '地上人间',
                    'fang_xiaoqu' => '五四公路3389号'
                ]
            ]
        ];
        $res = $client->update($params);
        dd($res);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值