<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tasteby.mapper.VectorMapper">

    <resultMap id="vectorSearchResultMap" type="VectorSearchResult">
        <result property="restaurantId" column="restaurant_id"/>
        <result property="chunkText" column="chunk_text"
                typeHandler="com.tasteby.config.ClobTypeHandler"/>
        <result property="distance" column="dist"/>
    </resultMap>

    <select id="searchSimilar" resultMap="vectorSearchResultMap">
        <![CDATA[
        SELECT rv.restaurant_id, rv.chunk_text,
               VECTOR_DISTANCE(rv.embedding, TO_VECTOR(#{queryVec}), COSINE) AS dist
        FROM restaurant_vectors rv
        WHERE VECTOR_DISTANCE(rv.embedding, TO_VECTOR(#{queryVec}), COSINE) <= #{maxDistance}
        ORDER BY dist
        FETCH FIRST #{topK} ROWS ONLY
        ]]>
    </select>

    <insert id="insertVector">
        INSERT INTO restaurant_vectors (id, restaurant_id, chunk_text, embedding)
        VALUES (#{id}, #{restaurantId}, #{chunkText}, TO_VECTOR(#{embedding}))
    </insert>

</mapper>
