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

ILEX-80 - Use Elastic Search Endpoint #72

Open
wants to merge 3 commits into
base: devel
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
2 changes: 1 addition & 1 deletion interlex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ paths:
403:
description: The request cannot be fullfilled due to the permission of your account.
tags:
- API
- API
/ping:
get:
summary: Checks if the server is running
Expand Down
37 changes: 1 addition & 36 deletions orval.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,6 @@ module.exports = {
path: './mock/mutator/customClient.ts',
name: 'customInstance',
},
operations: {
/** TODO : Endpoint returns a 500, it's being reused by multiple endpoints.
* In our use case, we need this operation to ADD Ontologies with or
* without paths.
* */
get_ontologies_ontologies: {
mock: {
data: () => ({
status_code: 200,
message: "",
}),
},
},
get_endpoints_curies_ : {
mock: {
data : mockCuries
}
},
// Override existing endpoint, return mock data for fragment ID ilx_0101431
get_endpoints_ilx: {
mock: {
data: mockTerm,
},
},
patch_endpoints_ilx: {
mock: {
data: mockPatchTermResponse,
},
},
get_endpoints_ilx_get: {
mock: {
data: mockTerm,
},
},
},
allParamsOptional: true,
urlEncodeParameters: true,
},
Expand Down Expand Up @@ -219,7 +184,7 @@ module.exports = {
mock: {
data: mockPatchTermResponse
},
},
},
// Search for specific 'term' and get all results
get_match_terms: {
mock: {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
"@mui/lab": "^5.0.0-alpha.170",
"@mui/material": "^5.15.15",
"@mui/x-tree-view": "^7.5.0",
"body-parser" : "^1.20.3",
"cors" : "^2.8.5",
"d3": "^7.1.1",
"date-fns": "^3.6.0",
"express" : "^4.21.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.23.1",
Expand Down
16 changes: 16 additions & 0 deletions src/api/custom-axios-instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import axios from 'axios';

export const customAxiosInstance = axios.create({
baseURL: 'https://scicrunch.org/api/1', // Base URL for the API
headers: {
'Content-Type': 'application/json',
},
});

customAxiosInstance.interceptors.request.use((config) => {
config.params = {
...config.params,
api_key: 'tvVqyYwrQqolqVfMo45cu31t5uEGx6RZ', // Add the API key to all requests
};
return config;
});
30 changes: 27 additions & 3 deletions src/api/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import * as mockApi from './../../api/endpoints/swaggerMockMissingEndpoints';
import * as api from './../../api/endpoints/interLexURIStructureAPI'
import { TERM, ONTOLOGY, ORGANIZATION } from '../../model/frontend/types'
import curieParser from '../../parsers/curieParser';
import termParser, { getTerm } from '../../parsers/termParser';
import termParser, { elasticSearhParser, getTerm } from '../../parsers/termParser';
import { Curies } from '../../model/frontend/curies';
import axios from 'axios';

const useMockApi = () => mockApi;
const useApi = () => api;
Expand Down Expand Up @@ -117,17 +118,40 @@ export const getCuries = async (term) => {
}

export const getMatchTerms = async (term, filters = {}) => {
const { getMatchTerms } = useMockApi();
const { getEndpointsIlxGet } = useApi();

/** Call Endpoint */
return getMatchTerms("base", term, filters).then((data) => {
return getEndpointsIlxGet("base", term, "jsonld").then((data) => {
return termParser(data, term, filters);
})
.catch((error) => {
return error;
});
}

// Elastic search client function
export const elasticSearch = async (query) => {
const proxyUrl = 'http://localhost:3000/api/scicrunch';

const data = {
query: query
};

try {
const response = await axios.post(proxyUrl, data, {
headers: {
'Content-Type': 'application/json',
},
});

const newData = elasticSearhParser(response?.data?.data?.hits?.hits);
return newData;
} catch (error) {
console.error('Elastic search error:', error);
throw error;
}
};

export const searchAll = async (term, filters = {}) => {
const { searchAll } = useMockApi();

Expand Down
46 changes: 46 additions & 0 deletions src/api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
const PORT = 3000;

app.use(cors());
app.use(bodyParser.json());

app.post('/api/scicrunch', async (req, res) => {
const apiUrl = 'https://scicrunch.org/api/1/term/elastic/search';
const apiKey = 'tvVqyYwrQqolqVfMo45cu31t5uEGx6RZ';

try {
// Extract the query from the request body
const query = req.body.query;

if (!query) {
return res.status(400).json({ message: "Missing 'query' in request body." });
}

// Make the GET request to the external API
const response = await axios.get(apiUrl, {
params: {
api_key: apiKey,
query: JSON.stringify({ match: { label: query } }),
},
});

console.log("Response from ElasticSearch:", response.data);
res.status(response.status).json(response.data);
} catch (error) {
console.error("Error in proxy:", error.response?.data || error.message);
res.status(error.response?.status || 500).json({
message: error.message,
error: error.response?.data,
});
}
});

// Start server
app.listen(PORT, () => {
console.log(`Proxy server running at http://localhost:${PORT}`);
});
12 changes: 6 additions & 6 deletions src/components/Header/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@mui/material";
import { vars } from "../../theme/variables";
import { useEffect, useState, useCallback, forwardRef } from 'react';
import { searchAll } from "../../api/endpoints";
import { searchAll, elasticSearch } from "../../api/endpoints";
import { CloseIcon, ForwardIcon, SearchIcon, TermsIcon } from '../../Icons';
import FolderOutlinedIcon from '@mui/icons-material/FolderOutlined';
import CorporateFareOutlinedIcon from '@mui/icons-material/CorporateFareOutlined';
Expand Down Expand Up @@ -100,7 +100,7 @@ const Search = () => {
setSearchTerm("");
setSelectedValue(newInputValue?.label);
handleCloseList();
navigate(`/view?searchTerm=${newInputValue?.label}`);
navigate(`/view?searchTerm=${newInputValue?.ilx}`);
};

const handleSearchTermClick = () => {
Expand Down Expand Up @@ -132,10 +132,10 @@ const Search = () => {
}, [handleKeyDown]);

const fetchTerms = useCallback(debounce(async (searchTerm) => {
const data = await searchAll(searchTerm);
const dataTerms = data?.results.filter(result => result.type === "TERM")
const dataOrganizations = data?.results.filter(result => result.type === "ORGANIZATION")
const dataOntologies = data?.results.filter(result => result.type === "ONTOLOGY")
const data = await elasticSearch(searchTerm);
const dataTerms = data?.results.filter(result => result.type === "term")
const dataOrganizations = data?.results.filter(result => result.type === "organization")
const dataOntologies = data?.results.filter(result => result.type === "ontology")
setTerms(dataTerms);
setOrganizations(dataOrganizations)
setOntologies(dataOntologies)
Expand Down
22 changes: 22 additions & 0 deletions src/parsers/termParser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,26 @@ export const termParser = (data, searchTerm, start?, end?) => {
}
};

export const elasticSearhParser = (data) => {
let terms : Terms;
if ( Array.isArray(data) ){
terms = data?.map( term => {
let newTerm : Term = {} as Term
term = term._source
return term
})

// We are receiving an unknown amout of terms from server, we need to control
// how much to send back based on request made (start,end)
const results = terms;
const filters = getFilters(results);
return {
filters : filters,
results : results
}
} else {
return { filters : {} ,results : []}
}
};

export default termParser;
Loading