elasticsearch6.0 php代码

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

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

composer require elasticsearch/elasticsearch:~6.0

 

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

7.0版本的可以设置密码

参考这篇文章https://blog.csdn.net/weixin_43315211/article/details/99677072

配置文件详解https://www.cnblogs.com/zhiqiangzhang/p/11425473.html

我们这里简单配置即可

vi /etc/elasticsearch/elasticsearch.yml
network.host:0.0.0.0

修改了后重启
任选一个
$hosts = [
    '192.168.1.1:9200',         // IP + Port
    '192.168.1.2',              // Just IP
    'mydomain.server.com:9201', // Domain + Port
    'mydomain2.server.com',     // Just Domain
    'https://localhost',        // SSL to localhost
    'https://192.168.1.3:9200'  // SSL to IP + Port
];
//创建一个连接
$client = 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

初始化操作
//创建索引

route:get('/init',function(){
    $hosts = [
    '192.168.1.1:9200',         // IP + Port
    '192.168.1.2',              // Just IP
    'mydomain.server.com:9201', // Domain + Port
    'mydomain2.server.com',     // Just Domain
    'https://localhost',        // SSL to localhost
    'https://192.168.1.3:9200'  // SSL to IP + Port
];
//创建一个连接
$client = ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build(); 
$params = [
    'index' => 'my_index',//索引名
    'body' => [
        'settings' => [
            'number_of_shards' => 1,//number_of_shards  是数据分片数,默认为5,有时候设置为3
            'number_of_replicas' => 0//number_of_replicas 是数据备份数,如果只有一台机器,设置为0
        ],
        'mappings' => [
            'my_type' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    //字段
                    'title' => [
                        'type' => 'text',
                    //分词器模式
                        'analyzer' => 'ik_max_word'
                    ],
                    'desc' => [
                        //'type' => 'integer'
                        'type' => 'text',
                        'analyzer' => 'ik_max_word'
                    ]
                ]
            ]
        ]
    ]
];


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

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

添加文档
$hosts = [
                '192.168.0.89:9200',         // IP + Port
            ];
            $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
            ->setHosts($hosts)      // Set the hosts
            ->build();
            $params = [
                'index' => 'article',
                'type' => '_doc',
                'id' => $article->id,//唯一的id
                'body' => [
                    'title' => $article->title,//内容
                    'desc' => $article->content//内容
                ]
            ];
            // Document will be indexed to my_index/my_type/my_id
            $response = $client->index($params);
修改
$hosts = [
                '192.168.0.89:9200',         // IP + Port
            ];
            $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
            ->setHosts($hosts)      // Set the hosts
            ->build();
            $params = [
                'index' => 'article',
                'type' => '_doc',
                'id' => $id,
                'body' => [
                    'title' => $request->input('title'),
                    'desc' => $request->input('content')
                ]
            ];
            // 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 = [];
            if ($request->title){
                $where["match_phrase"] = ['title' =>[
                    "query"=> $request->title,
                    "slop"=>100
                     ]];
            }
            if ($request->desc){
                $where["match_phrase"] = ['title' =>[
                    "query"=> $request->desc,
                    "slop"=>100
                ]];
            }
            //dd($where);
            //单字段查询
            /*$params = [
                'index' => 'article',
                'type' => '_doc',
                'body' => [
                    'query' => [
                        'match_phrase' => $where
                    ]
                ]
            ];*/
            $params = [
                'index' => 'article',
                'type' => '_doc',
                'body' => [
                    'query' => [
                        'bool' => [
                            'must' =>
                                $where
                        ]
                    ],
                    'highlight'=>[
                        "require_field_match"=> false,
                        "fields"=>[
                            "*"=>[
                                "pre_tags"=> [ "<font color='red'>" ],
                                "post_tags"=> [ "</font>" ]
                            ]
                        ]
            ]
                ]
            ];
$hosts = [
                '192.168.0.89:9200',         // IP + Port
            ];
            $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
            ->setHosts($hosts)      // Set the hosts
            ->build();
            $results = $client->search($params);
            dump($results);exit();
-----------------
取id
$ids = [];
foreach($results['hits']['hits'] as $v){
    $ids[] = $v['_id']; 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。我们建立一个网站或应用程序,并要添加搜索功能,但是想要完成搜索工作的创建是非常困难的。我们希望搜索解决方案要运行速度快,我们希望能有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP来索引数据,我们希望我们的搜索服务器始终可用,我们希望能够从一台开始并扩展到数百台,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。因此我们利用Elasticsearch来解决所有这些问题以及可能出现的更多其它问题。ElasticSearchPHP客户端库<?php namespace Elastica; class Index implements SearchableInterface {     protected $_name;     protected $_client;     public function __construct(Client $client, $name)     {         $this->_client = $client;         if (!is_scalar($name)) {             throw new InvalidException('Index name should be a scalar type');         }         $this->_name = (string) $name;     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值