The system provides functionality to search for domain objects. This is usually happens when a user fills a form on a website and clicks on a search button. The system then looks for objects matching user criteria and returns a portion of results (pagination). The user can then see the results, switch between pages to see other portions or resubmit the search with another criteria.
This becomes complicated when the website need to restore values in the search form so the user does not have to re-enter those again as she may click the refresh button or the same form may exist on several pages. More than that, the user may want to copy a link from her browser and paste it somewhere - clicking on this link must do the same search she just did. Such functionality is fully supported by the system.
When a user clicks search
This scenario is based on any property website. Most of the packs work in the similar manner when searching for objects.
- Step 1 - Filling a form
- First a user fills a search from selecting location, amount of bedrooms, clicking check boxes such as parking etc. The form is filled and ready for submission. JS then creates URL with user criteria embedded and forwards user to this URL. Seems odd, why doesn't JS make AJAX call to the server and populate results? First, the search form may be located on a page which does not support showing results and second, the user may want to bookmark the search link which is not updated on AJAX call. Forwarding the user to another URL will cover both cases.
- Step 2 - Processing URL
- The system accepts this URL and works with search criteria embedded in it. It extracts the parameters user entered and saves those in user's session. JSP rendering the page retrieves those parameters by using a tag library and fills the search form. The user is now on a page that has all the code to display results, browse results, plus the search form containing the values previously entered, the user may fix and resubmit her search. The page is only missing results - it is empty.
- Step 3 - Getting results
- JS makes AJAX call to the server supplying search parameters. The system retrieves the requested portion of results (based on search criteria, sorting and pagination supplied by JS) and sends those back. JS gets the response and populates the results.
Now the browser is displaying results and its address is pointing to URL with current search criteria. The URL may be bookmarked or emailed. Entering this URL from anywhere else will result in executing Step 2 and Step 3 producing the same results and populating the search form.
The system goes further by caching the last results to improve performance when the user refreshes the page or goes to another link and then returns to the search page. This cache is per user as results of the latest search are stored in user's session.
Sorting and pagination
JS supplies sorting and pagination to the server when it makes AJAX call in Step 3. When a user re-sorts or requests the next portion of results, JS makes AJAX call resulting in another search performed by the server. The system then stores such parameters in user's session to restore if a page eventually refreshes. Since sorting and pagination is not part of URL (Step 1), the system will reset those to defaults when a new search is performed or bookmarked URL is restored. This works fine for browsing the results, clicking to see the details of one of them then going back to see the same properly ordered portion of results.
The system is using database queries to fetch objects partially matching user's criteria. Then the code is checking every object to see if it fully matches user's criteria. Successful objects included into the set. As the system has to know a total count of matching objects, it has to go through every object. This is performance bottleneck when there are thousands of objects to go through, however when there are just few then this approach is beneficial as it also gathers all IDs and may provide fast navigation to the next or previous object.
When a database query is slow then the system may benefit from using a search engine. Solr is used to speed up the search. The system requests records from the search engine fully matching user's criteria and the search engine returns IDs and total count. The only thing left to do is to fetch objects from the database. This approach does not gather all IDs thus lacks fast navigation to the first or previous object. Workaround is getting IDs from Solr using another query, this slightly downgrades the performance as Solr will be queried twice.