PHP实现ElasticSearch

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

use Elasticsearch\ClientBuilder;

$hosts = [
    'http://elastic:123456@localhost:9200',       // HTTP Basic Authentication
    //'http://user2:[email protected]: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);

你可能感兴趣的:(elasticsearch,PHP,elasticsearch,php,大数据)