mbd-studio-sdk 3.4.0 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -99,6 +99,7 @@ mbd.addRanking(ranking);
99
99
  |--------|-------------|
100
100
  | `forUser(index, userId)` | Set user context for personalization |
101
101
  | `search()` | Build and run a search query |
102
+ | `frequentValues(index, field, size?)` | Fetch frequent values in an index/field (default size: 25) |
102
103
  | `addCandidates(array)` | Add search hits to the current context |
103
104
  | `features(version)` | Fetch features for candidates |
104
105
  | `addFeatures(result)` | Attach features to candidates |
@@ -109,5 +110,5 @@ mbd.addRanking(ranking);
109
110
 
110
111
  ## Useful links
111
112
 
112
- - **Demo repo:** [https://github.com/ZKAI-Network/mbd_studio_demo](https://github.com/ZKAI-Network/mbd_studio_demo)
113
- - **Studio webapp:** [https://api.mbd.xyz/v3/studio/frontend/](https://api.mbd.xyz/v3/studio/frontend/)
113
+ - **Examples:** [https://github.com/ZKAI-Network/mbd_studio_demo](https://github.com/ZKAI-Network/mbd_studio_demo)
114
+ - **Embed Studio webapp:** [https://api.mbd.xyz/v3/studio/frontend/](https://api.mbd.xyz/v3/studio/frontend/)
package/V1/Studio.js CHANGED
@@ -33,6 +33,9 @@ export class Studio {
33
33
  show: this._show,
34
34
  });
35
35
  }
36
+ async frequentValues(index, field, size = 25) {
37
+ return this.search().index(index).frequentValues(field, size);
38
+ }
36
39
  addCandidates(array) {
37
40
  this._candidates.push(...array);
38
41
  }
@@ -142,6 +145,7 @@ export class Studio {
142
145
  for (const hit of this._candidates) {
143
146
  hit._ranking_score = scoreByItemId[hit._id];
144
147
  }
148
+ this._candidates.sort((a, b) => (b._ranking_score ?? -Infinity) - (a._ranking_score ?? -Infinity));
145
149
  }
146
150
  log(string) {
147
151
  this._log(string);
@@ -123,6 +123,39 @@ export class Search {
123
123
  this._log(` max_score: ${infos.max_score}`);
124
124
  return res.hits;
125
125
  }
126
+ async frequentValues(field, size = 25) {
127
+ if (!this._index || typeof this._index !== 'string' || !this._index.trim()) {
128
+ throw new Error('Search.frequentValues: index must be set (call index(name) first)');
129
+ }
130
+ if (typeof field !== 'string' || !field.trim()) {
131
+ throw new Error('Search.frequentValues: field must be a non-empty string');
132
+ }
133
+ const n = Number(size);
134
+ if (!Number.isInteger(n) || n <= 0) {
135
+ throw new Error('Search.frequentValues: size must be a positive integer');
136
+ }
137
+ const endpoint = `/search/frequent_values/${encodeURIComponent(this._index)}/${encodeURIComponent(field.trim())}?size=${n}`;
138
+ const url = `${this._url}${endpoint}`;
139
+ this.log(`GET ${url}`);
140
+ const response = await fetch(url, {
141
+ method: 'GET',
142
+ headers: { Authorization: `Bearer ${this._apiKey}` },
143
+ });
144
+ if (!response.ok) {
145
+ const text = await response.text();
146
+ let message = `Search frequentValues error: ${response.status} ${response.statusText}`;
147
+ if (text) message += ` — ${text}`;
148
+ this.log(message);
149
+ throw new Error(message);
150
+ }
151
+ const result = await response.json();
152
+ if (result && typeof result.error !== 'undefined' && result.error !== null) {
153
+ const msg = typeof result.error === 'string' ? result.error : String(result.error);
154
+ this.log(msg);
155
+ throw new Error(msg);
156
+ }
157
+ return result;
158
+ }
126
159
  async lookup(docId) {
127
160
  if (!this._index || typeof this._index !== 'string' || !this._index.trim()) {
128
161
  throw new Error('Search.lookup: index must be set (call index(name) first)');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mbd-studio-sdk",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
4
4
  "description": "SDK for Embed Recommendation Engine APIs",
5
5
  "keywords": [
6
6
  "web3",