@stacksjs/search-engine 0.59.11 → 0.61.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/search-engine",
3
3
  "type": "module",
4
- "version": "0.59.11",
4
+ "version": "0.61.0",
5
5
  "description": "The Stacks search engine integrations.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -58,8 +58,8 @@
58
58
  "@stacksjs/ui": "latest"
59
59
  },
60
60
  "dependencies": {
61
- "@opensearch-project/opensearch": "^2.5.0",
62
- "meilisearch": "^0.37.0"
61
+ "@opensearch-project/opensearch": "^2.8.0",
62
+ "meilisearch": "^0.40.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@stacksjs/config": "latest",
@@ -1,124 +1,124 @@
1
- import process from 'node:process'
2
- import { searchEngine } from '@stacksjs/config'
3
- import { log } from '@stacksjs/logging'
4
- import type { MeiliSearchOptions, SearchEngineDriver } from '@stacksjs/types'
5
- import { ExitCode } from '@stacksjs/types'
6
- import type { DocumentOptions, EnqueuedTask, Index, IndexOptions, IndexesResults, SearchResponse } from 'meilisearch'
7
- import { MeiliSearch } from 'meilisearch'
8
-
9
- function client(options?: MeiliSearchOptions) {
10
- let host = searchEngine.meilisearch?.host
11
- let apiKey = searchEngine.meilisearch?.apiKey
12
-
13
- if (options?.host)
14
- host = options.host
15
-
16
- if (options?.apiKey)
17
- apiKey = options.apiKey
18
-
19
- if (!host) {
20
- log.error('Please specify a search engine host.')
21
- process.exit(ExitCode.FatalError)
22
- }
23
-
24
- return new MeiliSearch({ host, apiKey })
25
- }
26
-
27
- async function search(index: string, params: any): Promise<SearchResponse<Record<string, any>>> {
28
- const offsetVal = ((params.page * params.perPage) - 20) || 0
29
- const filter = convertToFilter(params.filter)
30
- const sort = convertToMeilisearchSorting(params.sort)
31
-
32
- return await client()
33
- .index(index)
34
- .search(params.query, { limit: params.perPage, filter, sort, offset: offsetVal })
35
- }
36
-
37
- async function addDocument(indexName: string, params: any): Promise<EnqueuedTask> {
38
- return await client().index(indexName).addDocuments([params])
39
- }
40
-
41
- async function addDocuments(indexName: string, params: any[]): Promise<EnqueuedTask> {
42
- return await client().index(indexName).addDocuments(params)
43
- }
44
-
45
- async function createIndex(name: string, options?: IndexOptions): Promise<EnqueuedTask> {
46
- return await client().createIndex(name, options)
47
- }
48
-
49
- async function updateIndex(indexName: string, params: IndexOptions): Promise<EnqueuedTask> {
50
- return await client().updateIndex(indexName, params)
51
- }
52
-
53
- async function updateDocument(indexName: string, params: DocumentOptions): Promise<EnqueuedTask> {
54
- return await client().index(indexName).updateDocuments([params])
55
- }
56
-
57
- async function updateDocuments(indexName: string, params: DocumentOptions[]): Promise<EnqueuedTask> {
58
- return await client().index(indexName).updateDocuments(params)
59
- }
60
-
61
- async function deleteDocument(indexName: string, id: number): Promise<EnqueuedTask> {
62
- return await client().index(indexName).deleteDocument(id)
63
- }
64
-
65
- async function deleteDocuments(indexName: string, filters: string | string[]): Promise<EnqueuedTask> {
66
- return await client().index(indexName).deleteDocuments({ filter: filters })
67
- }
68
-
69
- async function getDocument(indexName: string, id: number, fields: any): Promise<EnqueuedTask> {
70
- return await client().index(indexName).getDocument(id, fields)
71
- }
72
-
73
- async function deleteIndex(indexName: string): Promise<EnqueuedTask> {
74
- return await client().deleteIndex(indexName)
75
- }
76
-
77
- async function listAllIndexes(): Promise<IndexesResults<Index[]>> {
78
- return await client().getIndexes()
79
- }
80
-
81
- function convertToFilter(jsonData: any): string[] {
82
- const filters: string[] = []
83
-
84
- for (const key in jsonData) {
85
- if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
86
- const value = jsonData[key]
87
- const filter = `${key}='${value}'`
88
- filters.push(filter)
89
- }
90
- }
91
-
92
- return filters
93
- }
94
-
95
- function convertToMeilisearchSorting(jsonData: any): string[] {
96
- const filters: string[] = []
97
-
98
- for (const key in jsonData) {
99
- if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
100
- const value = jsonData[key]
101
- const filter = `${key}='${value}'`
102
- filters.push(filter)
103
- }
104
- }
105
-
106
- return filters
107
- }
108
-
109
- export default {
110
- client,
111
- search,
112
- createIndex,
113
- deleteIndex,
114
- updateIndex,
115
- listAllIndexes,
116
- addDocument,
117
- addDocuments,
118
- updateDocument,
119
- listAllIndices: listAllIndexes,
120
- updateDocuments,
121
- deleteDocument,
122
- deleteDocuments,
123
- getDocument,
124
- } satisfies SearchEngineDriver
1
+ // import process from 'node:process'
2
+ // import { searchEngine } from '@stacksjs/config'
3
+ // import { log } from '@stacksjs/logging'
4
+ // import type { MeilisearchOptions, SearchEngineDriver } from '@stacksjs/types'
5
+ // import { ExitCode } from '@stacksjs/types'
6
+ // import type { DocumentOptions, EnqueuedTask, Index, IndexOptions, IndexesResults, SearchResponse } from 'meilisearch'
7
+ // import { Meilisearch } from 'meilisearch'
8
+ //
9
+ // function client(options?: MeilisearchOptions) {
10
+ // let host = searchEngine.meilisearch?.host
11
+ // let apiKey = searchEngine.meilisearch?.apiKey
12
+ //
13
+ // if (options?.host)
14
+ // host = options.host
15
+ //
16
+ // if (options?.apiKey)
17
+ // apiKey = options.apiKey
18
+ //
19
+ // if (!host) {
20
+ // log.error('Please specify a search engine host.')
21
+ // process.exit(ExitCode.FatalError)
22
+ // }
23
+ //
24
+ // return new MeiliSearch({ host, apiKey })
25
+ // }
26
+ //
27
+ // async function search(index: string, params: any): Promise<SearchResponse<Record<string, any>>> {
28
+ // const offsetVal = ((params.page * params.perPage) - 20) || 0
29
+ // const filter = convertToFilter(params.filter)
30
+ // const sort = convertToMeilisearchSorting(params.sort)
31
+ //
32
+ // return await client()
33
+ // .index(index)
34
+ // .search(params.query, { limit: params.perPage, filter, sort, offset: offsetVal })
35
+ // }
36
+ //
37
+ // async function addDocument(indexName: string, params: any): Promise<EnqueuedTask> {
38
+ // return await client().index(indexName).addDocuments([params])
39
+ // }
40
+ //
41
+ // async function addDocuments(indexName: string, params: any[]): Promise<EnqueuedTask> {
42
+ // return await client().index(indexName).addDocuments(params)
43
+ // }
44
+ //
45
+ // async function createIndex(name: string, options?: IndexOptions): Promise<EnqueuedTask> {
46
+ // return await client().createIndex(name, options)
47
+ // }
48
+ //
49
+ // async function updateIndex(indexName: string, params: IndexOptions): Promise<EnqueuedTask> {
50
+ // return await client().updateIndex(indexName, params)
51
+ // }
52
+ //
53
+ // async function updateDocument(indexName: string, params: DocumentOptions): Promise<EnqueuedTask> {
54
+ // return await client().index(indexName).updateDocuments([params])
55
+ // }
56
+ //
57
+ // async function updateDocuments(indexName: string, params: DocumentOptions[]): Promise<EnqueuedTask> {
58
+ // return await client().index(indexName).updateDocuments(params)
59
+ // }
60
+ //
61
+ // async function deleteDocument(indexName: string, id: number): Promise<EnqueuedTask> {
62
+ // return await client().index(indexName).deleteDocument(id)
63
+ // }
64
+ //
65
+ // async function deleteDocuments(indexName: string, filters: string | string[]): Promise<EnqueuedTask> {
66
+ // return await client().index(indexName).deleteDocuments({ filter: filters })
67
+ // }
68
+ //
69
+ // async function getDocument(indexName: string, id: number, fields: any): Promise<EnqueuedTask> {
70
+ // return await client().index(indexName).getDocument(id, fields)
71
+ // }
72
+ //
73
+ // async function deleteIndex(indexName: string): Promise<EnqueuedTask> {
74
+ // return await client().deleteIndex(indexName)
75
+ // }
76
+ //
77
+ // async function listAllIndexes(): Promise<IndexesResults<Index[]>> {
78
+ // return await client().getIndexes()
79
+ // }
80
+ //
81
+ // function convertToFilter(jsonData: any): string[] {
82
+ // const filters: string[] = []
83
+ //
84
+ // for (const key in jsonData) {
85
+ // if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
86
+ // const value = jsonData[key]
87
+ // const filter = `${key}='${value}'`
88
+ // filters.push(filter)
89
+ // }
90
+ // }
91
+ //
92
+ // return filters
93
+ // }
94
+ //
95
+ // function convertToMeilisearchSorting(jsonData: any): string[] {
96
+ // const filters: string[] = []
97
+ //
98
+ // for (const key in jsonData) {
99
+ // if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
100
+ // const value = jsonData[key]
101
+ // const filter = `${key}='${value}'`
102
+ // filters.push(filter)
103
+ // }
104
+ // }
105
+ //
106
+ // return filters
107
+ // }
108
+ //
109
+ // export default {
110
+ // client,
111
+ // search,
112
+ // createIndex,
113
+ // deleteIndex,
114
+ // updateIndex,
115
+ // listAllIndexes,
116
+ // addDocument,
117
+ // addDocuments,
118
+ // updateDocument,
119
+ // listAllIndices: listAllIndexes,
120
+ // updateDocuments,
121
+ // deleteDocument,
122
+ // deleteDocuments,
123
+ // getDocument,
124
+ // } satisfies SearchEngineDriver
@@ -1,59 +1,59 @@
1
- import { searchEngine } from '@stacksjs/config'
2
- import type { ApiResponse } from '@opensearch-project/opensearch'
3
- import { Client } from '@opensearch-project/opensearch'
4
- import type { SearchEngineDriver } from '@stacksjs/types'
5
-
6
- const host = searchEngine.openSearch?.host
7
- const protocol = searchEngine.openSearch?.protocol
8
- const port = searchEngine.openSearch?.port
9
- const auth = searchEngine.openSearch?.auth
10
-
11
- const client = new Client({
12
- node: `${protocol}://${auth}@${host}:${port}`,
13
- })
14
-
15
- async function search(index: string, params?: any) {
16
- const response = await client.search({
17
- index,
18
- body: params.query,
19
- })
20
-
21
- return response
22
- }
23
-
24
- async function createIndex(indexName: string, settings?: any): Promise<ApiResponse<any, any>> {
25
- return await client.indices.create({ index: indexName, body: settings })
26
- }
27
- // async function updateIndex(indexName: string, settings?: any): Promise<ApiResponse<any, any>> {
28
- // return await client.indices.update({ index: indexName, body: settings })
1
+ // import { searchEngine } from '@stacksjs/config'
2
+ // import type { ApiResponse } from '@opensearch-project/opensearch'
3
+ // import { Client } from '@opensearch-project/opensearch'
4
+ // import type { SearchEngineDriver } from '@stacksjs/types'
5
+ //
6
+ // const host = searchEngine.openSearch?.host
7
+ // const protocol = searchEngine.openSearch?.protocol
8
+ // const port = searchEngine.openSearch?.port
9
+ // const auth = searchEngine.openSearch?.auth
10
+ //
11
+ // const client = new Client({
12
+ // node: `${protocol}://${auth}@${host}:${port}`,
13
+ // })
14
+ //
15
+ // async function search(index: string, params?: any) {
16
+ // const response = await client.search({
17
+ // index,
18
+ // body: params.query,
19
+ // })
20
+ //
21
+ // return response
29
22
  // }
