PHP实现ElasticSearch

<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$hosts = [
    'http://elastic:123456@localhost:9200',       // HTTP Basic Authentication
    //'http://user2:pass2@other-host.com:9200' // Different credentials on different host
];
$client = ClientBuilder::create()->setHosts($hosts)->build();

$PRODUCT_PRE = 'product_';
//创建索引
$params = [
    'index' => 'product_index',
    'type' => 'product_type',
    'id' => $PRODUCT_PRE . 6,
    'body' => [
        'product_desc' => 'cowboy is very busy,because he is a superman', //如果关键词要对产品详情和产品名称进行搜索。可以考虑将产品详情和产品名称拼接一起,再写进es
        'product_name' => 'hello cowBoy'
    ]
];

$response = $client->index($params);//如果id不存在新增,存在则更新
print_r($response);
//http://localhost:9200/product_index/product_type/product_6 => 根据id获取数据


//搜索
$params = [
    'index' => 'product_index',
    'type' => 'product_type',
    'body' => [
        'query' => [
            'match' => [
                'product_desc' => 'cowboy',
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值