Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

postgresql - Use gorm with to_tsvector when creating new entries

I'm following this example of running to_tsvector() over an item as its added to a postgres DB: https://github.com/sachaarbonel/pgsearch-gorm-example/blob/master/search/main.go

articles := []Article{
        Article{Title: "Title of Pack my box with five dozen liquor jugs.", PublishDate: now, Summary: "Summary of Pack my box with five dozen liquor jugs.", TopImage: "url"},
        Article{Title: "Title of Jackdaws love my big sphinx of quartz.", PublishDate: now, Summary: "Summary of Jackdaws love my big sphinx of quartz.", TopImage: "url"},
        Article{Title: "Title of The five boxing wizards jump quickly.", PublishDate: now, Summary: "Summary of The five boxing wizards jump quickly.", TopImage: "url"},
        Article{Title: "Title of How vexingly quick daft zebras jump!", PublishDate: now, Summary: "Summary of How vexingly quick daft zebras jump!", TopImage: "url"},
    }

    for _, a := range articles {
        db.Exec("INSERT INTO articles (title, title_tokens, publish_date, summary, summary_tokens, top_image) VALUES (?, to_tsvector(?), ?, ?, to_tsvector(?), ?)", a.Title, a.Title, now, a.Summary, a.Summary, a.TopImage)
    }

This example works fine, but I am wondering if theres a way to do this more succinctly without executing raw SQL. Example:

db.Model(Article{}).Create(map[string]interface{}{
  Title: "Title of Pack my box with five dozen liquor jugs.", PublishDate: now, Summary: "Summary of Pack my box with five dozen liquor jugs.", TopImage: "url",
})

With the above example, how would I also then include to_tsvector over several of the key/values?

I have tried the following documentation but I just can't seem to get it to work. Is this even the correct way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...