K
K
Khurshed Abdujalil2017-05-12 15:18:43
Sphinx
Khurshed Abdujalil, 2017-05-12 15:18:43

How to solve the problem with duplicate id in sphinx?

There is a parser that works in two threads and records are added to the database then to the sphinx
There is an rt index when a new record is made to the database, to add to the sphinx I do this,
$flow - This is the parsing stream

$maxId = (new Query())->select('id')->from(Yii::$app->params['real_index'])->max('id');
if((($maxId % 2) == 0 && $flow == 1) || (($maxId % 2) != 0 && $flow == 2)){
                $id = $maxId + 1;
            } elseif((($maxId % 2) == 0 && $flow == 2) || (($maxId % 2) != 0 && $flow == 1)) {
                $id = $maxId + 2;
            } else {
                $id = $maxId + 1;
            }

Yii::$app->sphinx->createCommand(
                'INSERT INTO `'.Yii::$app->params['real_index'].'` (`id`, `product_id`, `product_name`, `description`, `company_id`) VALUES (:id, :product_id, :product_name, :description, :company_id)',
                [
                    'id' => $id,
                    'product_id' => (string)$model->_id,
                    'product_name' => $model->product_name,
                    'description' => $model->description ? str_replace('\'', '', $model->description) : '',
                    'company_id' => isset($model->company_id) ? $model->company_id : '',
                ]
            )->execute();

the bottom line is that, for the first thread, id odd ones are substituted, for the second, even ones, I thought at least I’ll solve the problem, although it’s a crutch (so that $ id duplicates don’t turn out at runtime), but again somehow it turns out duplicates and gives an error
Next exception 'yii\ db\Exception' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 duplicate id '780214'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Puma Thailand, 2017-05-12
@opium

if you have two single-threaded parsers, then even or even parsers should solve your problem, just debug the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question