Skip to content

Commit

Permalink
Use Opensearch hybrid search when interacting with Chat AI search
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjarling committed May 7, 2024
1 parent 42c1672 commit f3c343c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 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
46 changes: 40 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,7 +19,11 @@ type BuildQueryProps = {
export function buildQuery(obj: BuildQueryProps) {
const { aggs, aggsFilterValue, size, term, urlFacets } = obj;
const must: QueryDslQueryContainer[] = [];
let queryValue;

const isAiChat = isAiChatActive();

// Build the "must" part of the query
if (term) must.push(buildSearchPart(term));

if (Object.keys(urlFacets).length > 0) {
Expand All @@ -29,14 +34,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,
},
};
}

// We are in AI chat mode and a search term exists
if (isAiChat && term) {
queryValue = {
hybrid: {
queries: [
{
bool: {
must,
},
},
{
neural: {
embedding: {
k: size || 20,
model_id: process.env.NEXT_PUBLIC_OPENSEARCH_MODEL_ID,
query_text: term, // if term has no value, the API returns a 400 error
},
},
},
],
},
};
}

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";
}
13 changes: 10 additions & 3 deletions pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ const SearchPage: NextPage = () => {
});

/**
* Post request to the search API endpoint for query and facet parameters
* Make requests to the search API endpoint
*/
useEffect(() => {
if (!router.isReady) return;
(async () => {
try {
const { page, q } = router.query;
const urlFacets = parseUrlFacets(router.query);
const requestUrl = new URL(DC_API_SEARCH_URL);
const pipeline = process.env.NEXT_PUBLIC_OPENSEARCH_PIPELINE;

// If there is a "similar" facet, get the work and set the state
if (urlFacets?.similar) {
Expand All @@ -93,9 +95,14 @@ const SearchPage: NextPage = () => {
urlFacets,
});

// Request as a "hybrid" OpensSearch query
// @ts-expect-error - 'hybrid' is not in Elasticsearch package types
if (!!body?.query?.hybrid && pipeline) {
requestUrl.searchParams.append("search_pipeline", pipeline);
}
const response = await apiPostRequest<ApiSearchResponse>({
body: body,
url: DC_API_SEARCH_URL,
body,
url: requestUrl.toString(),
});

/**
Expand Down

0 comments on commit f3c343c

Please sign in to comment.