Skip to content

Commit

Permalink
#17: Search function
Browse files Browse the repository at this point in the history
  • Loading branch information
darnjo committed Feb 10, 2023
1 parent 8623293 commit 8d51252
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 6 deletions.
8 changes: 5 additions & 3 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ app.post("/uli-service/v1/ingest/:providerUoi", async (req, res) => {
res.send({
statusCode: 200,
body: {
itemsProcessed: data?.length || 0
}
itemsProcessed: data?.length || 0,
},
});
} catch (err) {
console.log(err);
Expand All @@ -55,9 +55,11 @@ app.post("/uli-service/v1/ingest/:providerUoi", async (req, res) => {

app.post("/uli-service/v1/search", async (req, res) => {
try {
const { explain = false } = req?.query;

res.send({
statusCode: 200,
body: await search(),
body: await search(req?.body, explain),
});
} catch (err) {
console.error(err);
Expand Down
212 changes: 212 additions & 0 deletions api/services/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
"use strict";

const ULI_TEMPLATE = {
MemberFullName: {
filter: {
fuzzy: {
MemberFullName: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 10,
},
MemberLastName: {
filter: {
fuzzy: {
MemberLastName: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 1,
},
MemberFirstName: {
filter: {
fuzzy: {
MemberFirstName: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 1,
},
MemberMiddleInitial: {
filter: {
fuzzy: {
MemberMiddleInitial: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 1,
},
MemberNickname: {
filter: {
fuzzy: {
MemberNickname: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 3,
},
MemberType: {
filter: {
fuzzy: {
MemberType: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 3,
},
MemberNationalAssociationId: {
filter: {
fuzzy: {
MemberNationalAssociationId: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 10,
},
MemberStateLicense: {
filter: {
fuzzy: {
MemberStateLicense: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 10,
},
MemberStateLicenseType: {
filter: {
fuzzy: {
MemberStateLicenseType: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 3,
},
MemberStateLicenseState: {
filter: {
fuzzy: {
MemberStateLicenseState: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 6,
},
MemberMlsId: {
filter: {
fuzzy: {
MemberMlsId: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 5,
},
OfficeName: {
filter: {
fuzzy: {
OfficeName: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 2,
},
OfficeMlsId: {
filter: {
fuzzy: {
OfficeMlsId: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 10,
},
SourceSystemID: {
filter: {
fuzzy: {
SourceSystemID: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 10,
},
SourceSystemName: {
filter: {
fuzzy: {
SourceSystemName: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 5,
},
OriginatingSystemID: {
filter: {
fuzzy: {
OriginatingSystemID: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 10,
},
OriginatingSystemName: {
filter: {
fuzzy: {
OriginatingSystemName: {
value: null,
fuzziness: 2,
transpositions: true,
},
},
},
weight: 5,
},
};

module.exports = {
ULI_TEMPLATE,
};
35 changes: 32 additions & 3 deletions api/services/data-access.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";
const { get, post } = require("axios");
const { query } = require("express");
const { ULI_TEMPLATE } = require("./const");

const ES_HOST = process.env.ES_HOST || "localhost";
const ES_URL = "http://" + ES_HOST + ":9200";
Expand All @@ -15,10 +17,37 @@ const indexExists = async (indexName) => {
}
};

const search = async () => {
const createFilters = (fieldValues = []) =>
fieldValues.flatMap(({ fieldName, value }) => {
if (value && ULI_TEMPLATE[fieldName]) {
const filterValue = ULI_TEMPLATE[fieldName];
filterValue.filter.fuzzy[fieldName].value = value;
return filterValue;
} else {
return [];
}
});

const search = async (fieldValues = [], explain = false) => {
try {
const { data } = await get(`${ES_URL}/${ULI_SERVICE_INDEX}/_search`);
return data?.hits?.hits || [];
const queryParams = {
query: {
function_score: {
boost: 1,
functions: createFilters(fieldValues),
max_boost: 10,
score_mode: "multiply",
boost_mode: "multiply",
min_score: 7
},
},
explain
};
const { data } = await get(`${ES_URL}/${ULI_SERVICE_INDEX}/_search`, {
data: queryParams,
});

return data?.hits || [];
} catch (err) {
console.error(err);
return [];
Expand Down

0 comments on commit 8d51252

Please sign in to comment.