Skip to content

Commit 6db5f94

Browse files
committed
feat: add by similarity
1 parent d386774 commit 6db5f94

File tree

8 files changed

+135
-75
lines changed

8 files changed

+135
-75
lines changed

Dockerfile

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,15 @@ WORKDIR /data
2626
COPY --from=builder /app/main /app/main
2727

2828
ARG UNCONDITIONAL_API_SOURCE_REPO
29-
ARG UNCONDITIONAL_API_SOURCE_CLIENT_KEY
30-
ARG UNCONDITIONAL_API_FEED_REPO_HOST
31-
ARG UNCONDITIONAL_API_FEED_REPO_KEY
29+
ARG UNCONDITIONAL_API_FEED_REPO_INDEX
3230
ARG UNCONDITIONAL_API_LOG_ENV
3331

3432
ENV UNCONDITIONAL_API_SOURCE_REPO=${UNCONDITIONAL_API_SOURCE_REPO}
35-
ENV UNCONDITIONAL_API_SOURCE_CLIENT_KEY=${UNCONDITIONAL_API_SOURCE_CLIENT_KEY}
36-
ENV UNCONDITIONAL_API_FEED_REPO_HOST=${UNCONDITIONAL_API_FEED_REPO_HOST}
37-
ENV UNCONDITIONAL_API_FEED_REPO_KEY=${UNCONDITIONAL_API_FEED_REPO_KEY}
33+
ENV UNCONDITIONAL_API_FEED_REPO_INDEX=${UNCONDITIONAL_API_FEED_REPO_INDEX}
3834
ENV UNCONDITIONAL_API_LOG_ENV=${UNCONDITIONAL_API_LOG_ENV}
3935

4036
RUN --mount=type=secret,id=UNCONDITIONAL_API_SOURCE_CLIENT_KEY \
4137
--mount=type=secret,id=UNCONDITIONAL_API_FEED_REPO_HOST \
42-
--mount=type=secret,id=UNCONDITIONAL_API_FEED_REPO_INDEX \
4338
--mount=type=secret,id=UNCONDITIONAL_API_FEED_REPO_KEY \
4439
UNCONDITIONAL_API_SOURCE_CLIENT_KEY="$(cat /run/secrets/UNCONDITIONAL_API_SOURCE_CLIENT_KEY)" \
4540
UNCONDITIONAL_API_FEED_REPO_HOST="$(cat /run/secrets/UNCONDITIONAL_API_FEED_REPO_HOST)" \
@@ -55,18 +50,14 @@ ARG UNCONDITIONAL_API_ADDRESS
5550
ARG UNCONDITIONAL_API_ALLOWED_ORIGINS
5651
ARG UNCONDITIONAL_API_PORT
5752
ARG UNCONDITIONAL_API_SOURCE_REPO
58-
ARG UNCONDITIONAL_API_SOURCE_CLIENT_KEY
5953
ARG UNCONDITIONAL_API_LOG_ENV
60-
ARG UNCONDITIONAL_API_FEED_REPO_HOST
61-
ARG UNCONDITIONAL_API_FEED_REPO_KEY
54+
ARG UNCONDITIONAL_API_FEED_REPO_INDEX
6255

6356
ENV UNCONDITIONAL_API_ADDRESS=${UNCONDITIONAL_API_ADDRESS}
6457
ENV UNCONDITIONAL_API_ALLOWED_ORIGINS=${UNCONDITIONAL_API_ALLOWED_ORIGINS}
6558
ENV UNCONDITIONAL_API_PORT=${UNCONDITIONAL_API_PORT}
6659
ENV UNCONDITIONAL_API_SOURCE_REPO=${UNCONDITIONAL_API_SOURCE_REPO}
67-
ENV UNCONDITIONAL_API_SOURCE_CLIENT_KEY=${UNCONDITIONAL_API_SOURCE_CLIENT_KEY}
6860
ENV UNCONDITIONAL_API_LOG_ENV=${UNCONDITIONAL_API_LOG_ENV}
69-
ENV UNCONDITIONAL_API_FEED_REPO_HOST=${UNCONDITIONAL_API_FEED_REPO_HOST}
70-
ENV UNCONDITIONAL_API_FEED_REPO_KEY=${UNCONDITIONAL_API_FEED_REPO_KEY}
61+
ENV UNCONDITIONAL_API_FEED_REPO_INDEX=${UNCONDITIONAL_API_FEED_REPO_INDEX}
7162

7263
ENTRYPOINT ["./app/main","serve", "--address", "0.0.0.0", "--port","8080","--feed-repo-index","feeds"]

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ test-integration:
5656
build:
5757
@go build --tags=release -o ${_PROJECT_DIRECTORY}/bin/unconditional-server
5858

59+
.PHONY: deploy
60+
61+
deploy:
62+
@sh ./scripts/deploy.sh
63+
5964
# Helpers
6065
check-variable-%: # detection of undefined variables.
6166
@[[ "${${*}}" ]] || (echo '*** Please define variable `${*}` ***' && exit 1)

api/api.gen.go

Lines changed: 32 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yaml

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
11
openapi: 3.0.2
22
x-stoplight:
3-
id: 44w9u0405b53e
3+
id: g0km90jnbo4i0
44
info:
55
title: Unconditional
6-
version: "1.0"
6+
version: '1.0'
77
servers:
8-
- url: "https://api.unconditional.day/v1"
9-
- url: "http://localhost:8080"
10-
- url: "127.0.0.1:8080"
8+
- url: 'https://api.unconditional.day/v1'
9+
- url: 'http://localhost:8080'
10+
- url: '127.0.0.1:8080'
1111
paths:
12-
"/v1/search/feed/{query}":
12+
'/v1/search/feed/{query}':
1313
get:
1414
responses:
15-
"200":
15+
'200':
1616
description: OK
1717
content:
1818
application/json:
1919
schema:
2020
type: array
2121
items:
22-
$ref: "#/components/schemas/FeedItem"
23-
"500":
22+
$ref: '#/components/schemas/FeedItem'
23+
'500':
2424
description: Internal Server Error
2525
content:
2626
application/json:
2727
schema:
2828
type: object
29-
$ref: "#/components/schemas/Error"
30-
parameters:
31-
- name: query
32-
in: path
33-
required: true
34-
schema:
35-
type: string
29+
$ref: '#/components/schemas/Error'
3630
x-stoplight:
37-
id: wosl2fd4xhhrd
38-
"/v1/search/context/{query}":
31+
id: e6uenhk2m21k5
32+
parameters:
33+
- schema:
34+
type: boolean
35+
in: query
36+
name: bySimilarity
37+
parameters:
38+
- schema:
39+
type: string
40+
name: query
41+
in: path
42+
required: true
43+
'/v1/search/context/{query}':
3944
get:
4045
responses:
41-
"200":
46+
'200':
4247
description: OK
4348
content:
4449
application/json:
4550
schema:
4651
type: object
47-
$ref: "#/components/schemas/SearchContextDetails"
48-
"500":
52+
$ref: '#/components/schemas/SearchContextDetails'
53+
'500':
4954
description: Internal Server Error
5055
content:
5156
application/json:
5257
schema:
5358
type: object
54-
$ref: "#/components/schemas/Error"
59+
$ref: '#/components/schemas/Error'
5560
parameters:
5661
- name: query
5762
in: path
5863
required: true
5964
schema:
6065
type: string
6166
x-stoplight:
62-
id: idoe8qr80ebxd
63-
"/v1/version":
67+
id: 7c394v6kr5tff
68+
/v1/version:
6469
get:
6570
summary: Your GET endpoint
6671
tags: []
6772
responses:
68-
"200":
73+
'200':
6974
description: OK
7075
content:
7176
application/json:
7277
schema:
7378
type: object
74-
$ref: "#/components/schemas/ServerVersion"
79+
$ref: '#/components/schemas/ServerVersion'
7580
operationId: get-v1-version
7681
x-stoplight:
77-
id: q4lvzczpgn9wh
82+
id: oucep4v9ubq2z
7883
requestBody:
7984
content:
8085
application/json:
@@ -98,7 +103,7 @@ components:
98103
type: string
99104
image:
100105
type: object
101-
$ref: "#/components/schemas/FeedImage"
106+
$ref: '#/components/schemas/FeedImage'
102107
date:
103108
type: string
104109
format: date-time
@@ -110,7 +115,7 @@ components:
110115
- language
111116
- date
112117
x-stoplight:
113-
id: xa4xmb6bpguaf
118+
id: kwewfmf4xgq3e
114119
FeedImage:
115120
type: object
116121
properties:
@@ -122,11 +127,11 @@ components:
122127
- url
123128
- title
124129
x-stoplight:
125-
id: cmmybtqamyqiy
130+
id: albynchitcqzu
126131
SearchContextDetails:
127132
type: object
128133
x-stoplight:
129-
id: 2bea7d1686f73
134+
id: fhfrlaebk9ask
130135
properties:
131136
title:
132137
type: string
@@ -156,51 +161,51 @@ components:
156161
- message
157162
- code
158163
x-stoplight:
159-
id: ugjrjhvkdmen2
164+
id: mffsjrkb81ot5
160165
ServerVersion:
161166
title: ServerVersion
162167
x-stoplight:
163-
id: 24qo1fm939bg5
168+
id: 4m4ndcimoercb
164169
type: object
165170
properties:
166171
source:
167-
$ref: "#/components/schemas/SourceReleaseVersion"
172+
$ref: '#/components/schemas/SourceReleaseVersion'
168173
build:
169-
$ref: "#/components/schemas/ServerBuildVersion"
174+
$ref: '#/components/schemas/ServerBuildVersion'
170175
required:
171176
- source
172177
- build
173178
SourceReleaseVersion:
174179
title: SourceReleaseVersion
175180
x-stoplight:
176-
id: ufsclw4vu3qp5
181+
id: qrni9cae7qsr6
177182
type: object
178183
properties:
179184
version:
180185
type: string
181186
x-stoplight:
182-
id: t6xp7ev3nprdj
187+
id: g42kpmf59e9sb
183188
lastUpdatedAt:
184189
type: string
185190
x-stoplight:
186-
id: 26hfi8gtj9bdi
191+
id: ozgly07pllzwb
187192
required:
188193
- version
189194
- lastUpdatedAt
190195
ServerBuildVersion:
191196
title: ServerBuildVersion
192197
x-stoplight:
193-
id: p7byzats6s3zt
198+
id: a6g4vsjso6ozw
194199
type: object
195200
properties:
196201
commit:
197202
type: string
198203
x-stoplight:
199-
id: eoik296b0ddt3
204+
id: 48tl37vuwu47p
200205
version:
201206
type: string
202207
x-stoplight:
203-
id: jup15zjavkg3a
208+
id: mwqxrg5t0vvst
204209
required:
205210
- commit
206211
- version

internal/app/feed.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
type FeedRepository interface {
88
// Search returns the results of a search query.
99
Find(query string) ([]Feed, error)
10+
// Search returns the results of a search query by similarity.
11+
FindBySimilarity(query string) ([]Feed, error)
1012
// Index indexes a document.
1113
Save(doc Feed) error
1214
// Update a document in index.

internal/repository/typesense/feed.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,42 @@ func (f *FeedRepository) Find(query string) ([]app.Feed, error) {
5555
return feeds, nil
5656
}
5757

58+
59+
func (f *FeedRepository) FindBySimilarity(query string) ([]app.Feed, error){
60+
searchParameters := &api.SearchCollectionParams{
61+
Q: query,
62+
QueryBy: "title_summary_embedding",
63+
}
64+
searchResult, err := f.client.Collection("feeds").Documents().Search(f.ctx, searchParameters)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
feeds := make([]app.Feed, len(*searchResult.Hits))
70+
for i, x := range *searchResult.Hits {
71+
doc := *x.Document
72+
73+
date, err := time.Parse(time.RFC3339, doc["date"].(string))
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
f := app.Feed{
79+
Title: doc["title"].(string),
80+
Link: doc["link"].(string),
81+
Source: doc["source"].(string),
82+
Language: doc["language"].(string),
83+
Summary: doc["summary"].(string),
84+
Date: date,
85+
}
86+
87+
feeds[i] = f
88+
}
89+
90+
return feeds, nil
91+
}
92+
93+
5894
func (f *FeedRepository) Save(doc app.Feed) error {
5995
docMap := map[string]interface{}{
6096
"id": doc.Link,

0 commit comments

Comments
 (0)