Skip to content

Commit 3a7b02e

Browse files
authored
Merge pull request #372 from iceljc/features/refine-chat-window
add knowledge sort
2 parents 93624e5 + 8513bdb commit 3a7b02e

File tree

5 files changed

+225
-69
lines changed

5 files changed

+225
-69
lines changed

src/lib/helpers/types/knowledgeTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @property {boolean} [with_vector] - Include vector or not.
2626
* @property {string[]} [fields] - Included payload fields.
2727
* @property {VectorFilterGroup[]} [filter_groups] - Search filter groups.
28+
* @property {VectorSort?} [order_by] - Sort by.
2829
*/
2930

3031
/**
@@ -33,6 +34,12 @@
3334
* @property {{ key: string, value: string }[]} [filters] - Search filters.
3435
*/
3536

37+
/**
38+
* @typedef {Object} VectorSort
39+
* @property {string?} [field] - The sort field.
40+
* @property {string?} [order] - The sort order.
41+
*/
42+
3643
/**
3744
* @typedef {Object} KnowledgeSearchViewModel
3845
* @property {string} id - The knowledge data id.

src/lib/scss/custom/pages/_knowledgebase.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,24 @@
420420
}
421421
}
422422
}
423+
424+
.operator-container {
425+
margin-top: 20px;
426+
427+
.operator-item {
428+
display: flex;
429+
430+
@media (max-width: 420px) {
431+
flex-direction: column;
432+
gap: 10px !important;
433+
text-align: center;
434+
}
435+
436+
.operator-title {
437+
width: 120px;
438+
}
439+
}
440+
}
423441
}
424442

425443

src/routes/page/knowledge-base/common/search/advanced-search.svelte

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
/** @type {string} */
1414
export let operator = 'or';
1515
16+
/** @type {string} */
17+
export let sortOrder = "desc";
18+
19+
/** @type {string} */
20+
export let sortField = '';
21+
1622
/** @type {number} */
1723
export let maxLength = 1000;
1824
@@ -37,6 +43,21 @@
3743
}
3844
];
3945
46+
const sortDirections = [
47+
{
48+
id: 'sort-asc',
49+
value: 'asc',
50+
label: 'ASC',
51+
tip: 'Ascending sort.'
52+
},
53+
{
54+
id: 'sort-desc',
55+
value: 'desc',
56+
label: 'DESC',
57+
tip: 'Descending sort.'
58+
}
59+
];
60+
4061
/** @type {HTMLElement} */
4162
let scrollContainer;
4263
@@ -120,13 +141,6 @@
120141
});
121142
}
122143
}
123-
124-
/**
125-
* @param {any} e
126-
*/
127-
function changeLogicalOperator(e) {
128-
operator = e.target.value;
129-
}
130144
</script>
131145

132146

@@ -242,31 +256,77 @@
242256
{/if}
243257
</div>
244258

