//Jan Holp, DP 2021


//client1 = cassandra
//client2 = elasticsearch 
//-----------------------------------------------------------------

//require the Elasticsearch librray
const elasticsearch = require('elasticsearch');
const client2 = new elasticsearch.Client({
   hosts: [ 'localhost:9200']
});
client2.ping({
     requestTimeout: 30000,
 }, function(error) {
 // at this point, eastic search is down, please check your Elasticsearch service
     if (error) {
         console.error('Elasticsearch cluster is down!');
     } else {
         console.log('Everything is ok');
     }
 });

//create new index skweb2
client2.indices.create({
    index: 'skweb2'
}, function(error, response, status) {
    if (error) {
        console.log(error);
    } else {
        console.log("created a new index", response);
    }
});

const cassandra = require('cassandra-driver');
const client1 = new cassandra.Client({ contactPoints: ['localhost:9042'], localDataCenter: 'datacenter1', keyspace: 'websucker' });
const query = 'SELECT title  FROM websucker.content WHERE body_size > 0  ALLOW FILTERING';
client1.execute(query)
  .then(result => console.log(result)),function(error) {
    if(error){
      console.error('Something is wrong!');
      console.log(error);
    } else{
        console.log('Everything is ok');
    }
  }; 

/*
async  function indexData() {

  var i = 0;
  const query = 'SELECT title  FROM websucker.content WHERE body_size > 0  ALLOW FILTERING'; 
  client1.execute(query)
    .then((result) => {
    try {
        //for ( i=0; i<15;i++){
        console.log('%s', result.row[0].title)
      //}
  } catch (query) {
      if (query  instanceof SyntaxError) {
          console.log( "Neplatne query" );
        } 
  }

    

    });
      

  }

/*

//indexing method
const bulkIndex = function bulkIndex(index, type, data) {
	let bulkBody = [];
	id = 1;
const errorCount = 0;
	data.forEach(item => {
		bulkBody.push({
			index: {
				_index: index,
				_type:  type,
				_id :   id++,
			}
		});
		bulkBody.push(item);
	});
        console.log(bulkBody);
	client.bulk({body: bulkBody})
		.then(response => {

			response.items.forEach(item => {
				if (item.index && item.index.error) {
					console.log(++errorCount, item.index.error);
				}
			});
			console.log(
				`Successfully indexed ${data.length - errorCount}
				out of ${data.length} items`
			);
		})
		.catch(console.err);
};
*/