Integrating external search with Kirby filtering

A site I work on has a section that allows filtering by tags and categories as well as search, and the various filters and searching can all be used together. I also have this set up with lazy loading, so it’s using json stuff already on the backend to feed it, which may help with the next bit…

The client has become somewhat dissatisfied with the quality of the search results, so we’re looking at implementing an external search provider. The provider has a REST api that returns JSON, so I’m wondering if anybody can point me to some ideas or resources that might help me take that external search result data and combine it with the filtering I have going on so that I can still filter and search at the same time.

If i have understood correctly, you want to merge external data with data from the kirby site, and then do stuff with it?

i think i did something similar last year, but not with search results. I pulled Google Calendar events from Google Calendars API (which i think is really JSON that gets pulled in via a php library supplied by Google). This was then mixed with a the content in an Events section that was driven by kirby pages. The goal was to mix the two sources and display on the kirby site.

Most of my code wont help you but essentially what i did was map the the external data into an array, then map the kirby content into an array, then merged the arrays with array_merge(). Then i was able to sort them and list them out on the kirby site.

Forgive me if I have missed what you are trying to do. Whats the name of the search provider?

I think Kirby works well with Algolia, and theres a plugin over here for it.

If you convert your JSON data to an array, you can turn it into a collection:

$resultsArray = json_decode($jsonFromApi);
$results = new Collection($resultsArray);

Then you could filter that collection using Kirby’ filter methods.

Or, you do the filtering via JS, and only pass the filtered results back to Kirby.

1 Like

I’ll be using addsearch.

I’m pretty sure the algolia plugin doesn’t really do anything like this, but your comments are helpful, thanks.

Hmmm…that might be promising. The search results feed is obviously going to give me data that I’ll need to massage a bit to match the pages up with Kirby pages, but once I do that, this could work.

Well, I’m not sure what you are getting back from your API, if the search works rather on rendered pages (i.e. crawling URLs), that might be more difficult to merge with Kirby’s filter functions. After all, filtering (as well as searching) in Kirby happens on a per page/collection item basis, not on a per URL basis. What do you do with the results you get back from the search? Insert them directly into the page via JS?

Yeah, it works based on crawling the rendered pages. The standard method is that it automatically displays the results via JS, but I can use the API to get the results instead. The results will then give me the URL, which I figure I can massage in order to find the specific pages I need and then filter them using standard kirby functions. Assuming I can easily get a cross-site json feed into PHP.

Of course at that point my problem will be that I’m working with multiple load balanced servers that may not be in sync, so I might actually need to do everything in javascript in the browser. Hmmm…

The question is if your search results return the information you need for further filtering by tags and categories. If not, you’d have to merge this information into the results prior to further processing your data.

Oh, right. I’m going to see if I can add page metadata that gets picked up by addsearch and returned in the results. This would make everything easier.