es全文索引 关键字高亮

其实就是用composer安装es的扩展 ,然后调用使用就可以了

 

1.在wwwroot/default下面新建一个文件夹

2.在文件夹的里面写一个文件名称为 composer.json   在里面写下面的内容

   {
    "require": {
        "elasticsearch/elasticsearch": "~6.0"
    }
}

3.执行命令:

curl -s http://getcomposer.org/installer | php

php composer.phar install --no-dev

4:  再vim  index.php  里面写

require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();

然后打印   print_r($client);

es全文索引 关键字高亮_第1张图片

然在往 index.php里添加后面

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                '要搜索的字段' => '搜索的内容'
            ]
        ],
        'highlight'=>[
         'pre_tags'=>[""],
         'post_tags'=>[""],
       'fields'=>[
             '要搜索的字段'=>new \stdClass()
        ]
      ] 

    ]
];

$response = $client->search($params);
print_r($response);

像这样

es全文索引 关键字高亮_第2张图片

然后访问index.php   就会出现这样的效果

 

 

官方文档链接:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html#_quickstart

你可能感兴趣的:(es全文索引 关键字高亮)