Skip to content

Commit

Permalink
[ES] Indexer apply pg_unescape_bytea for bytea/blob values on postg…
Browse files Browse the repository at this point in the history
…res (#3578)
  • Loading branch information
mwjames authored and kghbln committed Jan 11, 2019
1 parent a59c76c commit 484a4b5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Elastic/Indexer/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ private function map_text( $bulk, $subject, $text ) {

private function map_data( $bulk, $changeDiff ) {

$dbType = $this->store->getInfo( 'db' );
$unescape_bytea = isset( $dbType['postgres'] );

$inserts = [];
$inverted = [];

Expand Down Expand Up @@ -580,7 +583,7 @@ private function map_data( $bulk, $changeDiff ) {
continue;
}

$this->mapRows( $fieldChangeOp, $propertyList, $inserts, $inverted );
$this->mapRows( $fieldChangeOp, $propertyList, $inserts, $inverted, $unescape_bytea );
}
}

Expand All @@ -593,7 +596,7 @@ private function map_data( $bulk, $changeDiff ) {
}
}

private function mapRows( $fieldChangeOp, $propertyList, &$insertRows, &$invertedRows ) {
private function mapRows( $fieldChangeOp, $propertyList, &$insertRows, &$invertedRows, $unescape_bytea ) {

// The structure to be expected in ES:
//
Expand Down Expand Up @@ -674,6 +677,13 @@ private function mapRows( $fieldChangeOp, $propertyList, &$insertRows, &$inverte
$type = 'txtField';
$val = $ins['o_blob'] === null ? $ins['o_hash'] : $ins['o_blob'];

// Postgres requires special handling of blobs otherwise escaped
// text elements are used as index input
// Tests: P9010, Q0704, Q1206, and Q0103
if ( $unescape_bytea && $ins['o_blob'] !== null ) {
$val = pg_unescape_bytea( $val );
}

// #3020, 3035
if ( isset( $prop['_type'] ) && $prop['_type'] === '_keyw' ) {
$val = DIBlob::normalize( $ins['o_hash'] );
Expand All @@ -686,6 +696,11 @@ private function mapRows( $fieldChangeOp, $propertyList, &$insertRows, &$inverte
} elseif ( $fieldChangeOp->has( 'o_serialized' ) && $fieldChangeOp->has( 'o_blob' ) ) {
$type = 'uriField';
$val = $ins['o_blob'] === null ? $ins['o_serialized'] : $ins['o_blob'];

if ( $unescape_bytea && $ins['o_blob'] !== null ) {
$val = pg_unescape_bytea( $val );
}

} elseif ( $fieldChangeOp->has( 'o_serialized' ) && $fieldChangeOp->has( 'o_sortkey' ) ) {
$type = strpos( $ins['o_serialized'], '/' ) !== false ? 'datField' : 'numField';
$val = (float)$ins['o_sortkey'];
Expand Down

0 comments on commit 484a4b5

Please sign in to comment.