-
Notifications
You must be signed in to change notification settings - Fork 1
/
elasticfactstore.js
148 lines (138 loc) · 3.31 KB
/
elasticfactstore.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var elasticsearch = require('elasticsearch')
var config = require('./config.js')
var _ = require('lodash')
var resultsPerPage = 100
var ESFactStore = function () {
}
var getESClient = function () {
var client = new elasticsearch.Client({
host: config.elasticServer,
apiVersion: '2.4'
})
return client
}
var convertFactsToOutputForm = function (facts) {
return _.map(facts, convertESLine2OutputForm)
}
var convertESLine2OutputForm = function (line) {
if (line) {
var lineOut = line._source
return lineOut
}
return null
}
ESFactStore.prototype.getByDate = function (date, page) {
var ESFrom = resultsPerPage * page
return new Promise(function (resolve, reject) {
getESClient().search({
index: config.elasticFactIndex,
method: 'GET',
body: {
from: ESFrom,
size: resultsPerPage,
query: {
bool: {
must: {
range: {
ingestionDate: {
gte: date + '||/d',
lt: date + '||+1d/d'
}
}
}
}
}
}
})
.then(function (results) {
var facts = results.hits.hits
resolve(convertFactsToOutputForm(facts))
})
.catch(console.log)
})
}
ESFactStore.prototype.getByItem = function (item, page) {
var ESFrom = resultsPerPage * page
return new Promise(function (resolve, reject) {
getESClient().search({
index: config.elasticFactIndex,
method: 'GET',
body: {
from: ESFrom,
size: resultsPerPage,
query: {
constant_score: {
filter: {
term: {
'identifiers.wikidata': item
}
}
}
}
}
})
.then(function (results) {
var facts = results.hits.hits
resolve(convertFactsToOutputForm(facts))
})
.catch(console.log)
})
}
ESFactStore.prototype.getBy2Item = function (item1, item2) {
return new Promise(function (resolve, reject) {
getESClient().search({
index: config.elasticFactIndex,
method: 'GET',
body: {
'query' : {
'or': [
{
'bool' : {
'must': {
'term': { 'identifiers.wikidata' : item1}
}
}
},
{
'bool' : {
'must': {
'term': { 'identifiers.wikidata' : item2}
}
}
}
]
},
'aggs': {
'papers': {
'terms': {
'field': 'cprojectID',
'size': 20,
'order': { 'fact_count': 'desc'}
},
'aggs': {
'fact_count': {
'cardinality': {'field': 'identifiers.wikidata'}
},
'documents': {
'top_hits': {'size': 5}
}
}
}
},
'size': 0
}
})
.then(function (results) {
var buckets = results.aggregations.papers.buckets
var facts = _.flatMap(buckets, function (bucket) {
if (bucket.fact_count.value === 2) {
return bucket.documents.hits.hits
}
return []
})
resolve(convertFactsToOutputForm(facts))
})
.catch(console.log)
})
}
module.exports = ESFactStore