fork of https://github.com/sourcegraph/zoekt
1package query
2
3type ExperimentalPhraseBoostOptions struct {
4 // The phrase needs to contain atleast this many terms. This is based on the
5 // parsed query.
6 //
7 // Defaults to 3.
8 MinTerms int
9
10 // Boost is how much to multiply the phrase match scores by.
11 //
12 // Defaults to 20.
13 Boost float64
14}
15
16// ExperimentalPhraseBoost transforms q into a query containing exact matches
17// to phrase boosted. opts control how and when the boosting is done.
18//
19// Note: This is a temporary API and will be removed in future commits.
20func ExpirementalPhraseBoost(q Q, phrase string, opts ExperimentalPhraseBoostOptions) Q {
21 if opts.MinTerms == 0 {
22 opts.MinTerms = 3
23 }
24 if opts.Boost == 0 {
25 opts.Boost = 20
26 }
27
28 contentAtoms := 0
29 caseSensitive := false
30 VisitAtoms(q, func(q Q) {
31 switch s := q.(type) {
32 case *Regexp:
33 // Check atom is for content
34 if s.Content || (s.Content == s.FileName) {
35 caseSensitive = s.CaseSensitive
36 contentAtoms++
37 }
38 case *Substring:
39 if s.Content || (s.Content == s.FileName) {
40 caseSensitive = s.CaseSensitive
41 contentAtoms++
42 }
43 }
44 })
45
46 if contentAtoms < opts.MinTerms {
47 return q
48 }
49
50 return NewOr(
51 &Boost{
52 Boost: opts.Boost,
53 Child: &Substring{
54 Pattern: phrase,
55 Content: true,
56 CaseSensitive: caseSensitive,
57 },
58 },
59 q,
60 )
61}