-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolr.js
78 lines (66 loc) · 2.28 KB
/
solr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const maxResult = 10;
const QUERY = `&q=`;
const ROWS = `&rows`;
const START = `&start=`;
const SORT = `&sort=`;
const JSON = `&wt=json`;
const INDENT = `&indent=on`;
const FIELDS = 'fl=';
class Query {
constructor(url, path){
this.url = url;
this.path = path;
}
set rows(rows){
this._rows = (rows > maxResult)? maxResult : rows;
}
next(){
if(this.hasNext()){
return new Promise((resolve, reject) => {
let offset = this.offset;
this.offset = this.offset + this._rows;
$.ajax({
url: this.url + this.path,
data: `?` + this.buildFieldParams(this.fields) + this.buildQueryParams(this.query, offset, this._rows, this.sort),
dataType: 'jsonp',
jsonp: 'json.wrf',
success: (res) => {
if(this.resultSize == undefined) this.resultSize = res.response.numFound;
resolve(res);
},
error: reject
});
});
}
else throw 'Query has returned a complete result set';
}
hasNext(){
return (this.resultSize == undefined || (this.resultSize != undefined && this.resultSize > this.offset));
}
reset(){
this.offset = 0;
}
getURLSearchParams(){
return `?` + this.buildFieldParams(this.fields) + this.buildQueryParams(this.query, offset, this._rows, this.sort);
}
buildFieldParams(fields){
if(fields == undefined || fields.length < 1) return;
return FIELDS + fields;
}
buildQueryParams(query, offset, rows, sort) {
let queryParams;
if(query != undefined) queryParams = QUERY + query;
else throw 'Query cannot be executed without a query parameter';
if(rows != undefined) queryParams = queryParams + ROWS + rows;
if(offset != undefined) queryParams = queryParams + START + offset;
if(sort != undefined) queryParams = queryParams + SORT + sort;
queryParams = queryParams + JSON;
queryParams = queryParams + INDENT;
return queryParams;
}
}
export default Query;
export const sortOrder = {
ASCENDING: 'asc',
DESCENDING: 'desc'
}