I am trying to bulk upload in elastic search and i am getting this error for every record i am trying to insert.
Please help me with this.
{"took":2828,"errors":true,"items":[{"index":{"_index":"abc","_type":"abc","_id":"0","status":400,"error":{"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}}}}]}
This is the code i am using
I am using 5.3 and elasticsearch driver 1.4
<?php require_once "/var/www/ElasticSearch/models/ElasticSearchModels.php"; $params = array(); $ids = array(); for($i=0;$i<10;$i++){ $ids[] = $i; $params[] = array("here"=>"here","temp"=>"temp","pr"=>$i); } $elasticSearch = new ElasticSearch(); $elasticSearch->saveInElasticSearchBulk("products_staging","products_staging",$ids,$params); ?>
You're not constructing the $params array correctly, it should look like this instead:
$params = array();
for($i = 0; $i < 10; $i++){
$params['body'][] = array(
'index' => array(
'_index' => 'products_staging',
'_type' => 'products_staging',
'_id' => $i
)
);
$params['body'][] = array(
'here' => 'here',
'temp' => 'temp',
'pr' => $i
);
}
$elasticSearch = new ElasticSearch();
$elasticSearch->saveInElasticSearchBulk($params);
I solved this issue since i was using ElasticSearch php driver 1.4. The syntax for the bulk api is quite different.
$params = array();
for($i = 0; $i < 10; $i++){
$params['body'][] = array(
'index' => array(
'_index' => 'products_staging',
'_type' => 'products_staging',
'_id' => $i
)
);
$params['body'][] = json_encode(array(
'here' => 'here',
'temp' => 'temp',
'pr' => $i
);)
}
$elasticSearch = new ElasticSearch();
$elasticSearch->saveInElasticSearchBulk($params);
Related
I am currently using Highcharts in symfony. It works good when I enter static data (array1/array2/array3/array4) like this:
$ob1 = new Highchart();
$ob1->chart->renderTo('barchart');
$ob1->title->text('Chart 1');
$ob1->xAxis->categories($arrayResult);
$ob1->plotOptions->pie(array(
'allowPointSelect' => true,
'cursor' => 'pointer',
'dataLabels' => array('enabled' => false),
'showInLegend' => true
));
$ob1->series(array(array('type' => 'column','name' => 'bar1', 'data' => $array1),
(array('type' => 'column','name' => 'bar2', 'data' => $array2)),
(array('type' => 'column','name' => 'bar3', 'data' => $array3)),
(array('type' => 'column','name' => 'bar4', 'data' => $array4))
));
but what I need is to enter data within a loop because I have an irregular number of arrays. I tried this but I got an error "unexpected 'while' (T_WHILE)" Is there anything I have missed here?
Here is my code using while to add Chart's Data Series:
$i=1;
$number=4;
$ob1->series(array(
(
$myarray = array();
while($i <= $number): array_push($myarray, array(0 => 'value', 1=> $i));
$i++;
array('type' => 'column','name' => 'bar'.$i, 'data' => $myarray)
endwhile;
),
));
I also tried this and displays only the last iteration of the while
$i=1;
$number=4;
$myarray = array();
while($i <= $number):
array_push($myarray, array(0 => 'value', 1=> $i));
$i++;
$ob1->series(array (array('type' => 'column','name' => 'bar'.$i, 'data' => $myarray)));
endwhile;
Your php statement is not valid, has invalid syntax. To avoid this type of error, never create a loop inside the function argument. Simplify your syntax and use temporal variables, is easy to read and to understand what are you doing, remember, every good developer always say: "divide and conquer" :)
$i = 1;
$number = 4;
$chartData = [];
while ($i <= $number) {
$chartData[] = [
'type' => 'column',
'name' => 'bar'.$i,
'data' => [
'value',
$i,
],
];
$i++;
}
$ob1->series($chartData);
I am currently working on adding multiple requests in single click using CodeIgniters insert_batch.
This is my model
function add_request($data) {
$this->db->insert_batch('requests',$data);
}
This is my controller
if($_POST) {
$code = $this->input->post('code');
$about = $this->input->post('about');
$qnty = $this->input->post('quantity');
$budget = $this->input->post('budget');
$sched = $this->input->post('sched');
for($i = 0; $i < count($code); $i++) {
$data[$i] = array(
'code' => $code,
'description' => $_POST['desc'],
'qnty' => $qnty,
'budget' => $budget,
'sched' => $sched,
'from' => $this->session->userdata('user_id'),
'status' => 'Pending',
'about' => $about
);
$this->request->add_request($data[$i]);
}
This code doesnt work It only adds blank record.
Consider this code for the controller:
$data = array();
for($i = 0; $i < count($code); $i++) {//build the array
$data[$i] = array(
'code' => $code,
'description' => $_POST['desc'],
'qnty' => $qnty,
'budget' => $budget,
'sched' => $sched,
'from' => $this->session->userdata('user_id'),
'status' => 'Pending',
'about' => $about
);
}
//$data will be a bidimentional array
//pass $data to the model after the looping is done, thus the array is complete
$this->request->add_request($data);
I have this complicated array.
<?php
require_once('core/connect.php');
require_once('core/database.class.php');
require_once('core/controller.class.php');
require_once('core/settings.class.php');
$database = new Database($db);
$controller = new Controller($db);
$settings = new Settings($db);
$database->selectAll('SELECT * FROM bookings_calendar');
$result = $database->fetchAll();
$count = 0;
$arr = array();
foreach($result as $row)
{
$arr['booking_date'][$count] = $row['booking_date'];
$arr['new'][$count] = $row['new'];
$arr['completed'][$count] = $row['completed'];
$arr['accepted'][$count] = $row['accepted'];
$arr['cancelled'][$count] = $row['cancelled'];
$count++;
}
header("content-type: application/json");
$year = date('Y');
$month = date('m');
echo json_encode(array(
array(
'id' => 111,
'title' => $arr['new'][0] . ' new',
'start' => $arr['booking_date'][0],
'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][0]),
'color' => '#F7F8E0',
'textColor' => 'black'
),
array(
'id' => 111,
'title' => $arr['new'][1] . ' new',
'start' => $arr['booking_date'][1],
'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][1]),
'color' => '#F7F8E0',
'textColor' => 'black'
),
array(
'id' => 111,
'title' => $arr['new'][2] . ' new',
'start' => $arr['booking_date'][2],
'url' => "bookings/ordered-by-date/" . str_replace('-','', $arr['booking_date'][2]),
'color' => '#F7F8E0',
'textColor' => 'black'
),
));
?>
As you can see i can only put values manually by changing index, however i'd like to put all elements into that array automatically, but unfortunately i cannot use a foreach loop within an array. And my php skills are not that good, so im searching for some help.
Any help really appreciated. Thanks!
Get rid of the foreach and use a for loop.
$arr = array();
for($i=0; $i < count($result); $i++) {
$arr[$i]['title'] = $result[$i]['new'] . ' new';
$arr[$i]['whatever'] = $result[$i]['whatever'];
}
return json_encode($arr);
I have an array like this:
array(
0 => array(
'name' => 'colors',
'options' => array(
array('name'=>'red', 'price'=>'2'),
array('name'=>'blue', 'price'=>'3')
)
),
1 => array(
'name' => 'sizes',
'options' => array(
array('name'=>'small', 'price'=>'5'),
array('name'=>'large', 'price'=>'10')
)
),
);
I want to merge all of the nested options arrays' name/prices into one, so it'll be like this:
array(
'red' => '2',
'blue' => '3',
'small' => '5',
'large' => '10'
);
I have a feeling this is very simple, but anything I try seems too convoluted. Any help would be appreciated.
...and yes, I just posted almost the same question not too long ago. This is a bit different, and what I meant to ask in the first place - oops, sorry.
I'm not sure if this can be done with internal methods such as array_merge(), array_combine(), etc since your structure seems quite special. But how about iterating over the array and building a new one yourself?
function customRearrange($arr) {
$result = array();
foreach ($arr as $group) {
foreach ($group['options'] as $option) {
// prevents values from being overwritten, uncomment if unwanted
if (!array_key_exists($option['name'], $result))
$result[ $option['name'] ] = $option['price'];
}
}
return $result;
}
You need to decide how you will handle collisions, anyway here is same which will give you right direction:
$newArray = array();
foreach ($array as $i => $item)
foreach ($item['options'] as $j => $option)
$newArray[$option['name']] = $option['price'];
Here's another way, assuming the array has been assigned to the variable $a:
<?php
for ($i = 0; $i < count($a); $i++) {
$key = $a[$i]['name'];
$options = $a[$i]['options'];
for ($j = 0; $j < count($options); $j++) {
$optkey = $options[$j]['name'];
$prices[$optkey] = $options[$j]['price'];
}
}
print_r($prices);
Iterate through "options" in the nested array and create a new array :
<?php
$arr = array(
0 => array(
'name' => 'colors',
'options' => array(
array('name'=>'red', 'price'=>'2'),
array('name'=>'blue', 'price'=>'3')
)
),
1 => array(
'name' => 'sizes',
'options' => array(
array('name'=>'small', 'price'=>'5'),
array('name'=>'large', 'price'=>'10')
)
),
);
$group = array();
foreach($arr as $a){
foreach($a['options'] as $op){
$group[$op['name']] = $op['price'];
}
}
print_r($group);
?>
how can i fill following type array dynamicly in for loop
array('items'=>array(
array('label'=>'News', 'url'=>array('/site/index')),
array('label'=>'News2', 'url'=>array('/site/2')),
));
I am new in programming
thanks for help
Try this:
$arr = array();
for($i = 1; $i <= $count; $i++) {
$arr[] = array(
'label' => 'News'.($i > 1 ? $i : ''),
'url' => $i == 1 ? '/site/index' : '/site/'.$i
)
}
$result = array('items' => $arr);
And the resulting array will be in the form:
array('items' => array(
array(
'label' => 'News',
'url' => '/site/index'
),
array(
'label' => 'News2',
'url' => '/site/2'
),
array(
'label' => 'News3',
'url' => '/site/3'
),
array(
'label' => 'News4',
'url' => '/site/4'
)
));
..depending on the $count variable.
for($i = 0; $i < $items; $i++) { //where $items is number of news items
if($i == 0)
$value = "Index";
else
$value = $i+1;
$ar["items"]["News".$i] = $value;
}
You can access the array by square brackets, by both alphanumerical and purely numerical keys. Anyway I suggest reading a basic php course.
use for to loop like :
$items=array();
for($i=1;$i<=$max_count;$i++){
$element = array('label'=>'news'.$i,'url'=>'/site/index'.$i);
$items[] = $element;
}