/*global define */ define(['jquery', 'underscore', 'backbone', 'models/SolrHeader', 'models/SolrResult'], function($, _, Backbone, SolrHeader, SolrResult) { 'use strict'; /** @class SolrResults @classdesc A collection of SolrResult models that represent a list of search results from the DataONE query service. @extends Backbone.Collection @classcategory Collections @constructor */ var SolrResults = Backbone.Collection.extend( /** @lends SolrResults.prototype */{ // Reference to this collection's model. model: SolrResult, initialize: function(models, options) { if( typeof options === "undefined" || !options ){ var options = {}; } this.docsCache = options.docsCache || null; this.currentquery = options.query || '*:*'; this.rows = options.rows || 25; this.start = options.start || 0; this.sort = options.sort || 'dateUploaded desc'; this.facet = options.facet || []; this.facetCounts = "nothing"; this.stats = options.stats || false; this.minYear = options.minYear || 1900; this.maxYear = options.maxYear || new Date().getFullYear(); this.queryServiceUrl = options.queryServiceUrl || MetacatUI.appModel.get('queryServiceUrl'); if( MetacatUI.appModel.get("defaultSearchFields")?.length ) this.fields = MetacatUI.appModel.get("defaultSearchFields").join(","); else this.fields = options.fields || "id,title"; //If POST queries are disabled in the whole app, don't use POSTs here if( MetacatUI.appModel.get("disableQueryPOSTs") ){ this.usePOST = false; } //If this collection was initialized with the usePOST option, use POSTs here else if( options.usePOST ){ this.usePOST = true; } //Otherwise default to using GET else{ this.usePOST = false; } }, url: function() { //Convert facet keywords to a string var facetFields = ""; this.facet = _.uniq(this.facet); for (var i=0; i 0) { facetFields += "&facet.mincount=1"; // only facets meeting the current search facetFields += "&facet.limit=-1"; // CAREFUL: -1 means no limit on the number of facets } //Do we need stats? if (!this.stats){ var stats = ""; } else{ var stats = "&stats=true"; for(var i=0; i 0 ) endpoint += "&facet=true&facet.sort=index" + facetFields; endpoint += stats + "&wt=json"; return endpoint; }, parse: function(solr) { //Is this our latest query? If not, use our last set of docs from the latest query if((decodeURIComponent(this.currentquery).replace(/\+/g, " ") != solr.responseHeader.params.q) && this.docsCache) return this.docsCache; if(!solr.response){ if(solr.error && solr.error.msg){ console.log("Solr error: " + solr.error.msg); } return } //Save some stats this.header = new SolrHeader(solr.responseHeader); this.header.set({"numFound" : solr.response.numFound}); this.header.set({"start" : solr.response.start}); this.header.set({"rows" : solr.responseHeader.params.rows}); //Get the facet counts and store them in this model if( solr.facet_counts ){ this.facetCounts = solr.facet_counts.facet_fields; } //Cache this set of results this.docsCache = solr.response.docs; return solr.response.docs; }, /** * Fetches the next page of results */ nextpage: function() { // Only increment the page if the current page is not the last page if (this.start + this.rows < this.header.get("numFound")) { this.start += this.rows; } if (this.header != null) { this.header.set({"start" : this.start}); } this.lastUrl = this.url(); var fetchOptions = this.createFetchOptions(); this.fetch(fetchOptions); }, /** * Fetches the previous page of results */ prevpage: function() { this.start -= this.rows; if (this.start < 0) { this.start = 0; } if (this.header != null) { this.header.set({"start" : this.start}); } this.lastUrl = this.url(); var fetchOptions = this.createFetchOptions(); this.fetch(fetchOptions); }, /** * Fetches the given page of results * @param {number} page */ toPage: function(page) { // go to the requested page var requestedStart = this.rows * page; /* if (this.header != null) { if (requestedStart < this.header.get("numFound")) { this.start = requestedStart; } this.header.set({"start" : this.start}); }*/ this.start = requestedStart; this.lastUrl = this.url(); var fetchOptions = this.createFetchOptions(); this.fetch(fetchOptions); }, setrows: function(numrows) { this.rows = numrows; }, query: function(newquery) { if(typeof newquery != "undefined" && this.currentquery != newquery){ this.currentquery = newquery; this.start = 0; } this.lastUrl = this.url(); var fetchOptions = this.createFetchOptions(); this.fetch(fetchOptions); }, setQuery: function(newquery) { if (this.currentquery != newquery) { this.currentquery = newquery; this.start = 0; this.lastQuery = newquery; } }, /** * Returns the last query that was fetched. * @returns {string} */ getLastQuery: function(){ return this.lastQuery; }, setfields: function(newfields) { this.fields = newfields; }, setSort: function(newsort) { this.sort = newsort; this.trigger("change:sort"); }, setFacet: function(fields) { this.facet = fields; this.trigger("change:facet"); }, setStats: function(fields){ this.stats = fields; }, createFetchOptions: function(){ var options = { start : this.start, reset: true } let usePOST = this.usePOST || (this.currentquery.length > 1500 && !MetacatUI.appModel.get("disableQueryPOSTs")); if( usePOST ){ options.type = "POST"; var queryData = new FormData(); queryData.append("q", decodeURIComponent(this.currentquery)); queryData.append("rows", this.rows); queryData.append("sort", this.sort.replace("+", " ")); queryData.append("fl", this.fields); queryData.append("start", this.start); queryData.append("wt", "json"); //Add the facet fields to the FormData if( this.facet.length ){ queryData.append("facet", "true"); for (var i=0; i