30
-
31
- async function deleteIndex(indexName: string): Promise<ApiResponse<any, any>> {
32
- return await client.indices.delete({ index: indexName })
33
- }
34
-
35
- async function addDocument(indexName: string, document: any): Promise<ApiResponse<any, any>> {
36
- return await client.index({
37
- id: document.id,
38
- index: indexName,
39
- body: document,
40
- refresh: true,
41
- })
42
- }
43
-
44
- async function deleteDocument(indexName: string, document: any): Promise<ApiResponse<any, any>> {
45
- return await client.delete({
46
- id: document.id,
47
- index: indexName,
48
- })
49
- }
50
-
51
- export default {
52
- client,
53
- search,
54
- createIndex,
55
- deleteIndex,
56
- addDocument,
57
- deleteDocument,
58
- // ...other methods
59
- } satisfies SearchEngineDriver
23
+ //
24
+ // async function createIndex(indexName: string, settings?: any): Promise<ApiResponse<any, any>> {
25
+ // return await client.indices.create({ index: indexName, body: settings })
26
+ // }
27
+ // // async function updateIndex(indexName: string, settings?: any): Promise<ApiResponse<any, any>> {
28
+ // // return await client.indices.update({ index: indexName, body: settings })
29
+ // // }
30
+ //
31
+ // async function deleteIndex(indexName: string): Promise<ApiResponse<any, any>> {
32
+ // return await client.indices.delete({ index: indexName })
33
+ // }
34
+ //
35
+ // async function addDocument(indexName: string, document: any): Promise<ApiResponse<any, any>> {
36
+ // return await client.index({
37
+ // id: document.id,
38
+ // index: indexName,
39
+ // body: document,
40
+ // refresh: true,
41
+ // })
42
+ // }
43
+ //
44
+ // async function deleteDocument(indexName: string, document: any): Promise<ApiResponse<any, any>> {
45
+ // return await client.delete({
46
+ // id: document.id,
47
+ // index: indexName,
48
+ // })
49
+ // }
50
+ //
51
+ // export default {
52
+ // client,
53
+ // search,
54
+ // createIndex,
55
+ // deleteIndex,
56
+ // addDocument,
57
+ // deleteDocument,
58
+ // // ...other methods
59
+ // } satisfies SearchEngineDriver
package/src/helpers.ts CHANGED
@@ -4,9 +4,11 @@
4
4
 
