eintopf.info@v0.13.16/service/eventsearch/aggregation.go (about) 1 // Code generated by scripts/gen/go/searchaggregation/main.go. DO NOT EDIT. 2 package eventsearch 3 4 import ( 5 "encoding/json" 6 "fmt" 7 "net/url" 8 "strings" 9 10 "eintopf.info/service/event" 11 "eintopf.info/service/search" 12 ) 13 14 func aggregationsFromQuery(q url.Values) (Aggregations, error) { 15 aggregationsRaw := q["aggregations"] 16 aggregations := make(Aggregations, len(aggregationsRaw)) 17 for _, a := range aggregationsRaw { 18 splitted := strings.Split(a, "-") 19 if len(splitted) != 3 { 20 return nil, fmt.Errorf("parse aggregation(%s): must be of form 'name-field-type'", a) 21 } 22 name := splitted[0] 23 field := splitted[1] 24 typ := splitted[2] 25 filters, err := filtersFromQuery(q, fmt.Sprintf("%s-", name)) 26 if err != nil { 27 return nil, fmt.Errorf("parse aggregation(%s): %s", a, err) 28 } 29 switch field { 30 31 case "children": 32 33 if typ == "terms" { 34 aggregations[name] = ChildrenTermsAggregation{Filters: filters} 35 continue 36 } 37 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 38 39 case "date": 40 41 if typ == "terms" { 42 aggregations[name] = DateTermsAggregation{Filters: filters} 43 continue 44 } 45 if typ == "terms" { 46 aggregations[name] = DateTermsAggregation{Filters: filters} 47 continue 48 } 49 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 50 51 case "day": 52 53 if typ == "terms" { 54 aggregations[name] = DayTermsAggregation{Filters: filters} 55 continue 56 } 57 if typ == "terms" { 58 aggregations[name] = DayTermsAggregation{Filters: filters} 59 continue 60 } 61 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 62 63 case "end": 64 65 if typ == "terms" { 66 aggregations[name] = EndTermsAggregation{Filters: filters} 67 continue 68 } 69 if typ == "terms" { 70 aggregations[name] = EndTermsAggregation{Filters: filters} 71 continue 72 } 73 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 74 75 case "groupID": 76 77 if typ == "terms" { 78 aggregations[name] = GroupIDTermsAggregation{Filters: filters} 79 continue 80 } 81 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 82 83 case "id": 84 85 if typ == "terms" { 86 aggregations[name] = IDTermsAggregation{Filters: filters} 87 continue 88 } 89 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 90 91 case "involved": 92 93 if typ == "object" { 94 aggregations[name] = InvolvedObjectsAggregation{Filters: filters} 95 continue 96 } 97 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 98 99 case "is": 100 101 if typ == "terms" { 102 aggregations[name] = ISTermsAggregation{Filters: filters} 103 continue 104 } 105 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 106 107 case "listed": 108 109 if typ == "terms" { 110 aggregations[name] = ListedTermsAggregation{Filters: filters} 111 continue 112 } 113 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 114 115 case "location": 116 117 if typ == "terms" { 118 aggregations[name] = LocationTermsAggregation{Filters: filters} 119 continue 120 } 121 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 122 123 case "location2": 124 125 if typ == "terms" { 126 aggregations[name] = Location2TermsAggregation{Filters: filters} 127 continue 128 } 129 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 130 131 case "multiday": 132 133 if typ == "terms" { 134 aggregations[name] = MultidayTermsAggregation{Filters: filters} 135 continue 136 } 137 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 138 139 case "name": 140 141 if typ == "terms" { 142 aggregations[name] = NameTermsAggregation{Filters: filters} 143 continue 144 } 145 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 146 147 case "organizer": 148 149 if typ == "terms" { 150 aggregations[name] = OrganizerTermsAggregation{Filters: filters} 151 continue 152 } 153 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 154 155 case "ownedBy": 156 157 if typ == "terms" { 158 aggregations[name] = OwnedByTermsAggregation{Filters: filters} 159 continue 160 } 161 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 162 163 case "placeID": 164 165 if typ == "terms" { 166 aggregations[name] = PlaceIDTermsAggregation{Filters: filters} 167 continue 168 } 169 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 170 171 case "start": 172 173 if typ == "terms" { 174 aggregations[name] = StartTermsAggregation{Filters: filters} 175 continue 176 } 177 if typ == "terms" { 178 aggregations[name] = StartTermsAggregation{Filters: filters} 179 continue 180 } 181 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 182 183 case "tag": 184 185 if typ == "terms" { 186 aggregations[name] = TagTermsAggregation{Filters: filters} 187 continue 188 } 189 return nil, fmt.Errorf("parse aggregation(%s): invalid aggregation type: %s", a, typ) 190 191 } 192 } 193 return aggregations, nil 194 } 195 196 type ChildrenTermsAggregation struct { 197 Filters Filters 198 } 199 200 func (a ChildrenTermsAggregation) SearchAggregation() search.Aggregation { 201 return search.Aggregation{ 202 Type: search.TermsAggregation, 203 Field: "children", 204 Filters: a.Filters.SearchFilters(), 205 } 206 } 207 208 type DateTermsAggregation struct { 209 Filters Filters 210 } 211 212 func (a DateTermsAggregation) SearchAggregation() search.Aggregation { 213 return search.Aggregation{ 214 Type: search.TermsAggregation, 215 Field: "date", 216 Filters: a.Filters.SearchFilters(), 217 } 218 } 219 220 type DateDateRangeAggregation struct { 221 Filters Filters 222 } 223 224 func (a DateDateRangeAggregation) SearchAggregation() search.Aggregation { 225 return search.Aggregation{ 226 Type: search.DateRangeAggregation, 227 Field: "date", 228 Filters: a.Filters.SearchFilters(), 229 } 230 } 231 232 type DayTermsAggregation struct { 233 Filters Filters 234 } 235 236 func (a DayTermsAggregation) SearchAggregation() search.Aggregation { 237 return search.Aggregation{ 238 Type: search.TermsAggregation, 239 Field: "day", 240 Filters: a.Filters.SearchFilters(), 241 } 242 } 243 244 type DayDateRangeAggregation struct { 245 Filters Filters 246 } 247 248 func (a DayDateRangeAggregation) SearchAggregation() search.Aggregation { 249 return search.Aggregation{ 250 Type: search.DateRangeAggregation, 251 Field: "day", 252 Filters: a.Filters.SearchFilters(), 253 } 254 } 255 256 type EndTermsAggregation struct { 257 Filters Filters 258 } 259 260 func (a EndTermsAggregation) SearchAggregation() search.Aggregation { 261 return search.Aggregation{ 262 Type: search.TermsAggregation, 263 Field: "end", 264 Filters: a.Filters.SearchFilters(), 265 } 266 } 267 268 type EndDateRangeAggregation struct { 269 Filters Filters 270 } 271 272 func (a EndDateRangeAggregation) SearchAggregation() search.Aggregation { 273 return search.Aggregation{ 274 Type: search.DateRangeAggregation, 275 Field: "end", 276 Filters: a.Filters.SearchFilters(), 277 } 278 } 279 280 type GroupIDTermsAggregation struct { 281 Filters Filters 282 } 283 284 func (a GroupIDTermsAggregation) SearchAggregation() search.Aggregation { 285 return search.Aggregation{ 286 Type: search.TermsAggregation, 287 Field: "groupID", 288 Filters: a.Filters.SearchFilters(), 289 } 290 } 291 292 type IDTermsAggregation struct { 293 Filters Filters 294 } 295 296 func (a IDTermsAggregation) SearchAggregation() search.Aggregation { 297 return search.Aggregation{ 298 Type: search.TermsAggregation, 299 Field: "id", 300 Filters: a.Filters.SearchFilters(), 301 } 302 } 303 304 type InvolvedObjectsAggregation struct { 305 Filters Filters 306 } 307 308 func (a InvolvedObjectsAggregation) SearchAggregation() search.Aggregation { 309 return search.Aggregation{ 310 Type: search.TermsAggregation, 311 Field: "involved", 312 Filters: a.Filters.SearchFilters(), 313 } 314 } 315 316 type ISTermsAggregation struct { 317 Filters Filters 318 } 319 320 func (a ISTermsAggregation) SearchAggregation() search.Aggregation { 321 return search.Aggregation{ 322 Type: search.TermsAggregation, 323 Field: "is", 324 Filters: a.Filters.SearchFilters(), 325 } 326 } 327 328 type ListedTermsAggregation struct { 329 Filters Filters 330 } 331 332 func (a ListedTermsAggregation) SearchAggregation() search.Aggregation { 333 return search.Aggregation{ 334 Type: search.TermsAggregation, 335 Field: "listed", 336 Filters: a.Filters.SearchFilters(), 337 } 338 } 339 340 type LocationTermsAggregation struct { 341 Filters Filters 342 } 343 344 func (a LocationTermsAggregation) SearchAggregation() search.Aggregation { 345 return search.Aggregation{ 346 Type: search.TermsAggregation, 347 Field: "location", 348 Filters: a.Filters.SearchFilters(), 349 } 350 } 351 352 type Location2TermsAggregation struct { 353 Filters Filters 354 } 355 356 func (a Location2TermsAggregation) SearchAggregation() search.Aggregation { 357 return search.Aggregation{ 358 Type: search.TermsAggregation, 359 Field: "location2", 360 Filters: a.Filters.SearchFilters(), 361 } 362 } 363 364 type MultidayTermsAggregation struct { 365 Filters Filters 366 } 367 368 func (a MultidayTermsAggregation) SearchAggregation() search.Aggregation { 369 return search.Aggregation{ 370 Type: search.TermsAggregation, 371 Field: "multiday", 372 Filters: a.Filters.SearchFilters(), 373 } 374 } 375 376 type NameTermsAggregation struct { 377 Filters Filters 378 } 379 380 func (a NameTermsAggregation) SearchAggregation() search.Aggregation { 381 return search.Aggregation{ 382 Type: search.TermsAggregation, 383 Field: "name", 384 Filters: a.Filters.SearchFilters(), 385 } 386 } 387 388 type OrganizerTermsAggregation struct { 389 Filters Filters 390 } 391 392 func (a OrganizerTermsAggregation) SearchAggregation() search.Aggregation { 393 return search.Aggregation{ 394 Type: search.TermsAggregation, 395 Field: "organizer", 396 Filters: a.Filters.SearchFilters(), 397 } 398 } 399 400 type OwnedByTermsAggregation struct { 401 Filters Filters 402 } 403 404 func (a OwnedByTermsAggregation) SearchAggregation() search.Aggregation { 405 return search.Aggregation{ 406 Type: search.TermsAggregation, 407 Field: "ownedBy", 408 Filters: a.Filters.SearchFilters(), 409 } 410 } 411 412 type PlaceIDTermsAggregation struct { 413 Filters Filters 414 } 415 416 func (a PlaceIDTermsAggregation) SearchAggregation() search.Aggregation { 417 return search.Aggregation{ 418 Type: search.TermsAggregation, 419 Field: "placeID", 420 Filters: a.Filters.SearchFilters(), 421 } 422 } 423 424 type StartTermsAggregation struct { 425 Filters Filters 426 } 427 428 func (a StartTermsAggregation) SearchAggregation() search.Aggregation { 429 return search.Aggregation{ 430 Type: search.TermsAggregation, 431 Field: "start", 432 Filters: a.Filters.SearchFilters(), 433 } 434 } 435 436 type StartDateRangeAggregation struct { 437 Filters Filters 438 } 439 440 func (a StartDateRangeAggregation) SearchAggregation() search.Aggregation { 441 return search.Aggregation{ 442 Type: search.DateRangeAggregation, 443 Field: "start", 444 Filters: a.Filters.SearchFilters(), 445 } 446 } 447 448 type TagTermsAggregation struct { 449 Filters Filters 450 } 451 452 func (a TagTermsAggregation) SearchAggregation() search.Aggregation { 453 return search.Aggregation{ 454 Type: search.TermsAggregation, 455 Field: "tag", 456 Filters: a.Filters.SearchFilters(), 457 } 458 } 459 460 func convertSearchBuckets(buckets map[string]search.Bucket, aggregations Aggregations) map[string]search.Bucket { 461 for k, b := range buckets { 462 463 if aggregations[k].SearchAggregation().Field == "involved" { 464 involvedBucket := make(InvolvedBucket, 0) 465 for _, t := range b.(search.TermsBucket) { 466 involved := event.Involved{} 467 err := json.Unmarshal([]byte(t.Term), &involved) 468 if err == nil { 469 involvedBucket = append(involvedBucket, involved) 470 } 471 } 472 473 buckets[k] = involvedBucket 474 } 475 476 } 477 return buckets 478 } 479 480 type InvolvedBucket []event.Involved 481 482 func (i InvolvedBucket) BucketType() search.AggregationType { 483 return "involved" 484 }