From 08957d5e82849769a408ecc5a5a10a5ed1d1cdde Mon Sep 17 00:00:00 2001 From: elraphty Date: Sun, 27 Oct 2024 18:34:43 +0100 Subject: [PATCH 1/6] listed event tickets --- src/config/elasticsearch.ts | 22 ++++++++++++++++------ src/pages/api/elasticSearchProxy/search.ts | 2 +- src/service/api/search/searchCall.ts | 1 + src/utils/server/apiFunctions.ts | 22 +++++++++++++++++++--- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/config/elasticsearch.ts b/src/config/elasticsearch.ts index 52a14f4e..af746eb5 100644 --- a/src/config/elasticsearch.ts +++ b/src/config/elasticsearch.ts @@ -1,8 +1,18 @@ import { Client } from "@elastic/elasticsearch"; -export const client = new Client({ - cloud: { - id: process.env.CLOUD_ID, // cloud id found under your cloud deployment overview page - }, - auth: { apiKey: process.env.API_KEY }, -}); +let client: Client; + +if (process.env.CLOUD_ID && process.env.API_KEY) { + client = new Client({ + cloud: { + id: process.env.CLOUD_ID, // cloud id found under your cloud deployment overview page + }, + auth: { apiKey: process.env.API_KEY }, + }); +} else { + client = new Client({ + node: process.env.ES_LOCAL_URL, + }); +} + +export default client; diff --git a/src/pages/api/elasticSearchProxy/search.ts b/src/pages/api/elasticSearchProxy/search.ts index 508c1c66..b0d5dada 100644 --- a/src/pages/api/elasticSearchProxy/search.ts +++ b/src/pages/api/elasticSearchProxy/search.ts @@ -1,5 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import { client } from "@/config/elasticsearch"; +import client from "@/config/elasticsearch"; import { buildQuery } from "@/utils/server/apiFunctions"; // import ElasticsearchAPIConnector from "@elastic/search-ui-elasticsearch-connector"; diff --git a/src/service/api/search/searchCall.ts b/src/service/api/search/searchCall.ts index 2b973460..d20e5f10 100644 --- a/src/service/api/search/searchCall.ts +++ b/src/service/api/search/searchCall.ts @@ -19,6 +19,7 @@ export const buildQueryCall: BuildQuery = async ( const jsonBody = JSON.stringify(body); + return fetch(url ?? "/api/elasticSearchProxy/search", { method: "POST", headers: { diff --git a/src/utils/server/apiFunctions.ts b/src/utils/server/apiFunctions.ts index c88ebb91..cbba9fbf 100644 --- a/src/utils/server/apiFunctions.ts +++ b/src/utils/server/apiFunctions.ts @@ -1,7 +1,7 @@ import { aggregatorSize } from "@/config/config"; import type { Facet, SearchQuery } from "@/types"; -const FIELDS_TO_SEARCH = ["authors", "title", "body"]; +const FIELDS_TO_SEARCH = ["title", "body", "authors"]; // Omitting 'page' from SearchQuery as 'from' is used for Elasticsearch pagination type BuildQueryForElaSticClient = Omit & { @@ -65,10 +65,11 @@ export const buildQuery = ({ // Construct and add the full-text search clause let shouldClause = buildShouldQueryClause(queryString); + let mustClause = buildMustQueryClause(queryString); if (!queryString) { baseQuery.query.bool.should.push(shouldClause); } else { - baseQuery.query.bool.must.push(shouldClause); + baseQuery.query.bool.must.push(mustClause); } // Add filter clauses for each specified filter field @@ -91,7 +92,21 @@ export const buildQuery = ({ }; // Helper to build the should query clause for full-text search -const buildShouldQueryClause = (queryString: string) => { +const buildMustQueryClause = (queryString: string) => { + + let shouldQueryClause = { + multi_match: { + query: queryString, + fields: FIELDS_TO_SEARCH, + "fuzziness": 0, + "minimum_should_match": "75%", + }, + }; + + return shouldQueryClause; +}; + +const buildShouldQueryClause = (queryString: string): any => { let shouldQueryClause = { multi_match: { query: queryString, @@ -102,6 +117,7 @@ const buildShouldQueryClause = (queryString: string) => { return shouldQueryClause; }; + // Helper to build filter query clauses based on facets const buildFilterQueryClause = ({ field, value }: Facet) => { let filterQueryClause = { From 2a6ae053b865a78dce7118e897a6e008e75d7b66 Mon Sep 17 00:00:00 2001 From: elraphty Date: Mon, 28 Oct 2024 15:39:28 +0100 Subject: [PATCH 2/6] use minimum_should_match and fuzziness for relevancy --- src/pages/index.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 1215b1f6..077e48cb 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -55,9 +55,9 @@ export const getServerSideProps: GetServerSideProps = async ( }; } - const page = pageQuery ? parseInt(pageQuery) - 1 ?? 0 : 0; + const page = pageQuery ? parseInt(pageQuery) - 1 : 0; const size = sizeQuery - ? parseInt(sizeQuery) ?? defaultParam[URLSearchParamsKeyword.SIZE] + ? parseInt(sizeQuery) || defaultParam[URLSearchParamsKeyword.SIZE] : defaultParam[URLSearchParamsKeyword.SIZE]; const options = { @@ -104,9 +104,8 @@ export const App = () => { return (
Date: Mon, 28 Oct 2024 18:29:42 +0100 Subject: [PATCH 3/6] fixed ureachable code error --- src/context/SearchQueryContext.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/context/SearchQueryContext.tsx b/src/context/SearchQueryContext.tsx index e45c3aa2..24281b40 100644 --- a/src/context/SearchQueryContext.tsx +++ b/src/context/SearchQueryContext.tsx @@ -78,13 +78,12 @@ export const SearchQueryProvider = ({ const searchQuery = useMemo(() => { return rawSearchQuery ?? ""; }, [rawSearchQuery]); - const page = useMemo(() => { - return pageQuery ? parseInt(pageQuery) - 1 ?? 0 : 0; + return pageQuery ? parseInt(pageQuery) - 1 : 0; }, [pageQuery]); const resultsPerPage = sizeQuery - ? parseInt(sizeQuery) ?? defaultParam[URLSearchParamsKeyword.SIZE] + ? parseInt(sizeQuery) || defaultParam[URLSearchParamsKeyword.SIZE] : defaultParam[URLSearchParamsKeyword.SIZE]; const setSearchParams = useCallback( From 91d4ec29088ba7de05806e7b2290f02b6484c9cb Mon Sep 17 00:00:00 2001 From: elraphty Date: Mon, 28 Oct 2024 18:34:43 +0100 Subject: [PATCH 4/6] changed client export default to export --- src/config/elasticsearch.ts | 2 +- src/pages/api/elasticSearchProxy/search.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/elasticsearch.ts b/src/config/elasticsearch.ts index af746eb5..89fc8d57 100644 --- a/src/config/elasticsearch.ts +++ b/src/config/elasticsearch.ts @@ -15,4 +15,4 @@ if (process.env.CLOUD_ID && process.env.API_KEY) { }); } -export default client; +export { client }; diff --git a/src/pages/api/elasticSearchProxy/search.ts b/src/pages/api/elasticSearchProxy/search.ts index b0d5dada..508c1c66 100644 --- a/src/pages/api/elasticSearchProxy/search.ts +++ b/src/pages/api/elasticSearchProxy/search.ts @@ -1,5 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import client from "@/config/elasticsearch"; +import { client } from "@/config/elasticsearch"; import { buildQuery } from "@/utils/server/apiFunctions"; // import ElasticsearchAPIConnector from "@elastic/search-ui-elasticsearch-connector"; From 8d34743b39d66570ce00da88740e93c33c044350 Mon Sep 17 00:00:00 2001 From: elraphty Date: Mon, 28 Oct 2024 18:39:18 +0100 Subject: [PATCH 5/6] fixed prettier errors --- src/pages/index.tsx | 5 +++-- src/service/api/search/searchCall.ts | 1 - src/utils/server/apiFunctions.ts | 6 ++---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 077e48cb..5edaa892 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -104,8 +104,9 @@ export const App = () => { return (
{ - let shouldQueryClause = { multi_match: { query: queryString, fields: FIELDS_TO_SEARCH, - "fuzziness": 0, - "minimum_should_match": "75%", + fuzziness: 0, + minimum_should_match: "75%", }, }; @@ -117,7 +116,6 @@ const buildShouldQueryClause = (queryString: string): any => { return shouldQueryClause; }; - // Helper to build filter query clauses based on facets const buildFilterQueryClause = ({ field, value }: Facet) => { let filterQueryClause = { From e8bb2a76c3999bec2e5376bfcba5d9136b24046c Mon Sep 17 00:00:00 2001 From: elraphty Date: Mon, 4 Nov 2024 14:38:35 +0100 Subject: [PATCH 6/6] boosted title --- src/utils/server/apiFunctions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/server/apiFunctions.ts b/src/utils/server/apiFunctions.ts index 3471e7fa..fbf9ab83 100644 --- a/src/utils/server/apiFunctions.ts +++ b/src/utils/server/apiFunctions.ts @@ -1,7 +1,7 @@ import { aggregatorSize } from "@/config/config"; import type { Facet, SearchQuery } from "@/types"; -const FIELDS_TO_SEARCH = ["title", "body", "authors"]; +const FIELDS_TO_SEARCH = ["title^3", "body", "authors"]; // Omitting 'page' from SearchQuery as 'from' is used for Elasticsearch pagination type BuildQueryForElaSticClient = Omit & {