feat(server): replace existing search functionality with searxng library
This commit is contained in:
parent
96f67c7028
commit
28077018a6
11 changed files with 460 additions and 519 deletions
|
@ -1,47 +1,19 @@
|
|||
import axios from 'axios';
|
||||
import { getSearxngApiEndpoint } from '../config';
|
||||
|
||||
interface SearxngSearchOptions {
|
||||
categories?: string[];
|
||||
engines?: string[];
|
||||
language?: string;
|
||||
pageno?: number;
|
||||
}
|
||||
import { SearxngService, type SearxngSearchParameters } from 'searxng';
|
||||
|
||||
interface SearxngSearchResult {
|
||||
title: string;
|
||||
url: string;
|
||||
img_src?: string;
|
||||
thumbnail_src?: string;
|
||||
thumbnail?: string;
|
||||
content?: string;
|
||||
author?: string;
|
||||
iframe_src?: string;
|
||||
}
|
||||
const searxng = new SearxngService({
|
||||
baseURL: getSearxngApiEndpoint(),
|
||||
defaultSearchParams: {
|
||||
format: 'json'
|
||||
}
|
||||
})
|
||||
|
||||
export const searchSearxng = async (
|
||||
query: string,
|
||||
opts?: SearxngSearchOptions,
|
||||
opts?: SearxngSearchParameters,
|
||||
) => {
|
||||
const searxngURL = getSearxngApiEndpoint();
|
||||
|
||||
const url = new URL(`${searxngURL}/search?format=json`);
|
||||
url.searchParams.append('q', query);
|
||||
|
||||
if (opts) {
|
||||
Object.keys(opts).forEach((key) => {
|
||||
if (Array.isArray(opts[key])) {
|
||||
url.searchParams.append(key, opts[key].join(','));
|
||||
return;
|
||||
}
|
||||
url.searchParams.append(key, opts[key]);
|
||||
});
|
||||
}
|
||||
|
||||
const res = await axios.get(url.toString());
|
||||
|
||||
const results: SearxngSearchResult[] = res.data.results;
|
||||
const suggestions: string[] = res.data.suggestions;
|
||||
|
||||
const { results, suggestions } = await searxng.search(query, opts);
|
||||
return { results, suggestions };
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue