title: Django Trigram Similarity for list of strings in Postgres
body: <p>I have a list of names <code>[&quot;May&quot;, &quot;Brown&quot;, &quot;Chaplin&quot;, etc...]</code> containing about 200 names. For each name string I want return a corresponding object from the database using Django <code>TrigramSimilarity</code>.</p>
  <p>Normally I would do something like this:</p>
  <pre><code>result = []
  for surname in surnames:
      person = Person.objects.annotate(
          similarity=TrigramSimilarity(&quot;surname&quot;, surname)
      )
      .filter(similarity__gt=0.3)
      .order_by(&quot;-similarity&quot;).first()
  
      if person:
          result.append(person)
  </code></pre>
  <p>but executing 200 queries in a row doesn't seem very optimal. Is there a way to create a single query for this operation? Also what are the ways to optimize this task even more?</p>
  <p>Here is my Django model:</p>
  <pre><code>class Person(models.Model):
      name = models.Charfield(max_length=100)
      surname = models.Charfield(max_length=100)
      age = models.IntegerField()
  
      etc...
  </code></pre>