I’ve been running batch .upsert operations on enti...
# community-help
a
I’ve been running batch .upsert operations on entire collections from my rails app, but it adds everything again — is there anyway to only add the updated or new records?
k
Make sure you are using IDs in the records being upserted. The "id" field is used to determine an update vs new insert.
a
Copy code
q = QuestionSet.all.map { |x| [x.id, <http://x.name.tr|x.name.tr>("-"," ").capitalize.to_s.squish, x.allow_for_subscriber, x.multiple_choice_questions.size]  }

qs = q.map { |question_set_id, name, allow_for_subscriber, number_of_questions|  { 'question_set_id' => question_set_id, 'name' => name, 'allow_for_subscriber' => allow_for_subscriber, 'number_of_questions_in_set' => number_of_questions   }  }

@typesense.collections['question_sets'].documents.import(qs, action: 'upsert')
this is the code i use, which gets the ids and maps it as well — unless this is wrong?
Copy code
schema = {
  'name'      => 'question_sets',
  'fields'    => [
    {
      'name'  => 'question_set_id',
      'type'  => 'int32'
    },
    {
      'name'  => 'name',
      'type'  => 'string',
    },
    {
      'name'  => 'allow_for_subscriber',
      'type'  => 'bool',
      'facet' => true
    },
    {
      'name'  => 'number_of_questions_in_set',
      'type'  => 'int32',
      'facet' => true
    },
  ]
}
schema for typesense
k
question_set_id
is a regular field. You need to have a
id
field. So just have
id
value to be same as
question_set_id
and it should work. Also
id
should be a string so just convert the
question_set_id
integer to string before using it as
id
.