Best practices: accelerate your search requests API
If we do a simple search:
GET https://{{KeepeekInstance}}/api/dam/media/search?f=title:li*
We receive a response of 1162 lines long and 29.63KB !
There is a golden rule for the search API, the less it has to send, the fastest !
So here is some advice to make the API faster.
Optimize the response size
Use the size parameter
If you want to display your results on a page with pages, instead of do a one time call and filter on your front end, use the pagination!
For example, you want to display your medias on a website where each page display 10 elements.
You use the "size" (the default value is 20) argument as :
GET https://{{KeepeekInstance}}/api/dam/media/search?f=title:li*&size=10
You receive a response like:
{ "_links": { "self": { "href": "https://{{KeepeekInstance}}/api/dam/search/media?f=title%3Alighthouse&page=1&size=10" }, "curies": [ { "name": "kpk", "href": "https://{{KeepeekInstance}}/api/doc/rels/{rel}", "templated": true } ], "next": { "href": "https://{{KeepeekInstance}}/api/dam/search/media?f=title%3Alighthouse&page=2&size=10" }, "last": { "href": "https://{{KeepeekInstance}}/api/dam/search/media?f=title%3Alighthouse&page=3&size=10" } }, "_embedded": { "media": [ // Media Objects... ], "facet": [ // Facet objects ], "selected-filter": { "key": "title", "value": "lighthouse", "_links": { "kpk:removed-filter-search": { "href": "https://{{KeepeekInstance}}/api/dam/search/media" } } } }, "totalCount": 22 }
The size of the response is now 681 lines long and 16.96KB !
You can see your medias into "_embedded.media". To use the pagination, just use the links in _links object such as _links.first, _links.last, _links.next, _links.previous.
So that was the first tip to reduce the response size !
Select the fields you need!
Even if the response contains only 10 medias per page, the size of the response for each media is quite big. For example in the last search, each media has the form:
{ "id": 2564, "title": "lighthouse-93487", "permission": "ADMIN", "mediaType": "image/jpeg", "fileSize": 9555468, "fileSizeString": "9,6 Mo", "updateDate": "2017-11-07T13:54:20.000Z", "duplicationStatus": "DUPLICATES_FOUND", "thumbnailGenerationStatus": "GENERATED", "attachmentCounts": { "online": 0, "offline": 0, "pendingToOnline": 0 }, "_links": { "self": { "href": "https://{{KeepeekInstance}}/api/dam/medias/2564" }, "kpk:500x500_extended": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-500x500_extended.png" }, "kpk:large": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-large.jpg" }, "kpk:medium": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-medium.jpg" }, "preview": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-preview.jpg" }, "kpk:small": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-small.jpg" }, "kpk:whr": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-whr.jpg" }, "kpk:xlarge": { "href": "https://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-xlarge.jpg" } } }
That is a lot of informations! And you probably don't need all of this. Let's continue with example of a webpage in which we display your medias.
We need:
- id of the image
- title of the image
- link to the preview to get the actual media
We then use the fields parameter with each field separated by a comma:
GET https://{{KeepeekInstance}}/api/dam/media/search?f=title:li*&size=10&fields=id,title,_links{kpk:preview}
Now, each media looks like:
{ "id": 2564, "title": "lighthouse-93487", "_links": { "self": { "href": "https://{{KeepeekInstance}}/api/dam/medias/2564" }, "preview": { "href": "http://assets.baobab.keepeek.com/medias/domain1/media14/2564-sfasu81pu9-preview.jpg" } } }
The answer is now 442 lines long and 10.37KB!
It is way much lighter and we have all the informations we need!
Select the facets you need!
442 lines is still a lot for only 10 medias. That is because we receive lots of facets !
In the 442 lines, 288 lines are for the facets, and we do not need it.
I am going to ask to not receive any facets with the parameter facet-fields with de value []:
GET https://{{KeepeekInstance}}/api/dam/media/search?f=title:li*&size=10&fields=id,title,_links{kpk:preview}&facet-fields=[]
The response is now 154 lines long and 3.25KB!
Optimize keyword search and sorting
Another golden rule is that the more precise your search, the faster! And also, the more precise you are with your search, the more relevant your results will be.
In our last searches we search with the title li* which search all the media that start by li, that's 45 items. To get less results, we can be more precise, by using a full word, as lighthouse. That returns 21 items only.
If we know which sorting we want to apply to our search we can use the parameter sort. It might avoid some additional sorting on client side that takes time.
If we want to sort the items by title field.
GET https://{{KeepeekInstance}}/api/dam/media/search?f=title:lighthouse&size=10&fields=id,title,_links{kpk:preview}&facet-fields=[]&sort=title
The sort is ascending by default, to sort by title in descending order:
GET https://{{KeepeekInstance}}/api/dam/media/search?f=title:lighthouse&size=10&fields=id,title,_links{kpk:preview}&facet-fields=[]&sort=title desc