Created
January 16, 2018 19:39
-
-
Save anonymous/3746504f1bfbeec8d73be0db0550f754 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fetch = require('node-fetch'); | |
function showStatus(){ | |
fetch('http://localhost:9200/') | |
.then(r => r.json()) | |
.then(r => console.log(r)); | |
} | |
function createIndex() { | |
fetch("http://localhost:9200/zzz", { | |
method: 'PUT' | |
}) | |
.then(r => r.json()) | |
.then(r => console.log(r)); | |
} | |
function getIndexSettings(indexName){ | |
fetch("http://localhost:9200/zzz/_settings", { | |
method: 'GET' | |
}) | |
.then(r => r.json()) | |
.then(r => console.log(JSON.stringify(r, null, 4))); | |
} | |
function getDocument(id=5){ | |
fetch("http://localhost:9200/tmdb/movie/5", { | |
method: 'GET' | |
}) | |
.then(r => r.json()) | |
.then(r => console.log(JSON.stringify(r, null, 4))); | |
} | |
function indexDocument() { | |
const bd = JSON.stringify({ | |
title: 'My super blog post', | |
body: 'hele hele gurban hele hele dadashc' | |
}); | |
fetch("http://localhost:9200/zzz/blog", { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: bd | |
}) | |
.then(r => r.json()) | |
.then(r => console.log(JSON.stringify(r, null, 4))); | |
} | |
function indexMoviesBulk(){ | |
const fs = require('fs'); | |
console.log('reading movies database ...'); | |
const objMovies = JSON.parse(fs.readFileSync('./tmdb.json', 'utf8')); | |
let bulkMovies = ""; | |
let index = 0; | |
Object.keys(objMovies).forEach(movieId => { | |
const movie = objMovies[movieId]; | |
const indexCmd = { "index" : { "_index" : "tmdb", "_type" : "movie", "_id": movie["id"] } }; | |
bulkMovies += JSON.stringify(indexCmd) + '\n' + JSON.stringify(movie) + '\n'; | |
}); | |
console.log('indexing...'); | |
fetch("http://localhost:9200/tmdb/_bulk", { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: bulkMovies | |
}) | |
.then(r => r.json()) | |
.then(r => console.log('completed...')); | |
} | |
function search(){ | |
const query = { | |
'query': { | |
'multi_match': { | |
'query': 'machines killing humans', | |
'fields': ['title^0.10', 'overview'] | |
}, | |
}, | |
'size': '20' | |
} | |
fetch("http://localhost:9200/tmdb/movie/_search", { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(query) | |
}) | |
.then(r => r.json()) | |
.then(r => | |
r.hits.hits.forEach(h=> | |
console.log(h['_source']['title']) | |
) | |
); | |
} | |
search(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment