Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I have two or more search results? #83

Open
2jiwon opened this issue Jul 31, 2019 · 4 comments
Open

Can I have two or more search results? #83

2jiwon opened this issue Jul 31, 2019 · 4 comments

Comments

@2jiwon
Copy link

2jiwon commented Jul 31, 2019

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.

@tobias-kuendig
Copy link
Member

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.

@2jiwon
Copy link
Author

2jiwon commented Jul 31, 2019

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?

@tobias-kuendig
Copy link
Member

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.

@2jiwon
Copy link
Author

2jiwon commented Jul 31, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants