Rework search to return a list

Like a proper search would!"
This commit is contained in:
2024-08-13 17:41:01 +02:00
parent a6626761e7
commit 8fd8d53cc3
6 changed files with 61 additions and 32 deletions

6
app.go
View File

@@ -44,12 +44,12 @@ func (a *App) UpdateFood(food Food) WailsFood1 {
return WailsFood1{Data: data, Success: true} return WailsFood1{Data: data, Success: true}
} }
func (a *App) GetLastPer100(name string) WailsFood1 { func (a *App) GetLastPer100(name string) WailsFoodSearch {
data, err := foodService.GetLastPer100(name) data, err := foodService.GetLastPer100(name)
if err != nil { if err != nil {
return WailsFood1{Success: false, Error: err.Error()} return WailsFoodSearch{Success: false, Error: err.Error()}
} }
return WailsFood1{Data: data, Success: true} return WailsFoodSearch{Data: data, Success: true}
} }
func (a *App) GetDailyFood() WailsAggregateFood { func (a *App) GetDailyFood() WailsAggregateFood {

View File

@@ -62,11 +62,12 @@ create table food(
); );
create virtual table foodfix using spellfix1; create virtual table foodfix using spellfix1;
-- insert into foodfix (word, rank) insert into foodfix (word, rank)
-- select food as word, select food as word,
-- count(*) as rank count(*) as rank
-- from food from food
-- group by food; group by food;
drop trigger if exists food_foodfix_insert; drop trigger if exists food_foodfix_insert;
create trigger food_foodfix_insert AFTER create trigger food_foodfix_insert AFTER
insert on food for EACH row insert on food for EACH row
@@ -125,15 +126,15 @@ end;
-- Spellfix search example -- Spellfix search example
with search_results as ( with search_results as (
select word, rank select word, score, f.rowid, f.*
from foodfix from foodfix
where word MATCH 'B' inner join food f on f.food == word
limit 1 where word match 'B'
) )
select f.*, s.rank select rowid, food, score, date, description, amount, per100, energy
from search_results s from search_results
inner join food f on s.word = f.food group by food
order by f.date desc; order by score asc, date desc
create index dailyIdx on food(strftime('%Y-%m-%d', date)); create index dailyIdx on food(strftime('%Y-%m-%d', date));
create index weeklyIdx on food(strftime('%Y-%W', date)); create index weeklyIdx on food(strftime('%Y-%W', date));

View File

@@ -19,6 +19,10 @@ type (
Per100 float32 `json:"per100"` Per100 float32 `json:"per100"`
Energy float32 `json:"energy"` Energy float32 `json:"energy"`
} }
FoodSearch struct {
Food
Score int64 `json:"score"`
}
AggregatedFood struct { AggregatedFood struct {
Period string `json:"period"` Period string `json:"period"`
Amount float32 `json:"amount"` Amount float32 `json:"amount"`
@@ -56,27 +60,44 @@ func (s *FoodService) GetRecent() ([]Food, error) {
return res, nil return res, nil
} }
func (s *FoodService) GetLastPer100(name string) (Food, error) { func (s *FoodService) GetLastPer100(name string) ([]FoodSearch, error) {
res := Food{} res := []FoodSearch{}
if s.db == nil || !s.db.Ready { if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get last per100, db is nil or is not ready") return res, fmt.Errorf("cannot get last per100, db is nil or is not ready")
} }
row := s.db.readConn.QueryRow(fmt.Sprintf(` query := fmt.Sprintf(`
with search_results as ( with search_results as (
select word, rank select word, score, f.rowid, f.*
from foodfix from foodfix
inner join food f on f.food == word
where word MATCH ? where word MATCH ?
limit 1
) )
select %s select %s, score
from search_results s from search_results
inner join food f on s.word = f.food group by food
order by f.date desc; order by score asc, date desc
`, foodColumns), name) limit %d
err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy) `, foodColumns, Settings.SearchLimit)
// log.Printf("%#v", query)
rows, err := s.db.readConn.Query(query, name)
if err != nil { if err != nil {
return res, fmt.Errorf("error scanning row: %v", err) log.Printf("error getting last per100: %v", err)
return res, err
}
for rows.Next() {
var f FoodSearch
err := rows.Scan(&f.Rowid, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
}
res = append(res, f)
}
if len(res) == 0 {
return nil, fmt.Errorf("no results found for %s", name)
} }
return res, nil return res, nil

View File

@@ -55,7 +55,8 @@
if (!per100Edited) if (!per100Edited)
GetLastPer100(name.trim()).then((res) => { GetLastPer100(name.trim()).then((res) => {
if (res.success) { if (res.success) {
per100 = res.data.toString(); console.log(res.data[0].food);
per100 = res.data[0].per100.toString();
} }
}); });
} }

View File

@@ -30,6 +30,7 @@ type settings struct {
Target int `default:"2000" json:"target"` Target int `default:"2000" json:"target"`
Limit int `default:"2500" json:"limit"` Limit int `default:"2500" json:"limit"`
SearchLimit int `default:"5" json:"searchLimit"`
} }
var Settings settings var Settings settings

View File

@@ -28,6 +28,11 @@ type (
Success bool `json:"success"` Success bool `json:"success"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
WailsFoodSearch struct {
Data []FoodSearch `json:"data"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
WailsWeight struct { WailsWeight struct {
Data []Weight `json:"data"` Data []Weight `json:"data"`