FUNCTION get_similar_student_list( query_vector vector(1536) ) RETURNS SETOF type_student_list_output AS $FUNCTION$ DECLARE hit_keywords type_hit_keyword[]; begin RETURN QUERY SELECT stu.student_id, stu.student_name, ARRAY_AGG( ROW( key.keyword_id, key.keyword_text, key.embedding <-> query_vector )::type_hit_keyword ) AS hit_keywords FROM students stu LEFT JOIN student_keywords_relations skr ON stu.student_id = skr.student_id LEFT JOIN keywords key ON skr.keyword_id = key.keyword_id GROUP BY stu.student_id ORDER BY stu.keyword_list_embedding <-> query_vector limit 10; END; $FUNCTION$ LANGUAGE plpgsql; ストアドプロシージャでかいてvector を渡せば、 ⼀発で返ってくるものをかけて、Rust の Struct で受け取れる。 [{ student_id: 1, student_name: ⼭⽥花⼦, keyword_list: [ { keyword_id: 1, keyword_text: "NLP", distance: 0.1 }, { keyword_id: 2, keyword_text: "深層学習", distance: 0.8 }, ] }, …, {...} ] ユニークビジョンさん の CTO の方のブログ参考にしました https://qiita.com/aoyagikouhei/items/9e52ae732d0f795f4dfc