Skip to content

Commit 1de4cac

Browse files
committed
JSON field support
1 parent ad08301 commit 1de4cac

File tree

5 files changed

+83
-31
lines changed

5 files changed

+83
-31
lines changed

pkg/meta/collections.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@ type Collection struct {
1616

1717
// CollectionRelation describes a related collection
1818
type CollectionRelation struct {
19-
Type string `json:"type"`
20-
Collection *string `json:"collection,omitempty"`
21-
Fields map[string]*CollectionRelation `json:"fields"`
19+
Type string `yaml:"type"`
20+
Collection *string `yaml:"collection,omitempty"`
21+
Fields map[string]*CollectionRelation `yaml:"fields"`
22+
}
23+
24+
// CollectionSearchableConfig contains per field config of a collection
25+
type CollectionSearchableConfig struct {
26+
Type *string `yaml:"type,omitempty"`
27+
Analyzer *string `yaml:"analyzer,omitempty"`
2228
}
2329

2430
// CollectionDescription is the collection format for search filters
2531
type CollectionDescription struct {
26-
Searchable []string `yaml:"searchable"`
27-
Additional []string `yaml:"additional"`
28-
Contains []string `yaml:"contains,omitempty"`
29-
Relations map[string]*CollectionRelation `yaml:"relations,omitempty"`
32+
Searchable []string `yaml:"searchable"`
33+
SearchableConfig map[string]*CollectionSearchableConfig `yaml:"searchable_config,omitempty"`
34+
Additional []string `yaml:"additional"`
35+
Contains []string `yaml:"contains,omitempty"`
36+
Relations map[string]*CollectionRelation `yaml:"relations,omitempty"`
3037
}
3138

3239
// Collections is part of the meta model.

pkg/meta/filters.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99

1010
// Filter is part of the meta model.
1111
type Filter struct {
12-
Name string
13-
Items []string
14-
Additional []string
15-
Contains map[string]struct{}
16-
Relations map[string]*CollectionRelation
12+
Name string
13+
Items []string
14+
ItemsConfig map[string]*CollectionSearchableConfig
15+
Additional []string
16+
Contains map[string]struct{}
17+
Relations map[string]*CollectionRelation
1718
}
1819

1920
// FilterKey is part of the meta model.
@@ -65,11 +66,12 @@ func (fs *Filters) UnmarshalYAML(value *yaml.Node) error {
6566
}
6667

6768
*fs = append(*fs, Filter{
68-
Name: s.Name,
69-
Items: fsm[s].Searchable,
70-
Additional: fsm[s].Additional,
71-
Relations: relations,
72-
Contains: contains,
69+
Name: s.Name,
70+
Items: fsm[s].Searchable,
71+
ItemsConfig: fsm[s].SearchableConfig,
72+
Additional: fsm[s].Additional,
73+
Relations: relations,
74+
Contains: contains,
7375
})
7476
}
7577
return nil
@@ -101,11 +103,16 @@ func (fs Filters) Retain(verbose bool) func(string, string, *Member) bool {
101103
keep := map[key]struct{}{}
102104
additional := map[key]struct{}{}
103105
relations := map[key]*CollectionRelation{}
106+
config := map[key]*CollectionSearchableConfig{}
104107
for _, m := range fs {
105108
for _, f := range m.Items {
106109
keep[key{rel: m.Name, field: f}] = struct{}{}
107110
}
108111

112+
for f, data := range m.ItemsConfig {
113+
config[key{rel: m.Name, field: f}] = data
114+
}
115+
109116
for _, f := range m.Additional {
110117
additional[key{rel: m.Name, field: f}] = struct{}{}
111118
}
@@ -119,6 +126,14 @@ func (fs Filters) Retain(verbose bool) func(string, string, *Member) bool {
119126
m.Relation = relations[key{rel: rk, field: fk}]
120127
}
121128

129+
if c, ok := config[key{rel: rk, field: fk}]; ok {
130+
if c.Type != nil {
131+
m.Type = *c.Type
132+
}
133+
134+
m.Analyzer = c.Analyzer
135+
}
136+
122137
if _, ok := additional[key{rel: rk, field: fk}]; ok {
123138
m.Searchable = false
124139
return true

pkg/meta/member.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Member struct {
2424
RestrictionMode string `yaml:"restriction_mode"`
2525
Required bool `yaml:"required"`
2626
Searchable bool `yaml:"-"`
27+
Analyzer *string `yaml:"-"`
2728
Relation *CollectionRelation `yaml:"-"`
2829
Order int32 `yaml:"-"`
2930
}

pkg/search/textindex.go

+31-14
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,31 @@ func buildIndexMapping(collections meta.Collections) mapping.IndexMapping {
168168
docMapping.AddFieldMappingsAt("_bleve_type", keywordFieldMapping)
169169
for fname, cf := range col.Fields {
170170
if cf.Searchable {
171-
switch cf.Type {
172-
case "HTMLStrict", "HTMLPermissive":
173-
docMapping.AddFieldMappingsAt(fname, htmlFieldMapping)
174-
case "string", "text":
175-
docMapping.AddFieldMappingsAt(fname, textFieldMapping)
176-
docMapping.AddFieldMappingsAt("_"+fname+"_original", simpleFieldMapping)
177-
case "generic-relation":
178-
docMapping.AddFieldMappingsAt(fname, keywordFieldMapping)
179-
case "relation", "number":
180-
docMapping.AddFieldMappingsAt(fname, numberFieldMapping)
181-
case "number[]":
182-
docMapping.AddFieldMappingsAt(fname, numberFieldMapping)
183-
default:
184-
log.Errorf("unsupport type %q on field %s\n", cf.Type, fname)
171+
if cf.Analyzer == nil {
172+
switch cf.Type {
173+
case "HTMLStrict", "HTMLPermissive":
174+
docMapping.AddFieldMappingsAt(fname, htmlFieldMapping)
175+
case "string", "text":
176+
docMapping.AddFieldMappingsAt(fname, textFieldMapping)
177+
docMapping.AddFieldMappingsAt("_"+fname+"_original", simpleFieldMapping)
178+
case "generic-relation":
179+
docMapping.AddFieldMappingsAt(fname, keywordFieldMapping)
180+
case "relation", "number":
181+
docMapping.AddFieldMappingsAt(fname, numberFieldMapping)
182+
case "number[]":
183+
docMapping.AddFieldMappingsAt(fname, numberFieldMapping)
184+
default:
185+
log.Errorf("unsupport type %q on field %s\n", cf.Type, fname)
186+
}
187+
} else {
188+
switch *cf.Analyzer {
189+
case "html":
190+
docMapping.AddFieldMappingsAt(fname, htmlFieldMapping)
191+
case "simple":
192+
docMapping.AddFieldMappingsAt(fname, simpleFieldMapping)
193+
default:
194+
log.Errorf("unsupported Analyzer %q on field %s\n", *cf.Analyzer, fname)
195+
}
185196
}
186197
}
187198
}
@@ -220,6 +231,12 @@ func (bt bleveType) fill(fields map[string]*meta.Member, data []byte) {
220231
}
221232
}, fname)
222233
continue
234+
case "json-int-string-map":
235+
bt[fname] = []string{}
236+
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
237+
bt[fname] = append(bt[fname].([]string), string(value))
238+
return nil
239+
}, fname)
223240
default:
224241
if v, _, _, err := jsonparser.Get(data, fname); err == nil {
225242
bt[fname] = v

search.yml

+12
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ motion:
8484
- text
8585
- reason
8686
- meeting_id
87+
- amendment_paragraphs
88+
searchable_config:
89+
amendment_paragraphs:
90+
type: json-int-string-map
91+
analyzer: html
8792
additional:
8893
- id
8994
- sequential_number
@@ -219,6 +224,13 @@ user:
219224
- organization_management_level
220225
- meeting_ids
221226
- owner_id
227+
searchable_config:
228+
first_name:
229+
analyzer: simple
230+
last_name:
231+
analyzer: simple
232+
email:
233+
analyzer: simple
222234
additional:
223235
- id
224236
relations:

0 commit comments

Comments
 (0)