Scott Stafford
Scott Stafford

Reputation: 44808

How do I write my own ranking function for Postgres full-text search?

The documentation for PostgreSQL states:

The built-in ranking functions are only examples. You can write your own ranking functions and/or combine their results with additional factors to fit your specific needs.

But there is no information on how you might go about doing that, either in the docs or anywhere on the web that I can find. Has anyone ever attempted this, or have any examples that they can point to?

In my particular case, I was hoping to weight certain specified lexemes as less important than others.

Upvotes: 2

Views: 212

Answers (1)

jjanes
jjanes

Reputation: 44305

I think the documentation is wrong. People do use those functions as-is for ranking in the real world. If they were really "only examples", no one would actually use them, and they would be in a contrib extension, not in the core code.

Instruction on how to extend PostgreSQL are located elsewhere, likely to start with https://www.postgresql.org/docs/current/extend.html, and specifically https://www.postgresql.org/docs/current/xfunc-c.html. There are no instructions on extending ranking functions specifically.

You can certainly use the existing code as an example, you will find it in "src/backend/utils/adt/tsrank.c". You will have to adapt it be used in a user-defined function rather than the core code (see previous paragraph), and of course have to change it to do whatever it is you want it to do, instead of what it already does do. Note that the documentation did not promise that writing your own non-trivial ranking function will be easy, it will not be.

Upvotes: 2

Related Questions