1 | var _Search_1;
|
2 | import { __decorate } from "tslib";
|
3 | import { _SPInstance, spInvokableFactory } from "../spqueryable.js";
|
4 | import { getHashCode, hOP, isArray } from "@pnp/core";
|
5 | import { body, CacheAlways, CacheKey, invokable } 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 = 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 poster = new _Search_1([this, this.parentUrl]);
|
96 | poster.using(CacheAlways(), CacheKey(getHashCode(JSON.stringify(postBody)).toString()));
|
97 | const data = await spPost(poster, postBody);
|
98 |
|
99 | return new SearchResults(data, new _Search_1([this, this.parentUrl]), query);
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 | fixArrProp(prop) {
|
107 | return typeof prop === "undefined" ? [] : isArray(prop) ? prop : [prop];
|
108 | }
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | parseQuery(query) {
|
115 | let finalQuery;
|
116 | if (typeof query === "string") {
|
117 | finalQuery = { Querytext: query };
|
118 | }
|
119 | else if (query.toSearchQuery) {
|
120 | finalQuery = query.toSearchQuery();
|
121 | }
|
122 | else {
|
123 | finalQuery = query;
|
124 | }
|
125 | return finalQuery;
|
126 | }
|
127 | };
|
128 | _Search = _Search_1 = __decorate([
|
129 | defaultPath("_api/search/postquery"),
|
130 | invokable(function (init) {
|
131 | return this.run(init);
|
132 | })
|
133 | ], _Search);
|
134 | export { _Search };
|
135 | export const Search = spInvokableFactory(_Search);
|
136 | export class SearchResults {
|
137 | constructor(rawResponse, _search, _query, _raw = null, _primary = null) {
|
138 | this._search = _search;
|
139 | this._query = _query;
|
140 | this._raw = _raw;
|
141 | this._primary = _primary;
|
142 | this._raw = rawResponse.postquery ? rawResponse.postquery : rawResponse;
|
143 | }
|
144 | get ElapsedTime() {
|
145 | var _a;
|
146 | return ((_a = this === null || this === void 0 ? void 0 : this.RawSearchResults) === null || _a === void 0 ? void 0 : _a.ElapsedTime) || 0;
|
147 | }
|
148 | get RowCount() {
|
149 | var _a, _b, _c;
|
150 | 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;
|
151 | }
|
152 | get TotalRows() {
|
153 | var _a, _b, _c;
|
154 | 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;
|
155 | }
|
156 | get TotalRowsIncludingDuplicates() {
|
157 | var _a, _b, _c;
|
158 | 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;
|
159 | }
|
160 | get RawSearchResults() {
|
161 | return this._raw;
|
162 | }
|
163 | get PrimarySearchResults() {
|
164 | var _a, _b, _c, _d;
|
165 | if (this._primary === null) {
|
166 | 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);
|
167 | }
|
168 | return this._primary;
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | getPage(pageNumber, pageSize) {
|
177 |
|
178 | if (this.TotalRows < this.RowCount) {
|
179 | return Promise.resolve(null);
|
180 | }
|
181 |
|
182 |
|
183 | const rows = pageSize !== undefined ? pageSize : hOP(this._query, "RowLimit") ? this._query.RowLimit : 10;
|
184 | const query = {
|
185 | ...this._query,
|
186 | RowLimit: rows,
|
187 | StartRow: rows * (pageNumber - 1),
|
188 | };
|
189 |
|
190 | if (query.StartRow > this.TotalRows) {
|
191 | return Promise.resolve(null);
|
192 | }
|
193 | return this._search.run(query);
|
194 | }
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 | formatSearchResults(rawResults) {
|
201 | const results = new Array();
|
202 | if (typeof (rawResults) === "undefined" || rawResults == null) {
|
203 | return [];
|
204 | }
|
205 | const tempResults = rawResults.results ? rawResults.results : rawResults;
|
206 | for (const tempResult of tempResults) {
|
207 | const cells = tempResult.Cells.results ? tempResult.Cells.results : tempResult.Cells;
|
208 | results.push(cells.reduce((res, cell) => {
|
209 | res[cell.Key] = cell.Value;
|
210 | return res;
|
211 | }, {}));
|
212 | }
|
213 | return results;
|
214 | }
|
215 | }
|
216 |
|
\ | No newline at end of file |