Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7112666
meilisearch.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
meilisearch.ts
View Options
import
type
{
Context
}
from
"koa"
;
import
{
MeiliSearch
}
from
"meilisearch"
;
import
type
{
CollectionItem
}
from
"sealious"
;
import
{
sleep
}
from
"../util.js"
;
import
{
MEILISEARCH_HOST
,
MEILISEARCH_MASTER_KEY
}
from
"./../config.js"
;
let
meilisearch
:
MeiliSearch
|
null
=
null
;
export
const
getMeilisearch
=
()
=>
{
if
(
!
meilisearch
)
{
meilisearch
=
new
MeiliSearch
({
host
:
MEILISEARCH_HOST
,
apiKey
:
MEILISEARCH_MASTER_KEY
,
});
}
return
meilisearch
;
};
export
const
createIndex
=
async
(
name
:
string
)
=>
{
try
{
const
ms
=
getMeilisearch
();
const
indexes
=
await
ms
.
getIndexes
();
const
indexExists
=
indexes
.
results
.
find
((
idx
)
=>
idx
.
uid
===
name
);
if
(
!
indexExists
)
{
await
getMeilisearch
().
createIndex
(
name
,
{
primaryKey
:
"id"
});
}
}
catch
(
error
)
{
throw
new
Error
(
`Error when creating index
${
name
}
:
${
error
}
`
);
}
};
const
indexes
=
{}
as
Record
<
string
,
{
getItems
:
(
ctx
:
Context
,
ids
:
string
[])
=>
CollectionItem
[]
}
>
;
export
async
function
search
(
ctx
:
Context
,
query
:
string
,
use_indexes
:
Array
<
keyof
typeof
indexes
>
)
{
const
meilisearch
=
getMeilisearch
();
const
queries
=
use_indexes
.
map
((
index_name
:
string
)
=>
({
indexUid
:
index_name
,
q
:
query
,
rankingScoreThreshold
:
0.2
,
matchingStrategy
:
<
const
>
"frequency"
,
}));
const
{
results
}
=
await
meilisearch
.
multiSearch
({
queries
,
});
const
result
=
Object
.
fromEntries
(
await
Promise
.
all
(
use_indexes
.
map
(
async
(
index_name
,
key
)
=>
{
const
ids
=
results
[
key
]
!
.
hits
.
map
((
e
)
=>
e
.
id
);
const
items
=
await
indexes
[
index_name
]
?
.
getItems
(
ctx
,
ids
);
return
[
index_name
,
items
];
})
)
);
const
ret
=
{
...
Object
.
fromEntries
(
Object
.
keys
(
indexes
).
map
((
index_name
)
=>
[
index_name
,
[]])),
...
result
,
};
return
ret
;
}
export
const
isMeilisearchRunning
=
async
()
=>
{
try
{
const
response
=
await
fetch
(
`
${
MEILISEARCH_HOST
}
/health`
);
if
(
response
.
status
===
200
)
{
return
true
;
}
else
{
return
false
;
}
}
catch
(
e
)
{
return
false
;
}
};
export
async
function
waitForMeilisearch
(
timeout_s
:
number
)
{
const
time_start
=
Date
.
now
();
// eslint-disable-next-line no-await-in-loop
while
(
!
(
await
isMeilisearchRunning
()))
{
// eslint-disable-next-line no-await-in-loop
await
sleep
(
1000
);
if
((
Date
.
now
()
-
time_start
)
/
1000
>
timeout_s
)
{
throw
new
Error
(
"timeout"
);
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Jul 4, 00:41 (22 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
799752
Default Alt Text
meilisearch.ts (2 KB)
Attached To
Mode
rPLAY Sealious playground
Attached
Detach File
Event Timeline
Log In to Comment