Skip to content

Instantly share code, notes, and snippets.

@osiloke
Created May 12, 2015 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osiloke/8464a89458c0d67ee61a to your computer and use it in GitHub Desktop.
Save osiloke/8464a89458c0d67ee61a to your computer and use it in GitHub Desktop.
JsonSchema to bleve mapping
func addSchemaMapper(schema *Schema) {
ix := schema.GetIndexer()
if ix == nil {
println("Schema does not have an indexer")
return
}
unindexedFieldMapping := bleve.NewTextFieldMapping()
unindexedFieldMapping.Index = false
unindexedFieldMapping.Store = false
// a generic reusable mapping for english text
englishTextFieldMapping := bleve.NewTextFieldMapping()
englishTextFieldMapping.Analyzer = "en"
englishTextFieldMapping.Store = false
keywordFieldMapping := bleve.NewTextFieldMapping()
keywordFieldMapping.Analyzer = "keyword"
keywordFieldMapping.Store = true
// emailFieldMapping := bleve.NewTextFieldMapping()
mapping := bleve.NewDocumentMapping()
indexed_fields := schema.GetIndexedFields()
indexed_fields_set := mapset.NewSetFromSlice(indexed_fields)
fields := schema.GetFields()
for k, f := range fields {
var field string
if f, ok := f.(map[string]interface{})["name"].(string); ok {
field = f
} else {
field = string(k)
}
if indexed_fields_set.Contains(field) {
switch schema.GetPropIndexType(field) {
case "email":
println("Add field mapping", schema.Name, field, "email")
mapping.AddFieldMappingsAt(field, keywordFieldMapping)
// case "string":
// println("Add field mapping", schema.Name, field)
// mapping.AddFieldMappingsAt(field, keywordFieldMapping)
default:
println("Add field mapping", schema.Name, field)
mapping.AddFieldMappingsAt(field, englishTextFieldMapping)
}
} else {
mapping.AddFieldMappingsAt(field, unindexedFieldMapping)
}
}
mapping.AddFieldMappingsAt("type", keywordFieldMapping)
ix.AddDocumentMapping(schema.Name, mapping)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment