manticoresearch-php
manticoresearch-php copied to clipboard
replaceDocument adds a new document and leaves the old one
I was building a wrapper around some of the provided functions and tried to use updateDocument to only find out it had an issue as stated here https://github.com/manticoresoftware/manticoresearch-php/issues/10
So I switched to replaceDocument as recommended and after running it I found it only added a second document and kept the old one, not a replace.
My work around was to use the deleteDocument and then follow with a addDocument.
here is my echo/print_r output
SELECT id FROM ww_geo_zips WHERE src_id = 1
Array
(
[geo_zip_id] => 1
[zip] => 34051
[city] => FPO
[county] => Erie
[lat] => 0.716239491787173
[lon] => -1.9491645100054953
[city_zip_count] => 17
)
Array
(
[took] => 11
[timed_out] =>
[hits] => Array
(
[total] => 1
[hits] => Array
(
[0] => Array
(
[_id] => 6053701685393360552
[_score] => 1
[_source] => Array
(
)
)
)
)
)
Array
(
[src_id] => 1
[zip] => 34051
[city] => FPO
[county] => Erie
[lat] => 0.716239491787173
[lon] => -1.9491645100054953
[city_zip_count] => 17
)
RECNO: 6053701685393360552
update
Array
(
[_index] => ww_geo_zips
[_id] => 0
[created] =>
[result] => updated
[status] => 200
)
I can't reproduce it with the following script:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$config = ['host'=>'127.0.0.1','port'=>9308];
$client = new \Manticoresearch\Client($config);
$index = new \Manticoresearch\Index($client);
$index->setName('products');
$index->create([
'title'=>['type'=>'text'],
'price'=>['type'=>'float'],
]);
$index->addDocuments([
['id' => 1, 'title' => 'Crossbody Bag with Tassel', 'price' => 19.85],
['id' => 2, 'title' => 'microfiber sheet set', 'price' => 19.99],
['id' => 3, 'title' => 'Pet Hair Remover Glove', 'price' => 7.99]
]);
$index->replaceDocument([
'title' => 'replaced title',
'price' => 10
],1);
$result = $index->search('@title replaced')->highlight(['title'])->get();
foreach($result as $doc)
{
echo "Doc ID: ".$doc->getId()."\n";
echo "Doc Score: ".$doc->getScore()."\n";
echo "Document fields:\n";
print_r($doc->getData());
echo "Highlights: \n";
print_r($doc->getHighlight());
}
It replaces the document successfully:
➜ replace php replace.php
Doc ID: 1
Doc Score: 1680
Document fields:
Array
(
[price] => 10
[title] => replaced title
)
Highlights:
Array
(
[title] => Array
(
[0] => <b>replaced</b> title
)
)
➜ replace mysql -P9306 -h0 -e "select * from products";
+------+-----------+------------------------+
| id | price | title |
+------+-----------+------------------------+
| 1 | 10.000000 | replaced title |
| 2 | 19.990000 | microfiber sheet set |
| 3 | 7.990000 | Pet Hair Remover Glove |
+------+-----------+------------------------+
So we need more details on your case so we can reproduce the issue and fix it.
what details do you need?
A script similar to the one I provided above which reproduces the issue would be best of all, i.e. which would:
- create an index
- populate it with few documents
- replace one unsuccessfully
I just encontered with same issue. Here is code sample:
$id = '1';
$data = [ ... ];
$index->replaceDocument( $data, $id );
Note: no addDocuments is used assuming that replaceDocument will insert it with correct ID if there is no document with specified ID.
This code doesn't replace documents but adds new ones with auto-id.
Issue disappeared when i used type casting of numeric string to integer (int) $id. So following code works as expected:
$id = '1';
$data = [ ... ];
$index->replaceDocument( $data, (int) $id );
➤ Nick Sergeev commented:
Fixed in https://github.com/manticoresoftware/manticoresearch-php/releases/tag/v2.3.1