Skip to content

Commit

Permalink
feat: different indexes for differen types of content
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Dec 5, 2023
1 parent df9d5a2 commit b520aa3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
8 changes: 5 additions & 3 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export const onRequestGet = async (context) => {
const { q, p = 0 } = Object.fromEntries(searchParams.entries());

const startTime = Date.now();
const list = q ? await search(env, q, p) : [];
const [blogs, docs] = q ? await search(env, q, p) : [];
const doneIn = Date.now() - startTime;
const hasResults = list.length > 0;
const hasResults = blogs.length > 0 || docs.length > 0;

const view = {
q,
title: 'kukei.eu',
list,
blogs,
docs,
hasResults,
noResults: q && !hasResults,
doneIn,
Expand Down
36 changes: 26 additions & 10 deletions functions/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ <h1>Kukey.eu <span>curated search for web developers</span></h1>
</form>
{{#hasResults}}
<p>Search results generated in {{ doneIn }}ms</p>
<ul>
{{#list}}
<li>
<h2>{{ title }}</h2>
<p>{{ excerpt }}</p>
<blockquote>{{ highlight }}</blockquote>
<a href="{{ url }}">{{ url }}</a>
</li>
{{/list}}
</ul>
<section>
<h2>Blogs</h2>
<ul>
{{#blogs}}
<li>
<h2>{{ title }}</h2>
<p>{{ excerpt }}</p>
<blockquote>{{ highlight }}</blockquote>
<a href="{{ url }}">{{ url }}</a>
</li>
{{/blogs}}
</ul>
</section>
<section>
<h2>Docs</h2>
<ul>
{{#docs}}
<li>
<h2>{{ title }}</h2>
<p>{{ excerpt }}</p>
<blockquote>{{ highlight }}</blockquote>
<a href="{{ url }}">{{ url }}</a>
</li>
{{/docs}}
</ul>
</section>
{{/hasResults}}
{{#noResults}}
<p>No results found</p>
Expand Down
27 changes: 18 additions & 9 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ const getMeiliClient = (env) => {
});
}

export const search = async (env, q, p = 0) => {
const makeOptions = (p) => ({
attributesToHighlight: ['content'],
attributesToCrop: ['content'],
cropLength: 50,
limit: 10,
offset: p,
});

const doSearch = async (env, index, q, p) => {
const meiliClient = getMeiliClient(env);
const meiliResults = await meiliClient.index('index').search(q, {
attributesToHighlight: ['content'],
attributesToCrop: ['content'],
cropLength: 50,
limit: 10,
offset: p,
});
const results = await meiliClient.index(index).search(q, makeOptions(p));

const {
estimatedTotalHits,
hits
} = meiliResults;
} = results;

const final = hits.map((el) => {
const highlightRaw = el._formatted?.content ?? '';
Expand All @@ -36,3 +38,10 @@ export const search = async (env, q, p = 0) => {

return final;
}
export const search = async (env, q, p = 0) => {
const meiliClient = getMeiliClient(env);
const blogs = await doSearch(env, 'blogs', q, p);
const docs = await doSearch(env, 'docs', q, p);

return [blogs, docs];
}

0 comments on commit b520aa3

Please sign in to comment.