@@ -44,7 +44,7 @@ func (f *FeedRepository) FindByKeyword(query string) ([]app.Feed, error) {
4444 }
4545
4646 f := app.Feed {
47- ID : doc ["id " ].(string ),
47+ FeedID : doc ["feedID " ].(string ),
4848 Title : doc ["title" ].(string ),
4949 Link : doc ["link" ].(string ),
5050 Source : doc ["source" ].(string ),
@@ -60,45 +60,78 @@ func (f *FeedRepository) FindByKeyword(query string) ([]app.Feed, error) {
6060}
6161
6262func (f * FeedRepository ) FindByID (id string ) (app.Feed , error ) {
63- d , err := f .client .Collection ("feeds" ).Document (id ).Retrieve (f .ctx )
63+ searchParameters := & api.SearchCollectionParams {
64+ Q : id ,
65+ QueryBy : "feedID" ,
66+ }
67+ searchResult , err := f .client .Collection ("feeds" ).Documents ().Search (f .ctx , searchParameters )
6468 if err != nil {
6569 return app.Feed {}, err
6670 }
6771
68- date , err := time .Parse (time .RFC3339 , d ["date" ].(string ))
72+ if searchResult .Hits == nil || len (* searchResult .Hits ) == 0 {
73+ return app.Feed {}, fmt .Errorf ("feed with id %s not found" , id )
74+ }
75+
76+ doc := * (* searchResult .Hits )[0 ].Document
77+
78+ date , err := time .Parse (time .RFC3339 , doc ["date" ].(string ))
6979 if err != nil {
7080 return app.Feed {}, err
7181 }
7282
7383 fStruct := app.Feed {
74- ID : d [ "id " ].(string ),
75- Title : d ["title" ].(string ),
76- Link : d ["link" ].(string ),
77- Source : d ["source" ].(string ),
78- Language : d ["language" ].(string ),
79- Summary : d ["summary" ].(string ),
84+ FeedID : doc [ "feedID " ].(string ),
85+ Title : doc ["title" ].(string ),
86+ Link : doc ["link" ].(string ),
87+ Source : doc ["source" ].(string ),
88+ Language : doc ["language" ].(string ),
89+ Summary : doc ["summary" ].(string ),
8090 Date : date ,
8191 }
8292
8393 return fStruct , nil
8494}
8595
86- func (f * FeedRepository ) FindBySimilarity (feed app.Feed ) ([]app.Feed , error ) {
87- vQ := fmt .Sprintf ("title_summary_embedding:([], id: %s, distance_threshold:0.183)" , feed .ID )
88- eF := "title_summary_embedding"
96+ func (f * FeedRepository ) FindBySimilarity (feedID string ) ([]app.Feed , error ) {
97+ feed , err := f .FindByID (feedID )
98+ if err != nil {
99+ return nil , fmt .Errorf ("failed to find feed by ID: %w" , err )
100+ }
101+
89102 searchParameters := & api.SearchCollectionParams {
90- Q : feed .Title ,
91- QueryBy : "title,title_summary_embedding" ,
92- VectorQuery : & vQ ,
93- ExcludeFields : & eF ,
103+ Q : feed .Title + " " + feed .Summary ,
104+ QueryBy : "title_summary_embedding" ,
94105 }
95106 searchResult , err := f .client .Collection ("feeds" ).Documents ().Search (f .ctx , searchParameters )
96107 if err != nil {
97108 return nil , err
98109 }
99110
111+ maxVectorDistance := float32 (0.16576248 ) // Define a threshold for vector distance
112+ hits := * searchResult .Hits
113+ n := 0
114+ for _ , hit := range hits {
115+ if hit .VectorDistance == nil || * hit .VectorDistance <= maxVectorDistance {
116+ hits [n ] = hit
117+ n ++
118+ }
119+ }
120+ hits = hits [:n ]
121+ searchResult .Hits = & hits
122+
100123 feeds := make ([]app.Feed , len (* searchResult .Hits ))
101124 for i , x := range * searchResult .Hits {
125+ // If VectorDistance is present, filter by threshold
126+ if x .Document != nil && x .VectorDistance != nil {
127+ doc := * x .Document
128+ title , _ := doc ["title" ].(string )
129+ fmt .Printf ("Title: %s, VectorDistance: %v\n " , title , * x .VectorDistance )
130+ }
131+ if x .VectorDistance != nil && * x .VectorDistance > maxVectorDistance {
132+ continue
133+ }
134+
102135 doc := * x .Document
103136
104137 date , err := time .Parse (time .RFC3339 , doc ["date" ].(string ))
@@ -107,6 +140,7 @@ func (f *FeedRepository) FindBySimilarity(feed app.Feed) ([]app.Feed, error) {
107140 }
108141
109142 f := app.Feed {
143+ FeedID : doc ["feedID" ].(string ),
110144 Title : doc ["title" ].(string ),
111145 Link : doc ["link" ].(string ),
112146 Source : doc ["source" ].(string ),
@@ -123,7 +157,7 @@ func (f *FeedRepository) FindBySimilarity(feed app.Feed) ([]app.Feed, error) {
123157
124158func (f * FeedRepository ) Save (doc app.Feed ) error {
125159 docMap := map [string ]interface {}{
126- "id" : generateUniqueID (doc .Link ),
160+ "feedID" : generateUniqueID (doc .Link ),
127161 "title" : doc .Title ,
128162 "link" : doc .Link ,
129163 "source" : doc .Source ,
@@ -147,7 +181,7 @@ func (f *FeedRepository) Update(docs ...app.Feed) error {
147181 for i , doc := range docs {
148182 // Convert app.Feed to map[string]interface{} for updating
149183 docMap := map [string ]interface {}{
150- "id" : generateUniqueID (doc .Link ),
184+ "feedID" : generateUniqueID (doc .Link ),
151185 "title" : doc .Title ,
152186 "link" : doc .Link ,
153187 "source" : doc .Source ,
0 commit comments