php 使用elasticsearch

elasticsearch 版本 6.4.0

具体elasticsearch 和kibana 安装不清楚的请查看

 

1.安装类库

https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/installation.html

composer  require elasticsearch/elasticsearch:~6.

2 查看配置

https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/configuration.html

 

3.修改配置文件

/etc/elasticsearch/elasticsearch.yml

vim 打开配置文件

找的network.host :192.168.0.1 改成 0.0.0.0 保存退出

重启elasticsearch

service elasticsearch restart

开启 kibana

 /usr/share/kibana/bin/kibana


做初始化操作创建索引 在 路由设置里写入即可
Route::get('/init',function(){
$hosts = [
    '192.168.1.1:9200'        // IP + Port  elasticsearch 的ip 需要设置默认不对外开放
];
$client = \Elasticsearch\ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();
//https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/index_management.html
$params = [
    'index' => 'product',
    'body' => [
        'settings' => [
            'number_of_shards' => 1,
            'number_of_replicas' => 0
        ],
        'mappings' => [
            'my_type' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'first_name' => [
                        'type' => 'keyword',
                        'analyzer' => 'standard'
                    ],
                    'age' => [
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]
];


// Create the index with mappings and settings now
$response = $client->indices()->create($params);

});

添加修改数据同步到 elasticsearch
https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/indexing_documents.html

$hosts = [
    '192.168.1.1:9200'        // IP + Port  elasticsearch 的ip 需要设置默认不对外开放
];
$client = \Elasticsearch\ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();
//写入文档
$params = [
    'index' => 'product',
    'type' => '_doc',
    'id' => $id,//你添加商品的主键id
    'body' => [ 
      'title' => $shop_title,
      'description' => $shop_description,
   ]
];

// Document will be indexed to my_index/my_type/my_id
$response = $client->index($params);
实现中文分词搜索
https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/search_operations.html

$where['title']=[
  "query"=>$shop_title,
  "slop"=>20
];
$where['description']=[
  "query"=>$shop_description,
  "slop"=>20
];
$params = [
    'index' => 'product',
    'type' => '_doc',
    'body' => [
        'query' => [
            'match_phrase' => $where
        ]
    ]
];

$hosts = [
    '192.168.1.1:9200'        // IP + Port  elasticsearch 的ip 需要设置默认不对外开放
];
$client = \Elasticsearch\ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();
$results = $client->search($params);

//返回前台数据
$products = $results['hits']['hits'];
 
posted @ 2020-07-08 10:47  hubb  阅读(364)  评论(0编辑  收藏  举报