Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain the use of <-> in PostgreSQL

Tags:

postgresql

I was reading up on materialized views in the PostgreSQL 9.3 documentation and came across the following example, which is given in reference to spell checking a word:

SELECT word FROM words ORDER BY word <-> 'caterpiler' LIMIT 10;

I've tried searching Google and StackOverflow, but punctuation gets filtered out and I cannot see to come up what it is referring to. Can someone explain how and what it is used for?

And maybe a link to the documentation where I can read up more on the details of its usage?

like image 850
RyanKDalton Avatar asked Aug 20 '13 16:08

RyanKDalton


2 Answers

According to http://www.postgresql.org/docs/9.1/static/pgtrgm.html this operator <-> returns edit distance between strings

text <-> text real Returns the "distance" between the arguments, that is one minus the similarity() value.

So the whole query looks for the 10 most similar words to the word "caterpiler" in terms of edit distance

like image 146
lejlot Avatar answered Sep 28 '22 07:09

lejlot


That's the operator for finding the distance between two geometric figures (see the documentation). Also, it's the "distance" (Levenshtein's? the documentation does not state this explicitly) between strings according to this:

text <-> text Returns the "distance" between the arguments, that is one minus the similarity() value.

like image 42
Óscar López Avatar answered Sep 28 '22 09:09

Óscar López