Skip to content

Commit

Permalink
Merge pull request #114 from tripal/fix-low-priority-error
Browse files Browse the repository at this point in the history
Remove objects from json and convert them to arrays
  • Loading branch information
almasaeed2010 committed Dec 8, 2017
2 parents 8a50e04 + 1e29c7f commit 1939a34
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
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

0 comments on commit 1939a34

Please sign in to comment.