-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch.js
92 lines (84 loc) · 1.99 KB
/
elasticsearch.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
var elasticsearch = require('elasticsearch');
var elasticClient = new elasticsearch.Client({
host: 'http://elasticsearch:9200',
log: 'info'
});
var indexName = "randomindex";
/**
* Delete an existing index
*/
function deleteIndex() {
return elasticClient.indices.delete({
index: indexName
});
}
exports.deleteIndex = deleteIndex;
/**
* create the index
*/
function initIndex() {
return elasticClient.indices.create({
index: indexName
});
}
exports.initIndex = initIndex;
/**
* check if the index exists
*/
function indexExists() {
return elasticClient.indices.exists({
index: indexName
});
}
exports.indexExists = indexExists;
function initMapping() {
return elasticClient.indices.putMapping({
index: indexName,
type: "document",
body: {
properties: {
title: { type: "string" },
content: { type: "string" },
suggest: {
type: "completion",
analyzer: "simple",
search_analyzer: "simple",
payloads: true
}
}
}
});
}
exports.initMapping = initMapping;
function addDocument(document) {
return elasticClient.index({
index: indexName,
type: "document",
body: {
title: document.title,
content: document.content,
suggest: {
input: document.title.split(" "),
output: document.title,
payload: document.metadata || {}
}
}
});
}
exports.addDocument = addDocument;
function getSuggestions(input) {
return elasticClient.suggest({
index: indexName,
type: "document",
body: {
docsuggest: {
text: input,
completion: {
field: "suggest",
fuzzy: true
}
}
}
})
}
exports.getSuggestions = getSuggestions;