Commit b14b4210 authored by Selina Nicole Brunner's avatar Selina Nicole Brunner

update index

parents f6e02246 3aa4500c
......@@ -10,3 +10,4 @@ node_modules/
# build
dist/
bundle.js
\ No newline at end of file
This diff is collapsed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Poke Dex Go</title>
</head>
<body>
<section class="search">
<form class="searchForm" action="">
<input id="input" name="q" type="text" />
<button id="searchButton" type="submit" class="button">Send</button>
</form>
</section>
<section id="results">
</section>
<script src="bundle.js"></script>
</body>
</html>
......@@ -5,65 +5,27 @@ const request = require('sync-request')
let documents = []
// read file through fs readstream
const instream = fs.createReadStream('pokemon.json', 'utf8') // simplewiki file has to be named 'simplewiki.json'
const outstream = new stream
const rl = readline.createInterface(instream, outstream)
//get contents of json and write relevant information in solr-formatted json
const pokejson = JSON.parse(fs.readFileSync('pokemon.json', 'utf8'));
// do this for every line in the stream
rl.on('line', function(line) {
// only every second line is interesting -> lines starting with index are not interesting
pokejson.forEach(function(element){
if(element.Number === undefined){
// extract only the relevant properties of the input JSON file
if (line.length < 4) {
return
}
else {
const lineJson = JSON.parse(line);
}
else{
const outJson = {}
// write relevant properties to output JSON file
outJson["poke_id"] = lineJson.number
outJson["poke_txt_en"] = lineJson.name
outJson["_text_"] = lineJson.name
outJson["gen_txt_en"] = lineJson.generation
outJson["about_txt_en"] = lineJson.about
outJson["types_features"] = lineJson.types
/*
// opening_text is optional
if(lineJson.opening_text) outJson["opening_txt_en"] = lineJson.opening_text
else outJson["opening_txt_en"] = ""
// coordinates is optional
if(lineJson.coordinates) outJson["location"] = lineJson.coordinates
else outJson["location"] = []
*/
console.log(outJson);
//accumulate JSON objects before they are sent
accumData(outJson)
outJson["id"] = element.Number
outJson["poke_txt_en"] = element.Name
outJson["gen_txt_en"] = element.Generation
outJson["about_txt_en"] = element.About
outJson["types_features"] = element.Types
outJson["maxCP_txt_en"] = element.MaxCP
outJson["maxHP_txt_en"] = element.MaxHP
documents.push(outJson)
}
})
// send rest of Data when filestream is over
// otherwise the last <10k Objects are not sent
rl.on('close', function() {
sendData(documents)
documents = []
});
// accumulates JSON objects in array until 10k before they are sent
function accumData(postData) {
documents.push(postData)
if(documents.length == 10000){
// send array of JSON objects to solr server
console.log('sending')
sendData(documents)
documents = []
}
}
sendData(documents)
// sends data to solr server
function sendData(postData){
......@@ -80,6 +42,7 @@ function sendData(postData){
if (response.statusCode !== 200) {
throw(response.body)
} else {
console.log(response)
console.log('sent')
}
}
This diff is collapsed.
......@@ -5,12 +5,15 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
"start": "node index.js",
"search": "node search.js",
"build": "browserify search.js > bundle.js"
},
"author": "Christoph",
"license": "MIT",
"dependencies": {
"fs": "0.0.1-security",
"jquery": "^3.3.1",
"pokeapi": "0.0.1",
"pokeapi-js-wrapper": "^1.1.1",
"readline": "^1.3.0",
......@@ -18,5 +21,8 @@
"stream": "0.0.2",
"sync-request": "^6.0.0",
"util": "^0.11.0"
},
"devDependencies": {
"browserify": "^16.2.2"
}
}
This diff is collapsed.
const solr = require('solr-client');
// const $ = require('jquery');
const client = solr.createClient('127.0.0.1', '8983', 'gettingstarted', '/solr');
// const startQuery = `q=id:3`;
// // DixMax query
// var query = client.createQuery()
// .q('laptop')
// .dismax()
// .qf({title_t : 0.2 , description_t : 3.3})
// .mm(2)
// .start(0)
// .rows(10);
// client.search(query,function(err,obj){
// if(err){
// console.log(err);
// }else{
// console.log(obj);
// }
// });
// Lucene query
// var query2 = client.createQuery()
// .q({poke_txt_en : 'bulbasaur'})
// .start(0)
// .rows(10);
let searchButton = document.getElementById('searchButton')
searchButton.addEventListener("click", search, false)
function search(e) {
e.preventDefault();
const searchValue = document.getElementById('input').value
console.log(searchValue);
sendNameQuery(searchValue);
}
// $('searchForm').submit(function(){
//
// console.log(searchValue);
// console.log($('#input').val());
// sendQuery($('#input').val())
// console.log($('#input').val());
// // return false
// })
function sendNameQuery(q){
var query = "q=poke_txt_en:(" + q +")&rows=100" //rows ist nötig, sonst nur 10 results
console.log(query);
client.search(query,function(err,obj){
if(err){
console.log(err);
}else{
results = obj.response.docs
if (results.length == 0) {
document.getElementById("results").innerHTML = "No results"
}
for(var i = 0; i < results.length; i++){
let item = document.createElement("p")
item.innerHTML =
"<li>Id: <strong>" + results[i].id + "</strong></li>" +
"<li>Name: <strong>" + results[i].poke_txt_en + "</strong></li>" +
"<li>Gen: " + results[i].gen_txt_en + "</li>" +
"<li>HP: " + results[i].maxHP_txt_en + "</li>" +
"<li>Types:</li>" +
"<ul id='test'></ul>"
document.getElementById('results').appendChild(item)
let types = results[i].types_features
console.log(types)
// let item2 = document.createElement("ul")
// item2.setAttribute("id", "test");
// document.getElementById('results').appendChild(item2)
for (var i = 0; i < types.length; i++) {
let item3 = document.createElement("li")
// console.log(types[i])
item3.innerHTML = types[i];
document.getElementById('test').appendChild(item3)
// item2.appendChild(item3);
}
}
console.log(obj);
}
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment