-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer_param.go
More file actions
93 lines (85 loc) · 2.39 KB
/
analyzer_param.go
File metadata and controls
93 lines (85 loc) · 2.39 KB
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
package picker
import (
"encoding/json"
"github.com/chanced/dynamic"
)
// WithAnalyzer is a Field mapping with an analyzer
//
// Analyzer
//
// The analyzer parameter specifies the analyzer used for text analysis when
// indexing or searching a text field.
//
// Only text fields support the analyzer mapping parameter.
//
// Search Quote Analyzer
//
// The search_quote_analyzer setting allows you to specify an analyzer for
// phrases, this is particularly useful when dealing with disabling stop words
// for phrase queries.
//
// To disable stop words for phrases a field utilising three analyzer settings
// will be required:
//
// 1. An analyzer setting for indexing all terms including stop words
//
// 2. A search_analyzer setting for non-phrase queries that will remove stop
// words
//
// 3. A search_quote_analyzer setting for phrase queries that will not remove
// stop words
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html
type WithAnalyzer interface {
// Analyzer parameter specifies the analyzer used for text analysis when
// indexing or searching a text field.
Analyzer() string
// SetAnalyzer sets Analyzer to v
SetAnalyzer(v string)
}
// analyzerParam adds Analyzer, SearchAnalyzer, and SearchQuoteAnalyzer
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html
type analyzerParam struct {
analyzer string
}
// Analyzer parameter specifies the analyzer used for text analysis when
// indexing or searching a text field.
//
// Unless overridden with the search_analyzer mapping parameter, this
// analyzer is used for both index and search analysis.
func (ap analyzerParam) Analyzer() string {
return ap.analyzer
}
// SetAnalyzer sets Analyzer to v
func (ap *analyzerParam) SetAnalyzer(v string) {
if ap.Analyzer() != v {
ap.analyzer = v
}
}
func marshalAnalyzerParam(source interface{}) (dynamic.JSON, error) {
if a, ok := source.(WithAnalyzer); ok {
if len(a.Analyzer()) > 0 {
return json.Marshal(a.Analyzer())
}
}
return nil, nil
}
func unmarshalAnalyzerParam(data dynamic.JSON, target interface{}) error {
if a, ok := target.(WithAnalyzer); ok {
if data.IsNull() {
return nil
}
if data.IsString() {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
a.SetAnalyzer(str)
return nil
}
return &json.UnmarshalTypeError{Value: string(data), Type: typeString}
}
return nil
}