👇🏼 Inferring issue metadata upon import
This commit is contained in:
@@ -7,21 +7,13 @@ import {
|
||||
Errors,
|
||||
} from "moleculer";
|
||||
|
||||
const { Client } = require("@elastic/elasticsearch");
|
||||
const client = new Client({
|
||||
node: "http://tower.local:9200",
|
||||
auth: {
|
||||
username: "elastic",
|
||||
password: "password",
|
||||
},
|
||||
});
|
||||
|
||||
import { DbMixin } from "../mixins/db.mixin";
|
||||
import Comic from "../models/comic.model";
|
||||
import { refineQuery } from "filename-parser";
|
||||
import { filter, isEmpty, isNull } from "lodash";
|
||||
|
||||
console.log(client);
|
||||
import { each, filter, flatten, isEmpty, isNull } from "lodash";
|
||||
import { eSClient } from "../models/comic.model";
|
||||
import arrayToNDJSON from "array-to-ndjson";
|
||||
const s = eSClient.helpers.msearch();
|
||||
|
||||
export default class SettingsService extends Service {
|
||||
// @ts-ignore
|
||||
@@ -34,7 +26,7 @@ export default class SettingsService extends Service {
|
||||
Service.mergeSchemas(
|
||||
{
|
||||
name: "search",
|
||||
mixins: [client, DbMixin("comics", Comic)],
|
||||
mixins: [DbMixin("comics", Comic)],
|
||||
hooks: {},
|
||||
actions: {
|
||||
searchComic: {
|
||||
@@ -43,95 +35,55 @@ export default class SettingsService extends Service {
|
||||
timeout: 400000,
|
||||
async handler(
|
||||
ctx: Context<{
|
||||
queryObject: {
|
||||
issueName: string;
|
||||
volumeName: string;
|
||||
issueNumber: string;
|
||||
};
|
||||
queryObjects: [],
|
||||
elasticSearchQueries: [
|
||||
{
|
||||
elasticSearchQuery: object;
|
||||
}
|
||||
];
|
||||
}>
|
||||
) {
|
||||
let elasticSearchQuery = {};
|
||||
console.log(
|
||||
"Volume: ",
|
||||
ctx.params.queryObject.volumeName
|
||||
const flattenedQueryArray = flatten(
|
||||
ctx.params.elasticSearchQueries
|
||||
);
|
||||
console.log(
|
||||
"Issue: ",
|
||||
ctx.params.queryObject.issueName
|
||||
);
|
||||
if (isNull(ctx.params.queryObject.volumeName)) {
|
||||
elasticSearchQuery = {
|
||||
match: {
|
||||
"rawFileDetails.name": {
|
||||
query: ctx.params.queryObject
|
||||
.issueName,
|
||||
operator: "and",
|
||||
fuzziness: "AUTO",
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (
|
||||
isNull(ctx.params.queryObject.issueName)
|
||||
) {
|
||||
elasticSearchQuery = {
|
||||
match: {
|
||||
"rawFileDetails.name": {
|
||||
query: ctx.params.queryObject
|
||||
.volumeName,
|
||||
operator: "and",
|
||||
fuzziness: "AUTO",
|
||||
},
|
||||
},
|
||||
};
|
||||
} else {
|
||||
elasticSearchQuery = {
|
||||
bool: {
|
||||
should: [
|
||||
{
|
||||
match_phrase: {
|
||||
"rawFileDetails.name":
|
||||
ctx.params
|
||||
.queryObject
|
||||
.issueName,
|
||||
},
|
||||
},
|
||||
{
|
||||
match_phrase: {
|
||||
"rawFileDetails.name":
|
||||
ctx.params
|
||||
.queryObject
|
||||
.volumeName,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
console.log(elasticSearchQuery);
|
||||
return Comic.esSearch({
|
||||
query: elasticSearchQuery,
|
||||
}).then(function (results) {
|
||||
// results here
|
||||
const foo = results.body.hits.hits.map(
|
||||
(hit) => {
|
||||
const parsedFilename = refineQuery(
|
||||
hit._source.rawFileDetails.name
|
||||
);
|
||||
if (
|
||||
parsedFilename.searchParams
|
||||
.searchTerms.number ===
|
||||
parseInt(
|
||||
ctx.params.queryObject
|
||||
.issueNumber,
|
||||
10
|
||||
)
|
||||
) {
|
||||
return hit;
|
||||
}
|
||||
}
|
||||
);
|
||||
return filter(foo, null);
|
||||
let queries = flattenedQueryArray
|
||||
.map((item) => JSON.stringify(item))
|
||||
.join("\n");
|
||||
queries += "\n";
|
||||
const { body } = await eSClient.msearch({
|
||||
body: queries,
|
||||
});
|
||||
|
||||
body.responses.forEach((match) => {
|
||||
console.log(match.hits.hits);
|
||||
})
|
||||
|
||||
return body.responses;
|
||||
|
||||
// return Comic.esSearch({
|
||||
// query: elasticSearchQuery,
|
||||
// }).then(function (results) {
|
||||
// // results here
|
||||
// const foo = results.body.hits.hits.map(
|
||||
// (hit) => {
|
||||
// const parsedFilename = refineQuery(
|
||||
// hit._source.rawFileDetails.name
|
||||
// );
|
||||
// if (
|
||||
// parsedFilename.searchParams
|
||||
// .searchTerms.number ===
|
||||
// parseInt(
|
||||
// ctx.params.queryObject
|
||||
// .issueNumber,
|
||||
// 10
|
||||
// )
|
||||
// ) {
|
||||
// return hit;
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
// return filter(foo, null);
|
||||
// });
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user