The RequireJS module xwiki-attachment-picker-solr-search, one of the two modules of the Attachment Picker WebJar, returns the SolrSearch class. It is what a picker searches with, and it needs no picker: it sends every query to the get action of the XWiki.SuggestSolrService page, resolved once when the object is built.
Members
| Member | Returns | Description |
|---|
| new SolrSearch(options) | A SolrSearch | Takes one object, holding the three options below. |
| search(query, isGlobal) | A promise of an array of results | Always searches the target Page. When isGlobal is true, it also runs an unscoped search and merges the two. |
| Option | Value | Default |
|---|
| limit | The maximum number of results a search resolves to | 20 |
| target | The reference of the Page a local search is scoped to. It replaces the Page and its location, and replaces the wiki too when the reference names one | The Page the script runs on |
| solrOptions | filter, a comma-separated list of media types, and sort, a Solr sort clause | Neither |
| Result field | Value |
|---|
| id | The reference of the attachment, serialized |
| filename | The file name, as an array of one element |
| mimetype | The media type, as an array of one element |
| isLocal | Whether the attachment comes from the target Page |
require(['xwiki-attachment-picker-solr-search'], function (SolrSearch) {
const search = new SolrSearch({
limit: 10,
target: 'Sandbox.WebHome',
solrOptions: {filter: 'image/png,image/jpeg'}
});
// Pass true to search beyond the target page as well.
search.search('logo', true).then(function (results) {
results.forEach(function (result) {
console.log(result.id, result.filename[0], result.mimetype[0], result.isLocal);
});
});
});
Rules of a Search
| Rule | Behaviour |
|---|
| The order of a global search | The results of the target Page first, then those found elsewhere minus the ones already listed, the whole cut back to limit. |
| The scope of a global search | No wiki, location or Page clause at all, so the second query reaches the whole farm the search service covers. |
| The scope of every query | Attachments only and, deliberately, every locale, so the attachments of a Page translated into another language than the current one are not dropped. |
| An empty query | Searched as *, so an empty search field lists everything in scope. |
The list is split on commas and on nothing else, so each media type becomes one clause of the query only when the list carries no separator and no spacing of its own: write image/png,image/jpeg.