1 | var _Search_1;
|
2 | import { __decorate } from "tslib";
|
3 | import { _SPInstance } from "../spqueryable.js";
|
4 | import { hOP, isArray } from "@pnp/core";
|
5 | import { body, cacheAlways } from "@pnp/queryable";
|
6 | import { spPost } from "../operations.js";
|
7 | import { defaultPath } from "../decorators.js";
|
8 | const funcs = new Map([
|
9 | ["text", "Querytext"],
|
10 | ["template", "QueryTemplate"],
|
11 | ["sourceId", "SourceId"],
|
12 | ["trimDuplicatesIncludeId", ""],
|
13 | ["startRow", ""],
|
14 | ["rowLimit", ""],
|
15 | ["rankingModelId", ""],
|
16 | ["rowsPerPage", ""],
|
17 | ["selectProperties", ""],
|
18 | ["culture", ""],
|
19 | ["timeZoneId", ""],
|
20 | ["refinementFilters", ""],
|
21 | ["refiners", ""],
|
22 | ["hiddenConstraints", ""],
|
23 | ["sortList", ""],
|
24 | ["timeout", ""],
|
25 | ["hithighlightedProperties", ""],
|
26 | ["clientType", ""],
|
27 | ["personalizationData", ""],
|
28 | ["resultsURL", ""],
|
29 | ["queryTag", ""],
|
30 | ["properties", ""],
|
31 | ["queryTemplatePropertiesUrl", ""],
|
32 | ["reorderingRules", ""],
|
33 | ["hitHighlightedMultivaluePropertyLimit", ""],
|
34 | ["collapseSpecification", ""],
|
35 | ["uiLanguage", ""],
|
36 | ["desiredSnippetLength", ""],
|
37 | ["maxSnippetLength", ""],
|
38 | ["summaryLength", ""],
|
39 | ]);
|
40 | const props = new Map([]);
|
41 | function toPropCase(str) {
|
42 | return str.replace(/^(.)/, ($1) => $1.toUpperCase());
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export function SearchQueryBuilder(queryText = "", _query = {}) {
|
51 | return new Proxy({
|
52 | query: Object.assign({
|
53 | Querytext: queryText,
|
54 | }, _query),
|
55 | }, {
|
56 | get(self, propertyKey, proxy) {
|
57 | const pk = propertyKey.toString();
|
58 | if (pk === "toSearchQuery") {
|
59 | return () => self.query;
|
60 | }
|
61 | if (funcs.has(pk)) {
|
62 | return (...value) => {
|
63 | const mappedPk = funcs.get(pk);
|
64 | self.query[mappedPk.length > 0 ? mappedPk : toPropCase(pk)] = value.length > 1 ? value : value[0];
|
65 | return proxy;
|
66 | };
|
67 | }
|
68 | const propKey = props.has(pk) ? props.get(pk) : toPropCase(pk);
|
69 | self.query[propKey] = true;
|
70 | return proxy;
|
71 | },
|
72 | });
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | let _Search = _Search_1 = class _Search extends _SPInstance {
|
79 | |
80 |
|
81 |
|
82 | async run(queryInit) {
|
83 | const query = this.parseQuery(queryInit);
|
84 | const postBody = cacheAlways(body({
|
85 | request: {
|
86 | ...query,
|
87 | HitHighlightedProperties: this.fixArrProp(query.HitHighlightedProperties),
|
88 | Properties: this.fixArrProp(query.Properties),
|
89 | RefinementFilters: this.fixArrProp(query.RefinementFilters),
|
90 | ReorderingRules: this.fixArrProp(query.ReorderingRules),
|
91 | SelectProperties: this.fixArrProp(query.SelectProperties),
|
92 | SortList: this.fixArrProp(query.SortList),
|
93 | },
|
94 | }));
|
95 | const data = await spPost(this, postBody);
|
96 |
|
97 | const search = new _Search_1([this, this.parentUrl]);
|
98 | return new SearchResults(data, search, query);
|
99 | }
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 | fixArrProp(prop) {
|
106 | return typeof prop === "undefined" ? [] : isArray(prop) ? prop : [prop];
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | parseQuery(query) {
|
114 | let finalQuery;
|
115 | if (typeof query === "string") {
|
116 | finalQuery = { Querytext: query };
|
117 | }
|
118 | else if (query.toSearchQuery) {
|
119 | finalQuery = query.toSearchQuery();
|
120 | }
|
121 | else {
|
122 | finalQuery = query;
|
123 | }
|
124 | return finalQuery;
|
125 | }
|
126 | };
|
127 | _Search = _Search_1 = __decorate([
|
128 | defaultPath("_api/search/postquery")
|
129 | ], _Search);
|
130 | export { _Search };
|
131 | export const Search = (baseUrl) => (queryInit) => {
|
132 | return (new _Search(baseUrl)).run(queryInit);
|
133 | };
|
134 | export class SearchResults {
|
135 | constructor(rawResponse, _search, _query, _raw = null, _primary = null) {
|
136 | this._search = _search;
|
137 | this._query = _query;
|
138 | this._raw = _raw;
|
139 | this._primary = _primary;
|
140 | this._raw = rawResponse.postquery ? rawResponse.postquery : rawResponse;
|
141 | }
|
142 | get ElapsedTime() {
|
143 | var _a;
|
144 | return ((_a = this === null || this === void 0 ? void 0 : this.RawSearchResults) === null || _a === void 0 ? void 0 : _a.ElapsedTime) || 0;
|
145 | }
|
146 | get RowCount() {
|
147 | var _a, _b, _c;
|
148 | return ((_c = (_b = (_a = this === null || this === void 0 ? void 0 : this.RawSearchResults) === null || _a === void 0 ? void 0 : _a.PrimaryQueryResult) === null || _b === void 0 ? void 0 : _b.RelevantResults) === null || _c === void 0 ? void 0 : _c.RowCount) || 0;
|
149 | }
|
150 | get TotalRows() {
|
151 | var _a, _b, _c;
|
152 | return ((_c = (_b = (_a = this === null || this === void 0 ? void 0 : this.RawSearchResults) === null || _a === void 0 ? void 0 : _a.PrimaryQueryResult) === null || _b === void 0 ? void 0 : _b.RelevantResults) === null || _c === void 0 ? void 0 : _c.TotalRows) || 0;
|
153 | }
|
154 | get TotalRowsIncludingDuplicates() {
|
155 | var _a, _b, _c;
|
156 | return ((_c = (_b = (_a = this === null || this === void 0 ? void 0 : this.RawSearchResults) === null || _a === void 0 ? void 0 : _a.PrimaryQueryResult) === null || _b === void 0 ? void 0 : _b.RelevantResults) === null || _c === void 0 ? void 0 : _c.TotalRowsIncludingDuplicates) || 0;
|
157 | }
|
158 | get RawSearchResults() {
|
159 | return this._raw;
|
160 | }
|
161 | get PrimarySearchResults() {
|
162 | var _a, _b, _c, _d;
|
163 | if (this._primary === null) {
|
164 | this._primary = this.formatSearchResults(((_d = (_c = (_b = (_a = this._raw) === null || _a === void 0 ? void 0 : _a.PrimaryQueryResult) === null || _b === void 0 ? void 0 : _b.RelevantResults) === null || _c === void 0 ? void 0 : _c.Table) === null || _d === void 0 ? void 0 : _d.Rows) || null);
|
165 | }
|
166 | return this._primary;
|
167 | }
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | getPage(pageNumber, pageSize) {
|
175 |
|
176 | if (this.TotalRows < this.RowCount) {
|
177 | return Promise.resolve(null);
|
178 | }
|
179 |
|
180 |
|
181 | const rows = pageSize !== undefined ? pageSize : hOP(this._query, "RowLimit") ? this._query.RowLimit : 10;
|
182 | const query = {
|
183 | ...this._query,
|
184 | RowLimit: rows,
|
185 | StartRow: rows * (pageNumber - 1),
|
186 | };
|
187 |
|
188 | if (query.StartRow > this.TotalRows) {
|
189 | return Promise.resolve(null);
|
190 | }
|
191 | return this._search.run(query);
|
192 | }
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 | formatSearchResults(rawResults) {
|
199 | const results = new Array();
|
200 | if (typeof (rawResults) === "undefined" || rawResults == null) {
|
201 | return [];
|
202 | }
|
203 | const tempResults = rawResults.results ? rawResults.results : rawResults;
|
204 | for (const tempResult of tempResults) {
|
205 | const cells = tempResult.Cells.results ? tempResult.Cells.results : tempResult.Cells;
|
206 | results.push(cells.reduce((res, cell) => {
|
207 | res[cell.Key] = cell.Value;
|
208 | return res;
|
209 | }, {}));
|
210 | }
|
211 | return results;
|
212 | }
|
213 | }
|
214 |
|
\ | No newline at end of file |