Note that there are some explanatory texts on larger screens.

plurals
  1. POPostgresql trigram text search with pg_search results order
    primarykey
    data
    text
    <p>I implemented a trigram search using pg_search gem on rails. <a href="https://github.com/Casecommons/pg_search" rel="nofollow">https://github.com/Casecommons/pg_search</a></p> <p>The problem is that sometimes the order of returned results doesn't seems right according to the definition of the trigram search that shows the gem documentation:</p> <blockquote> <p>Trigram search works by counting how many three-letter substrings (or “trigrams”) match between the query and the text.</p> </blockquote> <p>My application receives string input from the user ("111 Streetname") and returns a list of addresses that matches the Address.full_string value with approximate search with trigram. </p> <h1>List of search examples</h1> <p><strong>Trigram search: "1493 cambrid"</strong></p> <ul> <li>Results: <ul> <li>100 Cambridgeside Pl</li> <li>100 Cambridgeside Pl</li> <li>150 Cambridgepark Dr</li> <li>1575 Cambridge St</li> <li>1573 Cambridge St</li> <li>1493 Cambridge St</li> </ul></li> </ul> <p><strong>Trigram search: "1493 cambr"</strong></p> <ul> <li>Result: <ul> <li>1493 Cambridge St</li> </ul></li> </ul> <p><strong>Trigram search: "1493 cambri"</strong></p> <ul> <li>Results: <ul> <li>1575 Cambridge St</li> <li>1573 Cambridge St</li> <li>1493 Cambridge St</li> </ul></li> </ul> <p><strong>Trigram search: "1493 cambridge"</strong></p> <ul> <li>Results: <ul> <li>1493 Cambridge St</li> <li>5 Cambridgepark Dr</li> <li>7 Cambridgepark Dr</li> <li>100 Cambridgeside Pl</li> <li>and many more</li> </ul></li> </ul> <h1>Question</h1> <p>¿Why isn't "1493 Cambridge St" always on top of the results? ¿Do I need to change the query of the trigram search or is it just the way the algorithm works?</p> <h1>Query example</h1> <pre><code>SELECT "addresses".*, (ts_rank((to_tsvector('simple', coalesce("addresses"."full_string"::text, ''))), (to_tsquery('simple', ''' ' || '1493' || ' ''') &amp;&amp; to_tsquery('simple', ''' ' || 'cambridge' || ' ''')), 0)) AS pg_search_rank FROM "addresses" WHERE (((coalesce("addresses"."full_string"::text, '')) % '1493 cambridge')) ORDER BY pg_search_rank DESC, "addresses"."id" ASC </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload