Example #1
0
func putSongDoc(c mp.Context, id int64, s *chordpro.Song) error {
	index, err := search.Open("songs")
	if err != nil {
		return err
	}

	doc := &data.SongDoc{
		Title:  m.Wordify(s.Title),
		Artist: m.Wordify(strings.Join(s.Artists, " ")),
		Album:  m.Wordify(s.Album),
		Lyric:  m.Wordify(s.Lyric()),
	}
	_, err = index.Put(c, strconv.FormatInt(id, 10), doc)
	if err != nil {
		return err
	}

	return nil
}
Example #2
0
func Search(c mp.Context, w http.ResponseWriter, r *http.Request) error {
	query := r.URL.Query()
	q := query.Get("q")
	if q == "" {
		return core.Render(w, "songs.search.tmpl.html", &SongSearchResult{Includes: core.NewIncludes(c)})
	}

	searchQuery := m.Wordify(q)
	docList, docIDs, err := data.SearchSongs(c, searchQuery)
	if err != nil {
		return err
	}

	if len(docList) == 0 {
		c.Infof("AND query [%s] return nothing", searchQuery)
		orQuery := strings.Join(strings.Fields(searchQuery), " OR ")
		c.Infof("Searching with OR query [%s]", orQuery)
		docList, docIDs, err = data.SearchSongs(c, orQuery)
		if err != nil {
			return err
		}
	}

	items := make([]*SongSearchResultItem, 0)
	for i, _ := range docList {
		items = append(items, createSongSearchResultItem(q, docIDs[i], docList[i]))
	}

	result := &SongSearchResult{
		Includes: core.NewIncludes(c),
		Query:    q,
		Items:    items,
	}

	return core.Render(w, "songs.search.tmpl.html", result)
}