245-
<div class="mt-2">
246-
<div class="d-flex align-items-center gap-5">
247-
<span class="fw-bold">Search operator:</span>
248-
{#each logicalOperators as op, idx (idx)}
249-
<div class="d-flex align-items-center gap-1">
250-
<Input
251-
type="radio"
252-
id={op.id}
253-
name="searchOperator"
254-
value={op.value}
255-
disabled={disabled}
256-
bind:group={operator}
257-
on:change={e => changeLogicalOperator(e)}
258-
/>
259-
<label for={op.id} class="mb-0 d-flex gap-1">
260-
<span>{op.label}</span>
261-
<span class="line-align-center" id={`tooltip-${op.id}`}>
262-
<i class="bx bx-info-circle" />
263-
</span>
264-
<Tooltip target={`tooltip-${op.id}`} placement="top" class="operator-tooltip">
265-
<div>{op.tip}</div>
266-
</Tooltip>
267-
</label>
268-
</div>
269-
{/each}
259+
<div class="operator-container">
260+
<div class="operator-item align-items-center gap-5">
261+
<div class="fw-bold operator-title">Search operator:</div>
262+
<div class="d-flex align-items-center gap-5">
263+
{#each logicalOperators as op, idx (idx)}
264+
<div class="d-flex align-items-center gap-1">
265+
<Input
266+
type="radio"
267+
id={op.id}
268+
name="searchOperator"
269+
value={op.value}
270+
disabled={disabled}
271+
bind:group={operator}
272+
/>
273+
<label for={op.id} class="mb-0 d-flex gap-1">
274+
<span>{op.label}</span>
275+
{#if op.tip}
276+
<span class="line-align-center" id={`tooltip-${op.id}`}>
277+
<i class="bx bx-info-circle" />
278+
</span>
279+
<Tooltip target={`tooltip-${op.id}`} placement="top" class="operator-tooltip">
280+
<div>{op.tip}</div>
281+
</Tooltip>
282+
{/if}
283+
</label>
284+
</div>
285+
{/each}
286+
</div>
287+
</div>
288+
</div>
289+
290+
<div class="operator-container">
291+
<div class="operator-item align-items-center gap-5">
292+
<div class="fw-bold operator-title">Sort by field:</div>
293+
<div>
294+
<Input
295+
type="text"
296+
name="searchSortField"
297+
bind:value={sortField}
298+
disabled={disabled}
299+
maxlength={maxLength}
300+
/>
301+
</div>
302+
</div>
303+
<div class="operator-item align-items-center gap-5">
304+
<div class="operator-title"></div>
305+
<div class="d-flex align-items-center gap-5" style="margin-top: 5px;">
306+
{#each sortDirections as op, idx (idx)}
307+
<div class="d-flex align-items-center gap-1">
308+
<Input
309+
type="radio"
310+
id={op.id}
311+
name="searchSort"
312+
value={op.value}
313+
disabled={disabled}
314+
bind:group={sortOrder}
315+
/>
316+
<label for={op.id} class="mb-0 d-flex gap-1">
317+
<span>{op.label}</span>
318+
{#if op.tip}
319+
<span class="line-align-center" id={`tooltip-${op.id}`}>
320+
<i class="bx bx-info-circle" />
321+
</span>
322+
<Tooltip target={`tooltip-${op.id}`} placement="top" class="operator-tooltip">
323+
<div>{op.tip}</div>
324+
</Tooltip>
325+
{/if}
326+
</label>
327+
</div>
328+
{/each}
329+
</div>
270330
</div>
271331
</div>
272332
{/if}

src/routes/page/knowledge-base/documents/+page.svelte

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@
4848
const step = 0.1;
4949
const enableVector = true;
5050
const collectionType = KnowledgeCollectionType.Document;
51-
const includedPayloads = [
52-
KnowledgePayloadName.Text,
53-
KnowledgePayloadName.FileId,
54-
KnowledgePayloadName.FileName,
55-
KnowledgePayloadName.DataSource,
56-
KnowledgePayloadName.FileSource,
57-
KnowledgePayloadName.FileUrl
58-
];
5951
6052
/** @type {string} */
6153
let text = "";
@@ -91,6 +83,14 @@
9183
/** @type {import('$knowledgeTypes').VectorFilterGroup[]} */
9284
let innerSearchGroups = [];
9385
86+
let sortField = '';
87+
let sortOrder = 'desc';
88+
/** @type {import('$knowledgeTypes').VectorSort?} */
89+
let innerSort = {
90+
field: '',
91+
order: 'desc'
92+
};
93+
9494
/** @type {boolean} */
9595
let showDemo = true;
9696
let isSearching = false;
@@ -114,15 +114,17 @@
114114
* isReset: boolean,
115115
* isLocalLoading: boolean,
116116
* skipLoader: boolean,
117-
* filterGroups: any[]
117+
* filterGroups: any[],
118+
* sort: any
118119
* }}
119120
*/
120121
const defaultParams = {
121122
startId: null,
122123
isReset: false,
123124
isLocalLoading: false,
124125
skipLoader: false,
125-
filterGroups: []
126+
filterGroups: [],
127+
sort: null
126128
};
127129
128130
$: disabled = isLoading || isLoadingMore || isSearching;
@@ -149,7 +151,8 @@
149151
...defaultParams,
150152
isReset: true,
151153
skipLoader: true,
152-
filterGroups: innerSearchGroups
154+
filterGroups: innerSearchGroups,
155+
sort: null
153156
}).finally(() => isLoading = false);
154157
}).finally(() => {
155158
isLoading = false;
@@ -173,13 +176,15 @@
173176
isFromSearch = false;
174177
175178
innerSearchGroups = buildSearchFilterGroups(searchItems);
179+
innerSort = buildSearchSort(sortField, sortOrder);
176180
177181
if (textSearch) {
178182
getData({
179183
...defaultParams,
180184
isReset: true,
181185
skipLoader: true,
182-
filterGroups: innerSearchGroups
186+
filterGroups: innerSearchGroups,
187+
sort: innerSort
183188
}).then(() => {
184189
isFromSearch = true;
185190
}).finally(() => {
@@ -226,7 +231,8 @@
226231
startId: null,
227232
isReset: true,
228233
skipLoader: skipLoader,
229-
filterGroups: innerSearchGroups
234+
filterGroups: innerSearchGroups,
235+
sort: innerSort
230236
});
231237
}
232238
@@ -249,6 +255,12 @@
249255
textSearch = false;
250256
selectedOperator = 'or';
251257
innerSearchGroups = [];
258+
sortField = '';
259+
sortOrder = 'desc';
260+
innerSort = {
261+
field: sortField,
262+
order: sortOrder
263+
};
252264
}
253265
254266
@@ -316,20 +328,23 @@
316328
* @param {{
317329
* startId: string | null,
318330
* isReset: boolean,
319-
* filterGroups: any[] }} params
331+
* filterGroups: any[],
332+
* sort: any }} params
320333
*/
321334
function getKnowledgeListData(params = {
322335
startId: null,
323336
isReset: false,
324-
filterGroups: []
337+
filterGroups: [],
338+
sort: null
325339
}) {
326340
return new Promise((resolve, reject) => {
327341
const filter = {
328342
size: pageSize,
329343
start_id: params.startId,
330344
with_vector: enableVector,
331345
fields: [],
332-
filter_groups: params.filterGroups
346+
filter_groups: params.filterGroups,
347+
order_by: !!params.sort?.field ? params.sort : null
333348
};
334349
335350
getVectorKnowledgePageList(
@@ -358,14 +373,16 @@
358373
* isReset: boolean,
359374
* isLocalLoading: boolean,
360375
* skipLoader: boolean,
361-
* filterGroups: any[] }} params
376+
* filterGroups: any[],
377+
* sort: any }} params
362378
*/
363379
function getData(params = {
364380
startId: null,
365381
isReset: false,
366382
isLocalLoading: false,
367383
skipLoader: false,
368-
filterGroups: []
384+
filterGroups: [],
385+
sort: null
369386
}) {
370387
return new Promise((resolve, reject) => {
371388
if (!params.skipLoader) {
@@ -404,7 +421,8 @@
404421
...defaultParams,
405422
startId: nextId || null,
406423
isLocalLoading: true,
407-
filterGroups: innerSearchGroups
424+
filterGroups: innerSearchGroups,
425+
sort: innerSort
408426
};
409427
getData(params);
410428
}
@@ -713,11 +731,25 @@
713731
if (isAdvSearchOn && items?.length > 0) {
714732
const validItems = items.filter(x => x.checked && !!util.trim(x.key) && !!util.trim(x.value))
715733
.map(x => ({ key: util.trim(x.key), value: util.trim(x.value) }));
716-
groups = [ ...groups, { filter_operator: selectedOperator, filters: validItems } ];
734+
735+
if (validItems.length > 0) {
736+
groups = [ ...groups, { filter_operator: selectedOperator, filters: validItems } ];
737+
}
717738
}
718739
719740
return groups;
720741
}
742+
743+
/**
744+
* @param {string} field
745+
* @param {string} order
746+
*/
747+
function buildSearchSort(field, order) {
748+
return isAdvSearchOn ? {
749+
field: field,
750+
order: order
751+
} : null;
752+
}
721753
</script>
722754
723755
<HeadTitle title="{$_('Document Knowledge')}" />
@@ -889,6 +921,8 @@
889921
bind:showAdvSearch={isAdvSearchOn}
890922
bind:items={searchItems}
891923
bind:operator={selectedOperator}
924+
bind:sortField={sortField}
925+
bind:sortOrder={sortOrder}
892926
disabled={disabled}
893927
/>
894928

0 commit comments

Comments
 (0)