2016-09-17 10 views
5

मैं लैरवेल स्काउट और लोचदार खोज के साथ एक खोज एकीकरण का निर्माण कर रहा हूं। मैं यह समझने की कोशिश कर रहा हूं कि मैं अपने प्रश्नों को बढ़ाने के लिए कैसे परिष्कृत कर सकता हूं।लैरवेल स्काउट और लोचदार खोज के साथ आप बूस्ट और फ़िल्टर कैसे सेट कर सकते हैं?

क्या लैरवेल स्काउट के साथ ऐसा करना संभव है या क्या मुझे सीधे लोचदार खोज PHP लाइब्रेरी का उपयोग करने के लिए वापस लौटना होगा?

उत्तर

1

लैरवेल स्काउट उन्नत लोचदार खोज सुविधाओं का समर्थन नहीं करता है। मैं मॉडल इंडेक्स के आधार पर सभी इंडेक्स निर्माण और अपडेटिंग के लिए स्काउट के साथ चिपक गया, लेकिन मैंने अपनी खोज को सीधे लोचदार खोज पुस्तकालय का उपयोग करने के लिए स्विच किया।

3

असल में इसे कस्टम स्काउट इंजन के साथ किया जा सकता है।

की यह ElasticqueryEngine फोन, उदाहरण के लिए, और यह डिफ़ॉल्ट ElasticsearchEngine से विस्तार:

<?php 

namespace App\Libs\Scout\Engines; 

use Laravel\Scout\Builder; 
use Laravel\Scout\Engines\ElasticsearchEngine; 

class ElasticqueryEngine extends ElasticsearchEngine 
{ 
    /** 
    * Perform the given search on the engine. 
    * 
    * @param Builder $query 
    * @param array $options 
    * @return mixed 
    */ 
    protected function performSearch(Builder $query, array $options = []) 
    { 
     if (!is_array($query->query)) { 
      return parent::performSearch($query, $options); 
     } 

     $searchQuery = [ 
      'index' => $this->index, 
      'type' => $query->model->searchableAs(), 
      'body' => [ 
       'query' => $query->query, 
      ], 
     ]; 

     if (array_key_exists('size', $options)) { 
      $searchQuery = array_merge($searchQuery, [ 
       'size' => $options['size'], 
      ]); 
     } 
     if (array_key_exists('from', $options)) { 
      $searchQuery = array_merge($searchQuery, [ 
       'from' => $options['from'], 
      ]); 
     } 
     return $this->elasticsearch->search($searchQuery); 
    } 
} 

नई ElasticqueryEngine रजिस्टर में जोड़े नए सेवा प्रदाता (या मौजूदा सेवा प्रदाताओं में से किसी में यह कर):

<?php 

namespace App\Providers; 

use Laravel\Scout\EngineManager; 
use Illuminate\Support\ServiceProvider; 
use Elasticsearch\ClientBuilder as Elasticsearch; 
use App\Libs\Scout\Engines\ElasticqueryEngine; 

class ElasticqueryServiceProvider extends ServiceProvider 
{ 
    /** 
    * Perform post-registration booting of services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     resolve(EngineManager::class)->extend('elasticquery', function() { 
      return new ElasticqueryEngine(
       Elasticsearch::fromConfig(config('scout.elasticsearch.config')), 
       config('scout.elasticsearch.index') 
      ); 
     }); 
    } 

    /** 
    * Register bindings in the container. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     // 
    } 
} 

config/app.php में नए सेवा प्रदाता को जोड़ने के लिए मत भूलना:

'providers' => [ 
    // ... 
    Laravel\Scout\ScoutServiceProvider::class, 
    App\Providers\ElasticqueryServiceProvider::class, 
], 

और चालक को बदलने "elasticquery" config/scout.php या .env (SCOUT_DRIVER = elasticquery)

में सब के बाद करने के लिए, आप https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html से किसी भी प्रश्न के आधार पर खोज कर सकते हैं:

$query = [ 
    'simple_query_string' => [ 
     'query' => 'findme', 
     'fields' => [ 
      'title^5', 
      'description', 
     ], 
    ], 
]; 
$posts = Posts::search($query)->get(); 

// also you can use default ElasticsearchEngine query 
$posts = Posts::search('findme')->where('user_id', 1)->get(); 
+0

मैं इस समाधान चाहते, लेकिन अभी के लिए, मैं सिर्फ ईएस PHP एसडीके के माध्यम से सीधे लोचदार खोज से पूछताछ कर रहा हूं। – ATLChris

संबंधित मुद्दे