PostgreSQL to iterate through rows and find the closest match using custom distance-function

前端 未结 2 1394
名媛妹妹
名媛妹妹 2021-01-01 02:29

I am new to PostgreSQL and my question is similar to the one here: link

For e.g I have the following table:

| id |       vector         |
|  1 |  { 1         


        
相关标签:
2条回答
  • 2021-01-01 03:12

    Generally speaking you can solve this type of problems by using a stored function, written in Java or Scala (some might prefer PL/SQL, C or C++).

    PostgreSql supports (Java based) stored functions, so let the SQL query fetch the data, and pass it to a stored function. The stored function returns the distance, so you can filter/sort etc. on it.

    Based on a table like this

    create table point(vector float8[]);
    insert into point values('{0.0, 0.0, 0.0}');
    insert into point values('{0.5, 0.5, 0.5}');
    

    with a Java function like this:

    public class PlJava {
        public final static double distance2(double[] v1, double[] v2) {
            return Math.sqrt(Math.pow(v2[0] - v1[0], 2)
              + Math.pow(v2[1] - v1[1], 2) + Math.pow(v2[2] - v1[2], 2));
        }
    }
    

    and the function declaration in SQL:

    CREATE FUNCTION pljava.distance2(float8[], float8[])
      RETURNS float8
      AS 'PlJava.distance2'
      IMMUTABLE
      LANGUAGE java;
    

    your query could look like this:

    select
        point.*, 
        pljava.distance2(vector, '{1.0, 1.0, 1.0}') as dist
      from
        point 
      order by
        dist;    
    

    which results in

        vector     |       dist  
    ---------------+-------------------  
     {0.5,0.5,0.5} | 0.866025403784439  
     {0,0,0}       |  1.73205080756888  
    

    Update

    Stored functions can be written in C and C++ as well. C++ requires more effort, because the interface to PostgreSql is using the C calling convention. See Using C++ for Extensibility

    0 讨论(0)
  • 2021-01-01 03:17

    PostgresQL has a nearest neighbor index feature

    http://wiki.postgresql.org/wiki/What%27s_new_in_PostgreSQL_9.1#K-Nearest-Neighbor_Indexing

    It can be used with PostgreSQL or PostGIS a GIS extensuion to PostgreSQL. See

    K-Nearest Neighbor Query in PostGIS

    0 讨论(0)
提交回复
热议问题