zoukankan      html  css  js  c++  java
  • 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'];
     
  • 相关阅读:
    2020年12月2日
    2020年12月1日
    2020年11月30日
    2020年11月29日
    2020年11月28日
    2020年11月27日
    2020年11月26日
    2020年11月25日
    浅谈扩展欧几里得算法
    Hello 2020
  • 原文地址:https://www.cnblogs.com/ithubb/p/13265562.html
Copyright © 2011-2022 走看看