RyanKDalton
RyanKDalton

Reputation: 1271

Explain the use of <-> in 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?

Upvotes: 5

Views: 310

Answers (3)

Bohemian
Bohemian

Reputation: 425448

It's the "distance between" operator, but it's only documented operands are geometric types (points, shapes, etc).

After some experimentation with strings, it seems to return a function of the levenstein distance:

  • 'abc' <-> 'abc' --> 0
  • 'abc' <-> 'abcd' --> 0.5
  • 'abc' <-> 'abd' --> 0.6666

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236170

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.

Upvotes: 1

lejlot
lejlot

Reputation: 66835

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

Upvotes: 4

Related Questions