2019-10-18 15:05:56 +00:00
import axios from "axios" ;
var SOLR_CONFIG = {
"server" : "https://arcticdata.io/metacat/d1/mn/v2/query/solr?" , // Solr server
"filter" : "knb-lter-bnz" , // Filter results for an organization or user
2020-04-17 15:55:18 +00:00
"limit" : 10 , // Max number of results to retrieve per page
2019-10-18 15:05:56 +00:00
"resultsElementId" : "searchResults" , // Element to contain results
"urlElementId" : "searchUrl" , // Element to display search URL
"countElementId" : "resultCount" , // Element showing number of results
"pagesElementId" : "pagination" , // Element to display result page links
"showPages" : 5 // MUST BE ODD NUMBER! Max number of page links to show
} ;
export default {
2020-06-02 14:11:07 +00:00
async search ( term : string , filterItems : Object , solrCore : string , solrHost : string , start? : string ) : Promise < any > {
2019-10-18 15:05:56 +00:00
// solr endpoint
// const host = 'http://voyagerdemo.com/';
2020-06-02 14:11:07 +00:00
//const host = 'https://www.tethys.at/';
2020-07-27 11:58:47 +00:00
//const host = 'https://' + solrHost;
2020-08-27 14:45:06 +00:00
const host = 'https://' + solrHost ;
2020-06-02 14:11:07 +00:00
const path = '/solr/' + solrCore + '/select?' ;
2019-10-18 15:05:56 +00:00
var base = host + path ;
//const fields = 'id,server_date_published,abstract_output,title_output,title_additional,author,subject'; // fields we want returned
2020-05-20 13:54:10 +00:00
var fields = [ "id" , "licence" ,
2019-10-18 15:05:56 +00:00
"server_date_published" ,
"abstract_output" ,
2021-05-31 07:49:49 +00:00
"identifier" ,
2019-10-18 15:05:56 +00:00
"title_output" ,
"title_additional" ,
"author" ,
2020-04-17 15:55:18 +00:00
"subject" , "doctype" ] . toString ( ) ;
2019-10-18 15:05:56 +00:00
var limit = "&rows=" + SOLR_CONFIG [ "limit" ] ;
// var limit = solrConfig.limit;
2019-10-18 15:49:02 +00:00
2019-11-21 17:43:56 +00:00
var qfFields = "title^3 author^2 subject^1" ;
2019-10-18 15:49:02 +00:00
var params = "fl=" + fields ;
if ( term == "*%3A*" ) {
params += "&defType=edismax&wt=json&indent=on" ; //edismax
} else {
2019-10-30 10:25:56 +00:00
params += "&defType=dismax&qf=" + qfFields + "&wt=json&indent=on" ; //dismax
2019-10-18 15:49:02 +00:00
}
2019-10-18 15:05:56 +00:00
if ( start === undefined ) start = "0" ;
start = "&start=" + start ;
2019-10-18 15:49:02 +00:00
2019-10-18 15:05:56 +00:00
const facetFields = "&facet=on&facet.field=language&facet.field={!key=datatype}doctype&facet.field=subject" ; //&fq=year:(2019)";//&facet.query=year:2018";
var filterFields = "" ;
// filterItems.forEach(function (item) {
// console.log(item.value + " " + item.category);
// filterFields += "&fq=" + item.category +":("+ item.value + ")";
// });
Object . entries ( filterItems ) . forEach ( ( [ key , valueArray ] ) = > {
// console.log(`${key} ${valueArray}`);
valueArray . forEach ( function ( value ) {
2020-03-10 17:58:46 +00:00
filterFields += '&fq=' + key + ':("' + value + '")' ;
2019-10-18 15:05:56 +00:00
} ) ;
} ) ;
2020-05-25 12:39:10 +00:00
var query = "&sort=server_date_published desc" + "&q=" + term ;
2019-10-18 15:05:56 +00:00
// $dismax->setQueryFields('title^3 abstract^2 subject^1');
//const api = `${host}${path}?defType=dismax&q=${term}&fl=${fields}&qf=${dismaxFields}&facet=on&${facetFields}&${filterFields}&wt=json&rows=20&indent=on`;
//const api = `${host}${path}?q=${term}&fl=${fields}&facet=on&${facetFields}&${filterFields}&wt=json&rows=20&indent=on`;
var api = base + params + limit + start + query + filterFields + facetFields ;
let res = await axios . get ( api ) ;
// let { data } = res.data;
return res . data ; //.response;//.docs;
2019-10-30 10:25:56 +00:00
} ,
2020-05-25 12:39:10 +00:00
// for the autocomplete search
2020-06-02 14:11:07 +00:00
async searchTerm ( term : string , solrCore : string , solrHost : string ) : Promise < any > {
2019-10-30 10:25:56 +00:00
// solr endpoint
// const host = 'http://voyagerdemo.com/';
2020-06-02 14:11:07 +00:00
// const host = 'https://www.tethys.at/';''
const host = 'https://' + solrHost ;
const path = '/solr/' + solrCore + '/select?' ;
2019-10-30 10:25:56 +00:00
var base = host + path ;
//const fields = 'id,server_date_published,abstract_output,title_output,title_additional,author,subject'; // fields we want returned
2020-05-20 13:54:10 +00:00
var fields = [ "id" , "licence" ,
2019-10-30 10:25:56 +00:00
"server_date_published" ,
"abstract_output" ,
"title_output" ,
"title_additional" ,
"author" ,
2020-04-17 15:55:18 +00:00
"subject" , "doctype" ] . toString ( ) ;
2019-10-30 10:25:56 +00:00
//var dismaxFields = "title^3 abstract^2 subject^1";
var qfFields = "title^3 author^2 subject^1" ;
var params = "fl=" + fields ;
// if (term == "*%3A*") {
// params += "&defType=edismax&wt=json&indent=on"; //edismax
// } else {
params += "&defType=edismax&qf=" + qfFields + "&wt=json&indent=on" ; //dismax
// }
var query = "&q=" + term + "*" ;
var api = base + params + query ;
let res = await axios . get ( api ) ;
return res . data ; //.response;//.docs;
2019-10-18 15:05:56 +00:00
}
}