Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Relevancy With Title Boost and Local Setup #149

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/config/elasticsearch.ts
Original file line number Diff line number Diff line change
@@ -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 { client };
5 changes: 2 additions & 3 deletions src/context/SearchQueryContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
20 changes: 17 additions & 3 deletions src/utils/server/apiFunctions.ts
Original file line number Diff line number Diff line change
@@ -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^3", "body", "authors"];

// Omitting 'page' from SearchQuery as 'from' is used for Elasticsearch pagination
type BuildQueryForElaSticClient = Omit<SearchQuery, "page"> & {
Expand Down Expand Up @@ -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
Expand All @@ -91,7 +92,20 @@ 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,
Expand Down