2017-01-06 13 views
1

我希望有人能夠幫助Laravel控制器中的ElasticSearch簡單查詢的語法。如何在Laravel控制器中查詢ElasticSearch

我已經設法索引並輸出到視圖中的刀片模板,但我無法獲得正確的查詢以啓用搜索表單變量來對我的種子數據執行搜索。

搜索從控制器的方法:

public function searchPlugins() { 

    $client = Elasticsearch\ClientBuilder::create()->build(); 

    $query2 = Request::input('query2'); 

    $params = [ 
     'index' => 'partnerpages', 
     'type' => 'plugins', 
     'body' => [ 
      'query' => $query2['query2'] 
     ] 
    ]; 

    $plugins = $client->search($params); 

    return View::make('search2')->with('plugins', $plugins); 
} 

我只是無法在參數數組右邊的查詢 - 我只能得到它的輸出特定領域和關鍵詞。

任何幫助非常感謝,在此先感謝。

編輯

的代碼輸出在我的刀模板的觀點:

<!-- Search engine --> 
      <div class="col-md-8"> 

       {{ Form::open(array('route' => 'search-plugins2', 'class' => 'form')) }} 

       {{ Form::input('search', 'query2', Input::get('query2', ''))}} 
       {{ Form::submit('Search plugins') }} 

       {{ Form:: close() }} 

      </div><!-- end of Search engine --> 

      <div class="col-md-8"> 

       <!-- insert Search engine --> 
       <br/> 
       <h1>Plugin results</h1> 
       <br/> 


       <div class="panel panel-default"> 
        <div class="panel-body"><h2></h2> 
        <div><?php print_r($plugins);?></div> 
        <div></div> 
        <div><small></small></div> 
       </div> 
        </div> 
      </div><!-- end of row --> 

回答

1

您需要使用query DSL構造查詢。你可以開始使用query_string query這樣的:

$params = [ 
    'index' => 'partnerpages', 
    'type' => 'plugins', 
    'body' => [ 
     'query' => [ 
      'query_string' => [ 
       'query' => $query2['query2'] 
      ] 
     ] 
    ] 
]; 
+0

感謝@val,我得到錯誤「非法串偏移‘QUERY2’」我明白意味着我試圖訪問值使用不存在的鍵(即query2)的數組的數組。如果我硬編碼查詢然後罰款。我會從視圖添加代碼,因爲那裏有一個問題? – pfeatherstone

+0

只包含變量'query'=> $ query2工作 - 很棒!再次感謝Val。儘管在foreach循環中輸出數組仍然有問題。也許是因爲它是一個多維陣列,已經訴諸於print_r – pfeatherstone

+0

很高興它幫助! – Val

0

就包括「查詢」的形式=> $ QUERY2爲我工作的變量。再次感謝瓦爾指出我在正確的方向。

編輯

有了更新的代碼:

public function searchPlugins() { 

     $client = Elasticsearch\ClientBuilder::create()->build(); 

     $query2 = Input::get('query2', 'RSS'); 

     $params = [ 
    'index' => 'partnerpages', 
    'type' => 'plugins', 
    'body' => [ 
     'query' => [ 
      'query_string' => [ 
       'query' => $query2 
      ] 
     ] 
    ] 
]; 

$plugins = $client->search($params); 

     return View::make('search2')->with('plugins', $plugins); 
    } 
+0

這不是一個答案。這應該是一個評論。請刪除此內容,並對答案@val提供的評論進行評論 - 或者,將此內容轉爲完整答案,展示您使用的代碼。 –

+0

已添加代碼,請參閱上文 – pfeatherstone