Skip to main content
  1. About
  2. For Teams
Asked
Viewed 325 times
5

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?

1
  • 5
    SELECT * FROM pg_operator where oprname = '<->' ;
    joop
    –  joop
    2013-08-20 16:26:38 +00:00
    Commented Aug 20, 2013 at 16:26

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I'm pretty sure with strings its an edit distance. Here's an example Also worth noting that the OP mentioned the source was the PostgreSQL docs.
This is not an error, it is a valid usage: postgresql.org/docs/9.1/static/pgtrgm.html
You're right guys. Fixed my answer, but @lejlot should get the accepted answer
0

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

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.