eintopf.info@v0.13.16/service/indexo/group.go (about) 1 // Copyright (C) 2022 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package indexo 17 18 import ( 19 "strings" 20 21 "eintopf.info/service/group" 22 ) 23 24 func (s *service) indexGroup(group *group.Group) error { 25 return s.searchService.Index(groupDocumentFromGroup(group)) 26 } 27 28 func groupDocumentFromGroup(group *group.Group) *GroupDocument { 29 return &GroupDocument{ 30 ID: group.ID, 31 Published: group.Published, 32 Name: group.Name, 33 Link: group.Link, 34 Email: group.Email, 35 Image: group.Image, 36 Description: group.Description, 37 Listed: group.Listable(), 38 } 39 } 40 41 type GroupDocument struct { 42 ID string `json:"id"` 43 Published bool `json:"published"` 44 Name string `json:"name"` 45 Link string `json:"link"` 46 Email string `json:"email"` 47 Image string `json:"image"` 48 Description string `json:"description"` 49 Listed bool `json:"listed"` 50 } 51 52 // Identifier returns an id which should uniquely identify the object for 53 // its type. 54 func (g *GroupDocument) Identifier() string { 55 return g.ID 56 } 57 58 // Type returns the type of the object. 59 func (g *GroupDocument) Type() string { 60 return "group" 61 } 62 63 // QueryText returns the string to index for a text search. 64 func (g *GroupDocument) QueryText() string { 65 var b strings.Builder 66 b.WriteString(g.Name) 67 b.WriteString(" ") 68 b.WriteString(g.Description) 69 return b.String() 70 } 71 72 // SearchFields returns a map of fields to be index for searching. 73 func (g *GroupDocument) SearchFields() map[string]interface{} { 74 return map[string]interface{}{ 75 "is": g.Type(), 76 "name": g.Name, 77 "id": g.ID, 78 "listed": g.Listed, 79 "published": g.Published, 80 } 81 }