【问题标题】:About the using of elasticsearch-php关于elasticsearch-php的使用
【发布时间】:2016-06-08 15:27:24
【问题描述】:

这是文档, https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html

问题在下面的截图中:

编辑:

比如我想得到下面这个例子的搜索结果,控制器怎么写?

查看:

<html>
<head>
    <meta charset="utf-8">
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/tether/1.3.2/css/tether.min.css" rel="stylesheet">
</head>
<body>

<div class="container">
    <nav class="navbar navbar-light bg-faded">
        <a class="navbar-brand" href="#">Navbar</a>
        <ul class="nav navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Features</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Pricing</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">About</a>
            </li>
        </ul>
        <form class="form-inline pull-xs-right">
            <input class="form-control" type="text" placeholder="Search">
            <button class="btn btn-success-outline" type="submit">Search</button>
        </form>
    </nav>
</div>


<script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/tether/1.3.2/js/tether.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
</body>
</html>

路线:

<?php

Route::group(['middleware' => 'web'], function () {

    Route::resource('/search', 'SearchController');

});

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class SearchController extends Controller
{

    public function index()
    {
        //
    }


    public function create()
    {
        //
    }


    public function store(Request $request)
    {
        //
    }


    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        //
    }


    public function update(Request $request, $id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }
}

型号:Article.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable = [
    'title', 'content'
    ];
}

【问题讨论】:

    标签: php laravel elasticsearch


    【解决方案1】:

    在我看来,他们只提供了有关如何安装作曲家的说明,但您仍然需要使用作曲家实际要求该软件包:

    composer require elasticsearch/elasticsearch
    

    如果您运行 composer install 它应该会为您自动加载,以便您可以从任何地方调用它。从那时起,您可以在需要的地方实例化 Elasticsearch 客户端构建器。

    “左边的代码”是您得到的返回响应,它是将elasticsearch json转换为php数组。

    要真正启动并运行,您需要:

    $client = Elasticsearch\ClientBuilder::create()->build();
    
    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id',
        'body' => ['testField' => 'abc']
    ];
    
    $response = $client->index($params);
    print_r($response);
    

    以上内容应该是您所需要的,您需要将参数更改为您的设置,并将正文更改为您的搜索查询。

    编辑

    查看了 composer.json 编辑,因此您实际上需要 composer require,因为它已经在文件中。只需“composer install”就足够了。

    这是我快速完成的,我尝试了 index 方法,它工作正常。在这种情况下,我仍然有一个索引名称为“节点”和“假期”类型的弹性搜索服务器,这需要根据您的个人弹性搜索服务器进行更改。合乎逻辑的事情当然是拥有一种“用户”。

    class UsersController extends Controller
    {
        // the main elasticsearch instance created with the constructor
        protected $client;
    
        public function __construct() {
            $hosts = [
                // since I am using homestead I have to refer to the ip address of my host machine on which I have installed
                // elasticcsearch, otherwise the default localhost option will point to homestead localhost
                '192.168.178.10:9200'
            ];
    
            $this->client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
        }
    
        public function index()
        {
            $params = [
                'index' => 'node',
                'type' => 'vacation',
                'body' => [
                    'query' => [
                        'match_all' => []
                    ]
                ]
            ];
    
            $response = $this->client->search($params);
            print_r($response);
        }
    
        public function create()
        {
            $params = [
                'index' => 'node',
                'type' => 'vacation',
                'id' => '1029',
                'body' => [
                    'query' => [
                        'match_all' => []
                    ]
                ]
            ];
    
            $response = $this->client->index($params);
            print_r($response);
        }
    }
    

    elasticsearch 文档对更新、删除、索引和搜索的所有设置进行了整齐的记录,因此只需为每个资源方法实现这些设置。

    如果你想以 laravel 的方式做这件事并巧妙地实现它,还有很大的改进空间。但这至少应该让你继续前进。更好的选择是为 Elasticsearch 客户端构建器创建一个服务提供者,并通过类型提示将其注入到您的 UsersController 中,但我将由您决定。

    祝你好运。

    【讨论】:

    • 非常感谢!我添加了一些代码,你能给我一个控制器的演示吗?
    • 目前无法尝试,但我今晚会尝试使用 elasticsearch 进行安装,以便为您提供一些代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2011-04-08
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多