Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove objects from json and convert them to arrays #114

Merged
merged 1 commit into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/Elasticsearch/ESInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Also Provides methods for building indices, searching,
* deleting and indexing.
*/
class ESInstance{
class ESInstance {

/**
* Elasticsearch client.
Expand Down
47 changes: 33 additions & 14 deletions includes/Jobs/EntitiesIndexJob.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class EntitiesIndexJob extends ESJob{
class EntitiesIndexJob extends ESJob {

/**
* Job type to show in progress report.
Expand Down Expand Up @@ -59,6 +59,13 @@ class EntitiesIndexJob extends ESJob{
*/
protected $es;

/**
* Should bulk update.
*
* @var bool
*/
protected $shouldUpdate = FALSE;

/**
* Constructor.
*
Expand All @@ -84,11 +91,16 @@ public function handle() {
$records = $this->loadContent($records);

if ($this->total > 1) {
if ($this->priority_round === 1) {
if (!$this->shouldUpdate) {
$this->es->bulkIndex($this->index, $records, $this->index, 'entity_id');
}
else {
$this->es->bulkUpdate($this->index, $records, $this->index, 'entity_id');

// foreach ($records as $record) {
// $this->es->deleteEntry($this->index, $this->index, $record->entity_id);
// $this->es->createEntry($this->index, $this->index, $record->entity_id, $record);
// }
}
}
elseif (count($records) > 0) {
Expand Down Expand Up @@ -151,7 +163,8 @@ protected function loadContent($records) {

$prev_entity = $this->es->getRecord('entities', 'entities', $entity->id);
if ($prev_entity['found']) {
$content = array_merge($content, $prev_entity['_source']['content']);
$this->shouldUpdate = TRUE;
$content = array_merge($prev_entity['_source']['content'], $content);
}

$all[] = (object) [
Expand Down Expand Up @@ -213,9 +226,7 @@ protected function prioritizeFields($fields) {
])->fetchAll();
}
else {
$results = db_query('SELECT * FROM {tripal_elasticsearch_priority} WHERE priority != :priority', [
':priority' => 1,
])->fetchAll();
$results = db_query('SELECT * FROM {tripal_elasticsearch_priority}')->fetchAll();
}

$indexed = [];
Expand All @@ -231,13 +242,11 @@ protected function prioritizeFields($fields) {

foreach ($fields as $field => $data) {
$id = $data['field_id'];
if (isset($indexed[$id])) {
if ($indexed[$id] > 0) {
$return['names'][] = $field;
$return['ids'][] = $id;
}
if (isset($indexed[$id]) && $indexed[$id] == $this->priority_round) {
$return['names'][] = $field;
$return['ids'][] = $id;
}
elseif ($this->priority_round === 2) {
elseif (!isset($indexed[$id]) && $this->priority_round > 1) {
// Assume the field is new and a priority setting has not yet been
// saved for it so automatically consider it low priority and add
// it to the list.
Expand All @@ -260,8 +269,18 @@ protected function extractValue($element) {
$items = [];
$this->flatten($element, $items);

// Remove repeated elements
return array_unique($items);
// Make sure arrays don't get turned into objects when encoding with JSON
$total = [];
foreach ($items as $item) {
if (is_array($item)) {
$total[] = array_values($item);
}
else {
$total[] = $item;
}
}

return $total;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions includes/indices_management.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,6 @@ function tripal_elasticsearch_progress_page($callback = FALSE) {

$data = ESQueue::progress();

if(!$callback) {
dpm($data);
}

$content .= '<h3>Overall Progress</h3>';

if ($data->remaining === 0) {
Expand Down