-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
109 lines (94 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const axios = require('axios');
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
host: 'localhost:9200',
// log: 'trace'
});
const create = async () => {
await client.indices.create({
index: 'github',
body: {
mappings: {
trending: {
properties: {
name: { type: 'text' },
url: { type: 'text' },
description: { type: 'text', analyzer: 'english' },
readme: { type: 'text', analyzer: 'english' },
}
}
}
}
});
};
const index = async ({ name, description, readme }) => {
await client.index({
index: 'github',
type: 'trending',
body: { name, description, readme }
})
}
const search = async query => {
const results = await client.search({
index: 'github',
size: 10,
body: {
query: {
multi_match: {
query,
type: 'cross_fields',
fields: ['name', 'description^2', 'readme^3'],
operator: 'or',
tie_breaker: 1.0,
cutoff_frequency: 0.1
}
}
}
})
return results.hits.hits.map(({ _source: { name, description, readme } }) => ({
name, description, readme,
}))
}
const fetchTrendingRepositories = async () => {
const { data: { items } } = await axios({
baseURL: 'https://api.github.com/',
url: "/search/repositories",
params: {
sort: 'stars',
order: 'desc',
q: 'language:javascript created:>2018-04-15',
}
})
return items.map(({ id, full_name, html_url, description }) => ({ id, name: full_name, url: html_url, description }));
}
const fetchReadme = async name => {
const { data: readme }= await axios({
baseURL: 'https://api.github.com/',
url: `/repos/${name}/readme`,
headers: {
accept: "application/vnd.github.v3.raw"
}
})
return readme;
}
const store = async () => {
try {
const repos = await fetchTrendingRepositories();
for (const repo of repos.slice(0, 2)) {
const readme = await fetchReadme(repo.name);
await index({ ...repo, readme })
}
} catch (error) {
console.log(error.message);
}
}
const init = async () => {
await create()
await store()
}
const main = async query => {
const results = await search(query);
console.log(results);
}
const args = process.argv.slice(2);
main(args.join(' '))