Wiki source code of Attachment Search Module
Last modified by Eleni Cojocariu on 2026/10/01 09:49
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | The RequireJS module ##xwiki-attachment-picker-solr-search##, one of the two modules of the [[Attachment Picker WebJar>>doc:documentation.xs.dev.attachments.attachment-picker-webjar.WebHome]], 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. | ||
| 2 | |||
| 3 | == Members == | ||
| 4 | |||
| 5 | |=Member|=Returns|=Description | ||
| 6 | |##new SolrSearch(options)##|A ##SolrSearch##|Takes one object, holding the three options below. | ||
| 7 | |##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. | ||
| 8 | |||
| 9 | |=Option|=Value|=Default | ||
| 10 | |##limit##|The maximum number of results a search resolves to|##20## | ||
| 11 | |##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 | ||
| 12 | |##solrOptions##|##filter##, a comma-separated list of media types, and ##sort##, a Solr sort clause|Neither | ||
| 13 | |||
| 14 | |=Result field|=Value | ||
| 15 | |##id##|The reference of the attachment, serialized | ||
| 16 | |##filename##|The file name, as an array of one element | ||
| 17 | |##mimetype##|The media type, as an array of one element | ||
| 18 | |##isLocal##|Whether the attachment comes from the target Page | ||
| 19 | |||
| 20 | {{code language="javascript"}} | ||
| 21 | require(['xwiki-attachment-picker-solr-search'], function (SolrSearch) { | ||
| 22 | const search = new SolrSearch({ | ||
| 23 | limit: 10, | ||
| 24 | target: 'Sandbox.WebHome', | ||
| 25 | solrOptions: {filter: 'image/png,image/jpeg'} | ||
| 26 | }); | ||
| 27 | |||
| 28 | // Pass true to search beyond the target page as well. | ||
| 29 | search.search('logo', true).then(function (results) { | ||
| 30 | results.forEach(function (result) { | ||
| 31 | console.log(result.id, result.filename[0], result.mimetype[0], result.isLocal); | ||
| 32 | }); | ||
| 33 | }); | ||
| 34 | }); | ||
| 35 | {{/code}} | ||
| 36 | |||
| 37 | == Rules of a Search == | ||
| 38 | |||
| 39 | |=Rule|=Behaviour | ||
| 40 | |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##. | ||
| 41 | |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. | ||
| 42 | |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. | ||
| 43 | |An empty query|Searched as ##*##, so an empty search field lists everything in scope. |