How can you emulate a Solr "more like this query" with Postgresql full text search?

postgresql, solr

Solution

Not out of the box I am afraid. It might be possible to compare two tsvectors to determine if they are similar enough, or pull the top n similar tsvectors, but there is no out of the box functionality to do this. The good news is that since tsvectors support GIN indexing, the complicated part is done for you.

What I think you'd need to do is create a function in C which determines the intersection of two tsvectors. From there you could create a function which determines if they overlap and an operator which addresses this. From there it shouldn't be too hard to create a ranking based on largest overlap.

Of course I suspect that this will be easiest to do in a language like C but you could probably use other procedural languages as well if you need to.

The wonderful thing about PostgreSQL is that anything is possible. of course the downside is that when you move further from core functionality you get to do a lot of it yourself.

Problem

I'd like to emulate this type of Solr query: http://wiki.apache.org/solr/MoreLikeThis with PostgreSQL using its full text search facility. Is there a way to do something like a "more like this" query with pure postgres?

Original source