5
5
  import type { Hits, SearchResponse } from 'meilisearch'
6
6
 
7
- // export function isString(val: unknown): val is string {
8
- // return typeof val === 'string'
9
- // }
7
+ // import { isString } from '@stacksjs/validation'
8
+
9
+ export function isString(val: unknown): val is string {
10
+ return typeof val === 'string'
11
+ }
10
12
 
11
13
  // this interface is primarily used to persist data to localStorage, in a unified way
12
14
  export interface SearchEngineStore {
@@ -125,10 +127,10 @@ export interface SearchEngineStore {
125
127
  export function determineState(): SearchEngineStore {
126
128
  const ls = localStorage.getItem('search-engine')
127
129
 
128
- if (isString(ls))
129
- return JSON.parse(ls) as SearchEngineStore
130
+ if (isString(ls)) return JSON.parse(ls) as SearchEngineStore
130
131
 
131
- return { // default state
132
+ return {
133
+ // default state
132
134
  source: 'http://127.0.0.1:7700',
133
135
  index: '',
134
136
  perPage: 20,
package/src/index.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import type { UiEngine } from '@stacksjs/ui'
2
2
  import { useStorage } from '@stacksjs/utils'
3
+ import { computed, ref } from 'vue'
3
4
 
4
5
  // import { client as meilisearch } from './drivers/meilisearch'
5
6
  import { determineState } from './helpers'
6
7
 
7
8
  // import type { Ref } from '@stacksjs/types'
8
9
 
9
- const table = (useStorage('table', determineState()).value)
10
+ const table = useStorage('table', determineState()).value
10
11
  const totalHits = table.results?.estimatedTotalHits ?? 1
11
12
 
12
13
  // state
@@ -42,25 +43,21 @@ export function useSearchEngine() {
42
43
  }
43
44
 
44
45
  export function calculatePagination() {
45
- if (table.perPage)
46
- totalPages.value = Math.ceil(totalHits / table.perPage)
46
+ if (table.perPage) totalPages.value = Math.ceil(totalHits / table.perPage)
47
47
 
48
- const hitPages = [...Array(totalPages.value).keys()].map(i => i + 1)
48
+ const hitPages = [...Array(totalPages.value).keys()].map((i) => i + 1)
49
49
  const offset = 2
50
50
  const currentPage = table.currentPage ?? 1
51
51
  const lastPage = hitPages[hitPages.length - 1]
52
52
 
53
53
  let from = currentPage - offset
54
- if (from < 1)
55
- from = 1
54
+ if (from < 1) from = 1
56
55
 
57
56
  let to = from + offset * 2
58
- if (to >= (lastPage as number))
59
- to = lastPage as number
57
+ if (to >= (lastPage as number)) to = lastPage as number
60
58
 
61
59
  const allPages = []
62
- for (let page = from; page <= to; page++)
63
- allPages.push(page)
60
+ for (let page = from; page <= to; page++) allPages.push(page)
64
61
 
65
62
  pages.value = allPages
66
63
  }
package/dist/helpers.d.ts DELETED
@@ -1,95 +0,0 @@
1
- /**
2
- * This file is used to define the types/interfaces used in the project.
3
- */
4
- import type { Hits, SearchResponse } from 'meilisearch';
5
- export interface SearchEngineStore {
6
- /**
7
- * The search engine index.
8
- */
9
- index: string;
10
- /**
11
- * The search engine host.
12
- * @default 'http://127.0.0.1:7700'
13
- */
14
- source?: string;
15
- /**
16
- * The search engine password/API key.
17
- */
18
- password?: string;
19
- /**
20
- * The search query.
21
- * @default 20
22
- * @type {string}
23
- * @default: ''
24
- */
25
- query?: string;
26
- /**
27
- * The number of results to return.
28
- * @default 20
29
- * @type {number}
30
- */
31
- perPage?: number;
32
- /**
33
- * The current page number
34
- * @default 1
35
- * @type {number}
36
- */
37
- currentPage?: number;
38
- /**
39
- * The results object returned from the search engine.
40
- */
41
- results?: SearchResponse<Record<string, any>>;
42
- /**
43
- * The hits object returned from the search engine.
44
- */
45
- hits?: Hits<Record<string, any>>;
46
- /**
47
- * The search Filter Name
48
- */
49
- filterName?: string;
50
- /**
51
- * The search filters
52
- */
53
- filters?: object;
54
- /**
55
- * The next page value
56
- */
57
- goToNextPage?: number;
58
- /**
59
- * Set to go to a specific page
60
- */
61
- goToPage?: number;
62
- /**
63
- * Set to go to the previous page
64
- */
65
- goToPrevPage?: number;
66
- /**
67
- * The last page number value
68
- */
69
- lastPageNumber?: number;
70
- /**
71
- * The searched term to use for the search
72
- */
73
- search?: string;
74
- /**
75
- * The search params filters to use for the search
76
- */
77
- searchFilters?: object;
78
- /**
79
- * The search params to use for the search
80
- */
81
- searchParams?: object;
82
- /**
83
- * Total hits value to use for the search
84
- */
85
- setTotalHits?: number;
86
- /**
87
- * The sort value to use for the search
88
- */
89
- sort?: string;
90
- /**
91
- * The sorts value to use for the search
92
- */
93
- sorts?: object;
94
- }
95
- export declare function determineState(): SearchEngineStore;
package/dist/index.d.ts DELETED
@@ -1,21 +0,0 @@
1
- export declare const totalPages: any;
2
- export declare const currentPage: any;
3
- export declare const filterName: any;
4
- export declare const filters: any;
5
- export declare const goToNextPage: any;
6
- export declare const goToPage: any;
7
- export declare const goToPrevPage: any;
8
- export declare const hits: any;
9
- export declare const index: any;
10
- export declare const lastPageNumber: any;
11
- export declare const perPage: any;
12
- export declare const query: any;
13
- export declare const results: any;
14
- export declare const searchFilters: any;
15
- export declare const searchParams: any;
16
- export declare const setTotalHits: any;
17
- export declare const sort: any;
18
- export declare const sorts: any;
19
- export declare function client(): string;
20
- export declare function useSearchEngine(): string;
21
- export declare function calculatePagination(): void;