-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
69 lines (62 loc) · 1.98 KB
/
search.js
File metadata and controls
69 lines (62 loc) · 1.98 KB
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
const fs = require( "fs" );
const client = require( "./esConfig" );
const data = JSON.parse( fs.readFileSync( "./city_list.json" ) );
const index = "weather";
const type = "cities";
async function writeToEs( index, data ){
const bulkArray = [];
for( let i = 0;i <= data.length;i++ ){
bulkArray.push( {
index:{
_index:index, _type:type, _id:i
}
} );
if( typeof data[i] !== "undefined" ){
bulkArray.push( {id:data[i]["city"]["id"]["$numberLong"],
name:data[i]["city"]["name"],
findName:data[i]["city"]["findName"],
country:data[i]["city"]["country"]} );
}
}
client.bulk( {body:bulkArray} )
.then( response=>response )
.catch( e=>{
console.error( e ); //eslint-disable-line no-console
} );
}
async function createMapping( index ){
const cityScehema = {
"name":"String",
"findName":"String",
"country":"String",
"id":"long"
}
return client.indices.putMapping( {index:index, body:{properties:cityScehema}} );
}
module.exports = {
async search( param, callback ){
const resultArray = [];
const {body} = await client.search( {
index:index,
body:{
query:{
wildcard:{
name:`*${param.toLowerCase()}*`
}
}
}
} ).catch( err=>console.error( err ) ); //eslint-disable-line no-console
for( let j = 0;j < body.hits.hits.length;j++ ){
resultArray.push( body.hits.hits[j]["_source"] );
}
callback( resultArray );
},
async cityListIndexing(){
if( client.indices.exists( {index:index} ) ){
client.indices.delete( {index: index} );
}
await client.indices.create( {index} );
createMapping( client, index );
writeToEs( index, data );
}
}