oc-site-search-plugin icon indicating copy to clipboard operation
oc-site-search-plugin copied to clipboard

Can I have two or more search results?

Open 2jiwon opened this issue 5 years ago • 4 comments

Hello, first of all, this is a great plugin, thank you for providing.

I have a question. I want to customize this plugin for getting all results from several different models. For example, I tried this below.

public function boot()
   {
     $this->search1();
     $this->search2();
   }

   public function search1()
   {
       \Event::listen('offline.sitesearch.query', function ($query) {

           // Search your plugin's contents
           $items1 = Models\AppsData::where('app_name', 'like', "%${query}%")
                                     ->orWhere('app_id', 'like', "%${query}%")
                                     ->get();

           // Now build a results array
           $results1 = $items1->map(function ($item) use ($query) {

               // If the query is found in the title, set a relevance of 2
               $relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;

               // Optional: Add an age penalty to older results. This makes sure that
               // newer results are listed first.
               // if ($relevance > 1 && $item->published_at) {
               //     $relevance -= $this->getAgePenalty($item->published_at->diffInDays(Carbon::now()));
               // }

               return [
                   'title'     => $item->app_name,
                   'text'      => $item->app_id,
                   'url'       => '/apps/appsDetail/' . $item->idx,
                   //'thumb'     => $item->images->first(), // Instance of System\Models\File
                   'relevance' => $relevance, // higher relevance results in a higher
                                              // position in the results listing
                   // 'meta' => 'data',       // optional, any other information you want
                                              // to associate with this result
                   // 'model' => $item,       // optional, pass along the original model
               ];
           });

           return [
               'provider' => 'App list', // The badge to display for this result
               'results'  =>  $results1,

           ];
         });
       }

       public function search2()
       {
           \Event::listen('offline.sitesearch.query', function ($query) {

           // Search your plugin's contents
           $items2 = Models\MAService::where('app_name', 'like', "%${query}%")
                                     ->get();

           // Now build a results array
           $results2 = $items2->map(function ($item) use ($query) {

               // If the query is found in the title, set a relevance of 2
               $relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;

               // Optional: Add an age penalty to older results. This makes sure that
               // newer results are listed first.
               // if ($relevance > 1 && $item->published_at) {
               //     $relevance -= $this->getAgePenalty($item->published_at->diffInDays(Carbon::now()));
               // }

               return [
                   'title'     => $item->app_name,
                   'text'      => $item->app_name,
                   'url'       => '/service/servicedetail/' . $item->idx,
                   //'thumb'     => $item->images->first(), // Instance of System\Models\File
                   'relevance' => $relevance, // higher relevance results in a higher
                                              // position in the results listing
                   // 'meta' => 'data',       // optional, any other information you want
                                              // to associate with this result
                   // 'model' => $item,       // optional, pass along the original model
               ];
           });

           return [
               'provider' => 'MA list', // The badge to display for this result
               'results'  =>  $results2,

           ];
       });
   }

However, it just shows first result only. Can I make this getting two or more search results? If I can, how can I modify this? I'm a novice to Laravel or OctoberCMS. If you could help me get through this, I would appreciate it.

2jiwon avatar Jul 31 '19 08:07 2jiwon

Place a info('searching in 1') and info('searching in 2') line in those two event listeners to check if both are called. You will find the logged entries in your storage/logs/system.log file.

Registering multiple event listeners shouldn't be a problem. It might be your query that doesn't work as expected.

tobias-kuendig avatar Jul 31 '19 08:07 tobias-kuendig

Could you explain more specific? I looked for the log file and it doesn't help much. For now, I can't find any errors related to this problem.

Do you mean my code doesn't have a problem?

2jiwon avatar Jul 31 '19 11:07 2jiwon

Note the info() additions:

public function boot()
   {
     $this->search1();
     $this->search2();
   }

   public function search1()
   {
       \Event::listen('offline.sitesearch.query', function ($query) {

           info('searching search1');

           // Search your plugin's contents
           $items1 = Models\AppsData::where('app_name', 'like', "%${query}%")
                                     ->orWhere('app_id', 'like', "%${query}%")
                                     ->get();

           // Now build a results array
           $results1 = $items1->map(function ($item) use ($query) {

               // If the query is found in the title, set a relevance of 2
               $relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;

               // Optional: Add an age penalty to older results. This makes sure that
               // newer results are listed first.
               // if ($relevance > 1 && $item->published_at) {
               //     $relevance -= $this->getAgePenalty($item->published_at->diffInDays(Carbon::now()));
               // }

               return [
                   'title'     => $item->app_name,
                   'text'      => $item->app_id,
                   'url'       => '/apps/appsDetail/' . $item->idx,
                   //'thumb'     => $item->images->first(), // Instance of System\Models\File
                   'relevance' => $relevance, // higher relevance results in a higher
                                              // position in the results listing
                   // 'meta' => 'data',       // optional, any other information you want
                                              // to associate with this result
                   // 'model' => $item,       // optional, pass along the original model
               ];
           });

           return [
               'provider' => 'App list', // The badge to display for this result
               'results'  =>  $results1,

           ];
         });
       }

       public function search2()
       {
           \Event::listen('offline.sitesearch.query', function ($query) {

           info('searching search2');

           // Search your plugin's contents
           $items2 = Models\MAService::where('app_name', 'like', "%${query}%")
                                     ->get();

           // Now build a results array
           $results2 = $items2->map(function ($item) use ($query) {

               // If the query is found in the title, set a relevance of 2
               $relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;

               // Optional: Add an age penalty to older results. This makes sure that
               // newer results are listed first.
               // if ($relevance > 1 && $item->published_at) {
               //     $relevance -= $this->getAgePenalty($item->published_at->diffInDays(Carbon::now()));
               // }

               return [
                   'title'     => $item->app_name,
                   'text'      => $item->app_name,
                   'url'       => '/service/servicedetail/' . $item->idx,
                   //'thumb'     => $item->images->first(), // Instance of System\Models\File
                   'relevance' => $relevance, // higher relevance results in a higher
                                              // position in the results listing
                   // 'meta' => 'data',       // optional, any other information you want
                                              // to associate with this result
                   // 'model' => $item,       // optional, pass along the original model
               ];
           });

           return [
               'provider' => 'MA list', // The badge to display for this result
               'results'  =>  $results2,

           ];
       });
   }

Execute a search and check your logs. If both info lines are logged, there might be a problem with your query. In this case, try to dd($items2); and see what the output is.

tobias-kuendig avatar Jul 31 '19 11:07 tobias-kuendig

Oh, I understand and checked it. Thank you for your help! :D

2jiwon avatar Jul 31 '19 12:07 2jiwon