@ournet/news-data
Version:
Ournet news data module
118 lines (117 loc) • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const elasticsearch_1 = require("elasticsearch");
const domain_1 = require("@ournet/domain");
const mappings = require("../../elasticsearch_mappings.json");
const ES_NEWS_INDEX = "news";
const ES_NEWS_TYPE = "news_item";
class NewsSearcher {
constructor(host) {
const options = typeof host === "string"
? { host, ssl: { rejectUnauthorized: false, pfx: [] } }
: host;
this.client = new elasticsearch_1.Client(options);
}
async search(params) {
const q = domain_1.atonic(params.q);
const body = {
query: {
filtered: {
filter: {
bool: {
must: [
{
term: {
country: params.country
}
},
{
term: {
lang: params.lang
}
}
]
}
},
query: {
multi_match: {
query: q,
fields: ["title_" + params.lang, "summary_" + params.lang]
}
}
}
}
};
if (params.minScore) {
body.min_score = params.minScore;
}
const response = await this.client.search({
index: ES_NEWS_INDEX,
type: ES_NEWS_TYPE,
body: body
});
return parseResponse(response);
}
async index(data, refresh) {
const item = normalizeItem(data);
await this.client.index({
index: ES_NEWS_INDEX,
type: ES_NEWS_TYPE,
id: item.id,
body: item,
ttl: "24h",
refresh
});
}
async update(data, refresh) {
const item = normalizeItem(data);
await this.client.update({
index: ES_NEWS_INDEX,
type: ES_NEWS_TYPE,
id: item.id,
body: item,
refresh
});
}
async refresh() {
await this.refresh();
}
async init() {
const exists = await this.client.indices.exists({
index: ES_NEWS_INDEX
});
if (exists) {
return;
}
await this.client.indices.create({
index: ES_NEWS_INDEX
});
await this.client.indices.putMapping({
index: ES_NEWS_INDEX,
type: ES_NEWS_TYPE,
body: mappings
});
}
}
exports.NewsSearcher = NewsSearcher;
function parseResponse(response) {
if (!response.hits || !response.hits.total) {
return [];
}
return response.hits.hits.map((item) => ({
id: item._source.id,
score: item._score
}));
}
function normalizeItem(data) {
const id = data.id;
const item = {
id,
country: data.country,
lang: data.lang,
publishedAt: data.publishedAt
};
item["title_" + item.lang] = domain_1.atonic(data.title);
item["summary_" + item.lang] = domain_1.atonic(data.summary.substr(0, 200));
return item;
}