Skip to content

Commit

Permalink
Update the OpenSearch query builder code to use a hybrid query if use…
Browse files Browse the repository at this point in the history
…r is searching via AI Chat
  • Loading branch information
adamjarling committed May 6, 2024
1 parent 42c1672 commit e63b659
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
2 changes: 2 additions & 0 deletions components/Facets/Filter/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Dialog from "@radix-ui/react-dialog";

import {
FilterBody,
FilterBodyInner,
Expand All @@ -7,6 +8,7 @@ import {
FilterHeader,
} from "@/components/Facets/Filter/Filter.styled";
import React, { useEffect, useState } from "react";

import { ApiSearchRequestBody } from "@/types/api/request";
import { ApiSearchResponse } from "@/types/api/response";
import { DC_API_SEARCH_URL } from "@/lib/constants/endpoints";
Expand Down
45 changes: 39 additions & 6 deletions lib/queries/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { QueryDslQueryContainer } from "@elastic/elasticsearch/api/types";
import { UrlFacets } from "@/types/context/filter-context";
import { buildAggs } from "@/lib/queries/aggs";
import { buildFacetFilters } from "@/lib/queries/facet";
import { isAiChatActive } from "../utils/get-url-search-params";

type BuildQueryProps = {
aggs?: FacetsInstance[];
Expand All @@ -18,6 +19,9 @@ type BuildQueryProps = {
export function buildQuery(obj: BuildQueryProps) {
const { aggs, aggsFilterValue, size, term, urlFacets } = obj;
const must: QueryDslQueryContainer[] = [];
let queryValue;

const isAiChat = isAiChatActive();

if (term) must.push(buildSearchPart(term));

Expand All @@ -29,14 +33,43 @@ export function buildQuery(obj: BuildQueryProps) {
}
}

// User facets exist and we are not in AI chat mode
if (must.length > 0 && !isAiChat) {
queryValue = {
bool: {
must: must,
},
};
}

// We are in AI chat mode
if (isAiChat) {
queryValue = {
hybrid: {
queries: [
{
bool: {
must: must,
},
},
{
neural: {
embedding: {
k: 5,
model_id: process.env.NEXT_PUBLIC_OPENSEARCH_MODEL_ID,
query_text: term,
},
},
},
],
},
};
}

return {
...querySearchTemplate,
...(must.length > 0 && {
query: {
bool: {
must: must,
},
},
...(queryValue && {
query: queryValue,
}),
...(aggs && { aggs: buildAggs(aggs, aggsFilterValue, urlFacets) }),
...(typeof size !== "undefined" && { size: size }),
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/get-url-search-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export function getUrlSearchParams(url: string) {

return paramsObj;
}

export function isAiChatActive() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get("ai") === "true";
}
16 changes: 15 additions & 1 deletion pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,23 @@ const SearchPage: NextPage = () => {
urlFacets,
});

const postUrl = new URL(DC_API_SEARCH_URL);

// Pipeline value is necessary to power hybrid OpensSearch queries
if (
// @ts-expect-error - 'hybrid' is not in Elasticsearch package types
!!body?.query?.hybrid &&
process.env.NEXT_PUBLIC_OPENSEARCH_PIPELINE
) {
postUrl.searchParams.append(
"search_pipeline",
process.env.NEXT_PUBLIC_OPENSEARCH_PIPELINE
);
}

const response = await apiPostRequest<ApiSearchResponse>({
body: body,
url: DC_API_SEARCH_URL,
url: postUrl.toString(),
});

/**
Expand Down

0 comments on commit e63b659

Please sign in to comment.