/
searchQuery.go
792 lines (741 loc) · 24.7 KB
/
searchQuery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package api
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/ourrootsorg/cms-server/model"
"github.com/ourrootsorg/cms-server/stdtext"
"github.com/ourrootsorg/cms-server/utils"
)
// SearchByIDRequest contains the possible search by id request parameters
type SearchByIDRequest struct {
SurnameFirst bool `schema:"surnameFirst"`
}
// SearchRequest contains the possible search request parameters
type SearchRequest struct {
SocietyID uint32 `schema:"societyId"`
// name
Given string `schema:"given"`
GivenFuzziness int `schema:"givenFuzziness"`
Surname string `schema:"surname"`
SurnameFuzziness int `schema:"surnameFuzziness"`
// relatives
FatherGiven string `schema:"fatherGiven"`
FatherGivenFuzziness int `schema:"fatherGivenFuzziness"`
FatherSurname string `schema:"fatherSurname"`
FatherSurnameFuzziness int `schema:"fatherSurnameFuzziness"`
MotherGiven string `schema:"motherGiven"`
MotherGivenFuzziness int `schema:"motherGivenFuzziness"`
MotherSurname string `schema:"motherSurname"`
MotherSurnameFuzziness int `schema:"motherSurnameFuzziness"`
SpouseGiven string `schema:"spouseGiven"`
SpouseGivenFuzziness int `schema:"spouseGivenFuzziness"`
SpouseSurname string `schema:"spouseSurname"`
SpouseSurnameFuzziness int `schema:"spouseSurnameFuzziness"`
OtherGiven string `schema:"otherGiven"`
OtherGivenFuzziness int `schema:"otherGivenFuzziness"`
OtherSurname string `schema:"otherSurname"`
OtherSurnameFuzziness int `schema:"otherSurnameFuzziness"`
// events
BirthDate string `schema:"birthDate"`
BirthDateFuzziness int `schema:"birthDateFuzziness"`
BirthPlace string `schema:"birthPlace"`
BirthPlaceFuzziness int `schema:"birthPlaceFuzziness"`
MarriageDate string `schema:"marriageDate"`
MarriageDateFuzziness int `schema:"marriageDateFuzziness"`
MarriagePlace string `schema:"marriagePlace"`
MarriagePlaceFuzziness int `schema:"marriagePlaceFuzziness"`
ResidenceDate string `schema:"residenceDate"`
ResidenceDateFuzziness int `schema:"residenceDateFuzziness"`
ResidencePlace string `schema:"residencePlace"`
ResidencePlaceFuzziness int `schema:"residencePlaceFuzziness"`
DeathDate string `schema:"deathDate"`
DeathDateFuzziness int `schema:"deathDateFuzziness"`
DeathPlace string `schema:"deathPlace"`
DeathPlaceFuzziness int `schema:"deathPlaceFuzziness"`
AnyDate string `schema:"anyDate"` // match on any date
AnyDateFuzziness int `schema:"anyDateFuzziness"`
AnyPlace string `schema:"anyPlace"` // match on any place
AnyPlaceFuzziness int `schema:"anyPlaceFuzziness"`
// other
Keywords string `schema:"keywords"`
Title string `schema:"title"`
Author string `schema:"author"`
// facets and filters
CollectionPlace1Facet bool `schema:"collectionPlace1Facet"`
CollectionPlace1 string `schema:"collectionPlace1"`
CollectionPlace2Facet bool `schema:"collectionPlace2Facet"`
CollectionPlace2 string `schema:"collectionPlace2"`
CollectionPlace3Facet bool `schema:"collectionPlace3Facet"`
CollectionPlace3 string `schema:"collectionPlace3"`
CategoryFacet bool `schema:"categoryFacet"`
Category string `schema:"category"`
CollectionFacet bool `schema:"collectionFacet"`
Collection string `schema:"collection"`
// from and size and surname first (vs last)
From int `schema:"from"`
Size int `schema:"size"`
SurnameFirst bool `schema:"surnameFirst"`
}
// int
type Search struct {
Query Query `json:"query,omitempty"`
Aggs map[string]Agg `json:"aggs,omitempty"`
Source []string `json:"_source,omitempty"`
From int `json:"from,omitempty"`
Size int `json:"size"`
}
type Query struct {
Bool *BoolQuery `json:"bool,omitempty"`
DisMax *DisMaxQuery `json:"dis_max,omitempty"`
Fuzzy map[string]FuzzyQuery `json:"fuzzy,omitempty"`
Match map[string]MatchQuery `json:"match,omitempty"`
Range map[string]RangeQuery `json:"range,omitempty"`
Term map[string]TermQuery `json:"term,omitempty"`
Wildcard map[string]TermQuery `json:"wildcard,omitempty"`
}
type BoolQuery struct {
Must []Query `json:"must,omitempty"`
Should []Query `json:"should,omitempty"`
Filter []Query `json:"filter,omitempty"`
}
type DisMaxQuery struct {
Queries []Query `json:"queries,omitempty"`
}
type FuzzyQuery struct {
Value string `json:"value"`
Fuzziness string `json:"fuzziness,omitempty"`
Rewrite string `json:"rewrite,omitempty"`
Boost float32 `json:"boost,omitempty"`
}
type MatchQuery struct {
Query string `json:"query"`
Operator string `json:"operator,omitempty"`
Boost float32 `json:"boost,omitempty"`
}
type RangeQuery struct {
GTE int `json:"gte,omitempty"`
LTE int `json:"lte,omitempty"`
Boost float32 `json:"boost,omitempty"`
}
type TermQuery struct {
Value interface{} `json:"value"`
Boost float32 `json:"boost,omitempty"`
}
type IntegerTermQuery struct {
Value uint32 `json:"value"`
Boost float32 `json:"boost,omitempty"`
}
type Agg struct {
Terms *TermsAgg `json:"terms,omitempty"`
Range *RangeAgg `json:"range,omitempty"`
}
type TermsAgg struct {
Field string `json:"field"`
Size int `json:"size,omitempty"`
}
type RangeAgg struct {
Field string `json:"field"`
Keyed bool `json:"keyed,omitempty"`
Ranges []RangeAggRange `json:"ranges"`
}
type RangeAggRange struct {
Key string `json:"key"`
From int `json:"from,omitempty'"`
To int `json:"to,omitempty'"`
}
func (api API) constructSearchQuery(ctx context.Context, req *SearchRequest) (*Search, error) {
var mustQueries []Query
var shouldQueries []Query
var filterQueries []Query
aggs := map[string]Agg{}
societyID, err := utils.GetSocietyIDFromContext(ctx)
if err != nil {
return nil, err
}
userID, err := utils.GetSearchUserIDFromContext(ctx)
if err != nil {
return nil, err
}
if societyID != req.SocietyID {
return nil, fmt.Errorf("jwt societyId %d does not match query societyId %d", societyID, req.SocietyID)
}
// name
shouldGivenQueries, mustGivenQueries, err := api.constructNameQueries(ctx, "given", req.Given, req.GivenFuzziness, model.GivenType)
if err != nil {
return nil, err
}
shouldSurnameQueries, mustSurnameQueries, err := api.constructNameQueries(ctx, "surname", req.Surname, req.SurnameFuzziness, model.SurnameType)
if err != nil {
return nil, err
}
if len(shouldGivenQueries) > 0 || len(shouldSurnameQueries) > 0 || len(mustGivenQueries) > 0 || len(mustSurnameQueries) > 0 {
mustQueries = append(mustQueries, Query{
Bool: &BoolQuery{
Must: append(mustGivenQueries, mustSurnameQueries...),
Should: append(shouldGivenQueries, shouldSurnameQueries...),
},
})
}
// relative names
shouldSubqueries, mustSubqueries, err := api.constructNameQueries(ctx, "fatherGiven", req.FatherGiven, req.FatherGivenFuzziness, model.GivenType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "fatherSurname", req.FatherSurname, req.FatherSurnameFuzziness, model.SurnameType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "motherGiven", req.MotherGiven, req.MotherGivenFuzziness, model.GivenType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "motherSurname", req.MotherSurname, req.MotherSurnameFuzziness, model.SurnameType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "spouseGiven", req.SpouseGiven, req.SpouseGivenFuzziness, model.GivenType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "spouseSurname", req.SpouseSurname, req.SpouseSurnameFuzziness, model.SurnameType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "otherGiven", req.OtherGiven, req.OtherGivenFuzziness, model.GivenType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries, err = api.constructNameQueries(ctx, "otherSurname", req.OtherSurname, req.OtherSurnameFuzziness, model.SurnameType)
if err != nil {
return nil, err
}
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
// events
shouldSubqueries, mustSubqueries = constructDateQueries("birthYear", "birthDateStd", req.BirthDate, req.BirthDateFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("birthPlace", req.BirthPlace, req.BirthPlaceFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("marriageYear", "marriageDateStd", req.MarriageDate, req.MarriageDateFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("marriagePlace", req.MarriagePlace, req.MarriagePlaceFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("residenceYear", "residenceDateStd", req.ResidenceDate, req.ResidenceDateFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("residencePlace", req.ResidencePlace, req.ResidencePlaceFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("deathYear", "deathDateStd", req.DeathDate, req.DeathDateFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("deathPlace", req.DeathPlace, req.DeathPlaceFuzziness)
shouldQueries = append(shouldQueries, shouldSubqueries...)
mustQueries = append(mustQueries, mustSubqueries...)
// any date
if len(req.AnyDate) > 0 {
var anyShouldQueries []Query
var anyMustQueries []Query
shouldSubqueries, mustSubqueries = constructDateQueries("birthYear", "birthDateStd", req.AnyDate, req.AnyDateFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("marriageYear", "marriageDateStd", req.AnyDate, req.AnyDateFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("residenceYear", "residenceDateStd", req.AnyDate, req.AnyDateFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("deathYear", "deathDateStd", req.AnyDate, req.AnyDateFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructDateQueries("otherYear", "otherDateStd", req.AnyDate, req.AnyDateFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
if len(anyShouldQueries) > 0 {
shouldQueries = append(shouldQueries, Query{
DisMax: &DisMaxQuery{
Queries: anyShouldQueries,
},
})
}
if len(anyMustQueries) > 0 {
mustQueries = append(mustQueries, Query{
DisMax: &DisMaxQuery{
Queries: anyMustQueries,
},
})
}
}
// any place
if len(req.AnyPlace) > 0 {
var anyShouldQueries []Query
var anyMustQueries []Query
shouldSubqueries, mustSubqueries = constructPlaceQueries("birthPlace", req.AnyPlace, req.AnyPlaceFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("marriagePlace", req.AnyPlace, req.AnyPlaceFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("residencePlace", req.AnyPlace, req.AnyPlaceFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("deathPlace", req.AnyPlace, req.AnyPlaceFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
shouldSubqueries, mustSubqueries = constructPlaceQueries("otherPlace", req.AnyPlace, req.AnyPlaceFuzziness)
anyShouldQueries = append(anyShouldQueries, shouldSubqueries...)
anyMustQueries = append(anyMustQueries, mustSubqueries...)
if len(anyShouldQueries) > 0 {
shouldQueries = append(shouldQueries, Query{
DisMax: &DisMaxQuery{
Queries: anyShouldQueries,
},
})
}
if len(anyMustQueries) > 0 {
mustQueries = append(mustQueries, Query{
DisMax: &DisMaxQuery{
Queries: anyMustQueries,
},
})
}
}
// other
mustQueries = append(mustQueries, constructTextQueries("keywords", req.Keywords)...)
mustQueries = append(mustQueries, constructTextQueries("book_title", req.Title)...)
mustQueries = append(mustQueries, constructTextQueries("book_author", req.Author)...)
// filters
filterQueries = append(filterQueries, constructFilterQueries("societyId", float64(societyID))...) // convert to float64 so tests pass
if userID == 0 { // not signed in
filterQueries = append(filterQueries, constructFilterQueries("privacy", Public)...)
}
filterQueries = append(filterQueries, constructFilterQueries("category", req.Category)...)
filterQueries = append(filterQueries, constructFilterQueries("collection", req.Collection)...)
filterQueries = append(filterQueries, constructFilterQueries("collectionPlace1", req.CollectionPlace1)...)
filterQueries = append(filterQueries, constructFilterQueries("collectionPlace2", req.CollectionPlace2)...)
filterQueries = append(filterQueries, constructFilterQueries("collectionPlace3", req.CollectionPlace3)...)
// facets
addTermsAgg(aggs, "category", req.CategoryFacet)
addTermsAgg(aggs, "collection", len(req.Category) > 0 && req.CollectionFacet)
addTermsAgg(aggs, "collectionPlace1", req.CollectionPlace1Facet)
addTermsAgg(aggs, "collectionPlace2", len(req.CollectionPlace1) > 0 && req.CollectionPlace2Facet)
addTermsAgg(aggs, "collectionPlace3", len(req.CollectionPlace1) > 0 && len(req.CollectionPlace2) > 0 && req.CollectionPlace3Facet)
if len(aggs) == 0 {
aggs = nil
}
from := req.From
if from > MaxFrom {
from = MaxFrom
}
size := req.Size
if size > MaxSize {
size = MaxSize
} else if size < 0 {
size = DefaultSize
}
return &Search{
Query: Query{
Bool: &BoolQuery{
Must: mustQueries,
Should: shouldQueries,
Filter: filterQueries,
},
},
Aggs: aggs,
From: from,
Size: size,
}, nil
}
// TODO learn the best boost values
const exactNameBoost = 1.0
const variantNameBoost = 0.7
const narrowNameBoost = 0.6
const wildcardNameBoost = 0.5
const broadNameBoost = 0.4
const fuzzyNameBoost = 0.3
const initialNameBoost = 0.2
func (api API) constructNameQueries(ctx context.Context, label, value string, fuzziness int, nameType model.NameType) ([]Query, []Query, error) {
if len(value) == 0 {
return nil, nil, nil
}
var queries []Query
for _, v := range splitWord(value) {
if strings.ContainsAny(v, "*?") {
v := stdtext.AsciiFold(strings.ToLower(v))
// TODO disallow wildcards within the first 3 characters?
if strings.HasPrefix(v, "*") || strings.HasPrefix(v, "?") {
continue
}
queries = append(queries, Query{
Wildcard: map[string]TermQuery{
label: {
Value: v,
Boost: wildcardNameBoost,
},
},
})
continue
}
exactQuery := Query{
Match: map[string]MatchQuery{
label: {
Query: v,
Boost: exactNameBoost,
},
},
}
if fuzziness == FuzzyNameExact {
queries = append(queries, exactQuery)
continue
}
subqueries := []Query{exactQuery}
if fuzziness == FuzzyNameDefault || fuzziness&FuzzyNameVariants > 0 {
nameVariants, err := api.GetNameVariants(ctx, nameType, stdtext.AsciiFold(strings.ToLower(v)))
if err != nil {
if !model.ErrNotFound.Matches(err) {
return nil, nil, err
}
nameVariants = &model.NameVariants{}
}
for _, variant := range nameVariants.Variants {
subqueries = append(subqueries, Query{
Match: map[string]MatchQuery{
label: {
Query: variant,
Boost: variantNameBoost,
},
},
})
}
}
// TODO choose the best coders for broad and narrow
if fuzziness == FuzzyNameDefault || fuzziness&FuzzyNameSoundsLikeNarrow > 0 {
subqueries = append(subqueries, Query{
Match: map[string]MatchQuery{
label + ".narrow": {
Query: v,
Boost: narrowNameBoost,
},
},
})
}
if fuzziness == FuzzyNameDefault || fuzziness&FuzzyNameSoundsLikeBroad > 0 {
subqueries = append(subqueries, Query{
Match: map[string]MatchQuery{
label + ".broad": {
Query: v,
Boost: broadNameBoost,
},
},
})
}
if fuzziness == FuzzyNameDefault || fuzziness&FuzzyNameLevenshtein > 0 {
std := stdtext.AsciiFold(strings.ToLower(v))
subqueries = append(subqueries, Query{
Fuzzy: map[string]FuzzyQuery{
label: {
Value: std,
Fuzziness: "AUTO",
Rewrite: "constant_score_boolean",
Boost: fuzzyNameBoost,
},
},
})
}
if (fuzziness == FuzzyNameDefault || fuzziness&FuzzyNameInitials > 0) && nameType == model.GivenType {
subqueries = append(subqueries, Query{
Match: map[string]MatchQuery{
label: {
Query: v[0:1],
Boost: initialNameBoost,
},
},
})
}
queries = append(queries, Query{
DisMax: &DisMaxQuery{
Queries: subqueries,
},
})
}
if fuzziness == FuzzyNameDefault {
return queries, nil, nil
} else {
return nil, queries, nil
}
}
const exactYearBoost = 0.7
const rangeYearBoost = 0.3
func constructDateQueries(yearLabel, dateLabel, value string, fuzziness int) ([]Query, []Query) {
if len(value) != 4 {
return nil, nil
}
year, err := strconv.Atoi(value)
if err != nil {
return nil, nil
}
query := Query{
Term: map[string]TermQuery{
yearLabel: {
Value: value,
Boost: exactYearBoost,
},
},
}
if fuzziness == FuzzyDateDefault || fuzziness > FuzzyDateExact {
var yrRange int
switch fuzziness {
case FuzzyDateDefault:
yrRange = 5
case FuzzyDateOne:
yrRange = 1
case FuzzyDateTwo:
yrRange = 2
case FuzzyDateFive:
yrRange = 5
case FuzzyDateTen:
yrRange = 10
}
query = Query{
DisMax: &DisMaxQuery{
Queries: []Query{query, {
Range: map[string]RangeQuery{
yearLabel: {
GTE: year - yrRange,
LTE: year + yrRange,
Boost: rangeYearBoost,
},
},
}},
},
}
}
if fuzziness == FuzzyDateDefault {
return []Query{query}, nil
} else {
return nil, []Query{query}
}
}
const exactPlaceBoost = 1.0
const wildcardPlaceBoost = 0.7
const fuzzyPlaceBoost = 0.2
const levelPlaceBoost = 0.2
func constructPlaceQueries(label, value string, fuzziness int) ([]Query, []Query) {
if len(value) == 0 {
return nil, nil
}
// support wildcards within words or ~word, which means to fuzzy-match word
if strings.ContainsAny(value, "~*?") {
var queries []Query
for _, v := range splitWord(value) {
v := stdtext.AsciiFold(strings.ToLower(v))
if strings.HasPrefix(v, "~") && !strings.ContainsAny(v, "*?") {
queries = append(queries, Query{
Fuzzy: map[string]FuzzyQuery{
label: {
Value: v[1:],
Fuzziness: "AUTO",
Rewrite: "constant_score_boolean",
Boost: fuzzyPlaceBoost,
},
},
})
continue
}
v = strings.ReplaceAll(v, "~", "")
if strings.ContainsAny(v, "*?") {
// TODO disallow wildcards within the first 3 characters?
if strings.HasPrefix(value, "*") || strings.HasPrefix(value, "?") {
continue
}
queries = append(queries, Query{
Wildcard: map[string]TermQuery{
label: {
Value: v,
Boost: wildcardPlaceBoost,
},
},
})
continue
}
queries = append(queries, Query{
Term: map[string]TermQuery{
label: {
Value: v,
Boost: exactPlaceBoost,
},
},
})
}
if fuzziness == FuzzyPlaceDefault {
return queries, nil
} else {
return nil, queries
}
}
levels := splitPlace(value)
reverse(levels)
// limit to 4 levels
if len(levels) > 4 {
levels = levels[0:4]
}
queries := []Query{
{
Term: map[string]TermQuery{
fmt.Sprintf("%s%d", label, len(levels)): {
Value: strings.Join(levels, ","),
Boost: exactPlaceBoost,
},
},
},
{
Term: map[string]TermQuery{
fmt.Sprintf("%s%d", label, len(levels)): {
Value: strings.Join(levels, ",") + ",",
Boost: exactPlaceBoost,
},
},
},
}
if fuzziness == FuzzyPlaceDefault || fuzziness&FuzzyPlaceHigherJurisdictions > 0 {
for i := 1; i < len(levels); i++ {
// don't match on just "United States"
if i == 1 && levels[0] == "United States" {
continue
}
queries = append(queries, Query{
Term: map[string]TermQuery{
fmt.Sprintf("%s%d", label, i): {
Value: strings.Join(levels[0:i], ","),
Boost: float32(i) * levelPlaceBoost,
},
},
})
}
}
// TODO include nearby places (lat and lon)
if fuzziness == FuzzyPlaceDefault || fuzziness&FuzzyPlaceNearby > 0 {
}
if len(queries) > 1 {
queries = []Query{
{
DisMax: &DisMaxQuery{
Queries: queries,
},
},
}
}
if fuzziness == FuzzyPlaceDefault {
return queries, nil
} else {
return nil, queries
}
}
func constructTextQueries(label, value string) []Query {
if len(value) == 0 {
return nil
}
if !strings.ContainsAny(value, "~*?") {
return []Query{
{
Match: map[string]MatchQuery{
label: {
Query: value,
Operator: "AND",
},
},
},
}
}
// support wildcards within words or ~word, which means to fuzzy-match word
var queries []Query
for _, v := range splitWord(value) {
v := stdtext.AsciiFold(strings.ToLower(v))
if strings.HasPrefix(v, "~") && !strings.ContainsAny(v, "*?") {
queries = append(queries, Query{
Fuzzy: map[string]FuzzyQuery{
label: {
Value: v[1:],
Fuzziness: "AUTO",
Rewrite: "constant_score_boolean",
},
},
})
continue
}
v = strings.ReplaceAll(v, "~", "")
if strings.ContainsAny(v, "*?") {
queries = append(queries, Query{
Wildcard: map[string]TermQuery{
label: {
Value: v,
},
},
})
continue
}
queries = append(queries, Query{
Term: map[string]TermQuery{
label: {
Value: v,
},
},
})
}
return queries
}
func constructFilterQueries(label string, value interface{}) []Query {
strValue, ok := value.(string)
if ok && len(strValue) == 0 {
return nil
}
return []Query{
{
Term: map[string]TermQuery{
label: {
Value: value,
},
},
},
}
}
func addTermsAgg(aggs map[string]Agg, label string, cond bool) {
if cond {
aggs[label] = Agg{
Terms: &TermsAgg{
Field: label,
Size: 250,
},
}
}
}
var wordRegexp = regexp.MustCompile("[^\\pL\\d*?~]+") // keep ~*? for fuzzy and wildcards
func splitWord(name string) []string {
return wordRegexp.Split(name, -1)
}
func reverse(s []string) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}