Skip to content

Commit

Permalink
refactor(indexes): move index names from env to config
Browse files Browse the repository at this point in the history
Moves index names from environment variables to config.ts since they
aren't sensitive data. This provides type-safety and makes it easier to
add new indexes. Updates API endpoints, components and
documentation to use the new configuration.
  • Loading branch information
kouloumos committed Jan 28, 2025
1 parent 0faab70 commit 9b54bf4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ The `.env` looks like:
```bash
API_KEY="0000000000000000000164dbb81fbea0a98f09eae1ff2a51493cb3a633523891=="
CLOUD_ID="Deployment_name:0000000000000000000365ff7535528e43b5c6793e840c2b2a0a38e1648c930f"
INDEX="index-name"
COREDEV_INDEX="coredev-index-name"
```

### Indexes Configuration

The application supports multiple Elasticsearch indexes. The configuration for these indexes can be found in [`src/config/config.ts`](/src/config/config.ts). To add or modify indexes, update the `INDEXES` object.

## Installation

```bash
Expand Down
20 changes: 16 additions & 4 deletions src/components/explore/IndexSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import React from "react";
import { INDEXES, IndexType } from "@/config/config";

const IndexSelector = ({ selectedIndex, onIndexChange }) => {
interface IndexSelectorProps {
selectedIndex: IndexType;
onIndexChange: (index: IndexType) => void;
}

const IndexSelector: React.FC<IndexSelectorProps> = ({
selectedIndex,
onIndexChange,
}) => {
return (
<div className="flex items-center gap-2">
<label className="text-sm text-custom-secondary-text">Index:</label>
<select
value={selectedIndex}
onChange={(e) => onIndexChange(e.target.value)}
onChange={(e) => onIndexChange(e.target.value as IndexType)}
className="text-sm px-2 py-1 bg-custom-background border border-custom-stroke rounded
text-custom-secondary-text hover:border-custom-accent focus:border-custom-accent
focus:outline-none cursor-pointer transition-colors duration-200"
>
<option value="main">Main</option>
<option value="coredev">Core Dev</option>
{Object.values(INDEXES).map((index) => (
<option key={index.id} value={index.id}>
{index.label}
</option>
))}
</select>
</div>
);
Expand Down
21 changes: 21 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,24 @@ export const TruncateLengthInChar = 300;
export const TruncateLinkInChar = 50;

export const aggregatorSize = 100;

export const INDEXES = {
MAIN: {
id: "main",
label: "Main",
indexName: "bitcoin-search-august-23",
},
CORE_DEV: {
id: "coredev",
label: "Core Dev",
indexName: "bitcoin-core-august-23",
},
} as const;

export type IndexType = (typeof INDEXES)[keyof typeof INDEXES]["id"];

export const getIndexConfig = (id: IndexType) => {
return (
Object.values(INDEXES).find((index) => index.id === id) ?? INDEXES.MAIN
);
};
8 changes: 4 additions & 4 deletions src/pages/api/elasticSearchProxy/getDocumentContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { client } from "@/config/elasticsearch";
import { getIndexConfig, IndexType } from "@/config/config";

export default async function handler(
req: NextApiRequest,
Expand All @@ -20,13 +21,12 @@ export default async function handler(
});
}

// Select index based on parameter
const selectedIndex =
index === "coredev" ? process.env.COREDEV_INDEX : process.env.INDEX;
// Get the actual index name from our config
const indexConfig = getIndexConfig(index as IndexType);

try {
const result = await client.search({
index: selectedIndex,
index: indexConfig.indexName,
body: {
// This query handles two different indexing patterns across our ES indexes:
// - In index A: document's _id matches its content 'id' field
Expand Down
9 changes: 4 additions & 5 deletions src/pages/api/elasticSearchProxy/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { client } from "@/config/elasticsearch";
import { buildQuery } from "@/utils/server/apiFunctions";
import { getIndexConfig, IndexType } from "@/config/config";

export default async function handler(
req: NextApiRequest,
Expand All @@ -23,9 +24,8 @@ export default async function handler(
index = "main",
} = req.body;

// Select index based on parameter
const selectedIndex =
index === "coredev" ? process.env.COREDEV_INDEX : process.env.INDEX;
// Get the actual index name from our config
const indexConfig = getIndexConfig(index as IndexType);

const from = page * size;
let searchQuery = buildQuery({
Expand All @@ -38,9 +38,8 @@ export default async function handler(
});

try {
// Call the search method
const result = await client.search({
index: selectedIndex,
index: indexConfig.indexName,
...searchQuery,
});

Expand Down
3 changes: 2 additions & 1 deletion src/pages/explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NavBar from "@/components/navBar/NavBar";
import Footer from "@/components/footer/Footer";
import { useExplore } from "@/hooks/useExplore";
import IndexSelector from "@/components/explore/IndexSelector";
import { IndexType } from "@/config/config";

interface ExplorerField {
key: string;
Expand All @@ -29,7 +30,7 @@ const EXPLORER_CONFIGS: Record<string, ExplorerField> = {
const ExplorePage = () => {
const [activeType, setActiveType] = useState("sources");
const [expandedItem, setExpandedItem] = useState<string | null>(null);
const [selectedIndex, setSelectedIndex] = useState<string>("main");
const [selectedIndex, setSelectedIndex] = useState<IndexType>("main");

const config = EXPLORER_CONFIGS[activeType];
const { data, isLoading, isError, error } = useExplore(
Expand Down

0 comments on commit 9b54bf4

Please sign in to comment.