2016-06-08 248 views
3

這是文檔, https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_quickstart.html關於使用elasticsearch-PHP的

的問題是在下面的截圖: enter image description here enter image description here

編輯:

例如,我想在下面的示例中獲取搜索結果,如何編寫控制器?

視圖:

<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' 
    ]; 
} 

回答

2

我看來,像他們只提供了關於如何安裝作曲家的指示,但你仍然需要使用實際需要的包裝與作曲家:

composer require elasticsearch/elasticsearch 

如果你運行的作曲家安裝應該自動載入了你,讓你可以從任何地方撥打它。從這一點開始,您可以在需要的地方實例化Elasticsearch clientbuilder。

「左邊的代碼」是你得到它的返回響應是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); 

以上應該是相當多的,你需要什麼,你需要改變PARAMS到無論你的設置是與機體的搜索查詢是什麼。

編輯

極目遠眺composer.json編輯,因此您的實際需要需要作曲家需要,因爲它已經在文件中。只需「作曲家安裝」就足夠了。

這是我很快鞭打的,我試過索引方法,它工作正常。在這種情況下,我仍然有一個索引名稱爲「node」並且類型爲「休假」的elasticsearch服務器,這需要根據您的個人elasticsearch服務器進行更改。這裏合乎邏輯的事情是有一種類型的「用戶」的課程。

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文件有更新,刪除,索引和搜索文檔整齊所以只執行那些爲每個資源法的所有設置。

如果你想要做到這一點,並且整齊地實施,還有很大的改進空間。但這至少應該讓你走。一個更好的選擇是爲Elasticsearch客戶端構建器創建一個服務提供程序,並通過typehinting將其注入到您的UsersController中,但我會將其留給您。

祝你好運。

+1

非常感謝!我添加了一些代碼,可以請給我一個控制器的演示嗎? – zwl1619

+0

目前無法嘗試,但我會嘗試使用elasticsearch在今晚進行安裝,以便我可以爲您提供一些代碼。 –

+1

再次感謝!!!! – zwl1619