Skip to content

Commit

Permalink
fix: add field type check
Browse files Browse the repository at this point in the history
  • Loading branch information
hengfeiyang committed Apr 8, 2022
1 parent 2dcceb2 commit 91eab75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 10 additions & 2 deletions pkg/core/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,21 @@ func (index *Index) buildField(mappings *meta.Mappings, bdoc *bluge.Document, ke
var field *bluge.TermField
switch mappings.Properties[key].Type {
case "text":
field = bluge.NewTextField(key, value.(string)).SearchTermPositions()
v, ok := value.(string)
if !ok {
return fmt.Errorf("field [%s] was set type to [text] but got a %T value", key, value)
}
field = bluge.NewTextField(key, v).SearchTermPositions()
fieldAnalyzer, _ := zincanalysis.QueryAnalyzerForField(index.CachedAnalyzers, index.CachedMappings, key)
if fieldAnalyzer != nil {
field.WithAnalyzer(fieldAnalyzer)
}
case "numeric":
field = bluge.NewNumericField(key, value.(float64))
v, ok := value.(float64)
if !ok {
return fmt.Errorf("field [%s] was set type to [numeric] but got a %T value", key, value)
}
field = bluge.NewNumericField(key, v)
case "keyword":
// compatible verion <= v0.1.4
if v, ok := value.(bool); ok {
Expand Down
4 changes: 1 addition & 3 deletions pkg/handlers/updatedocument.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/rs/zerolog/log"

"github.com/prabhatsharma/zinc/pkg/core"
)
Expand Down Expand Up @@ -40,7 +39,6 @@ func UpdateDocument(c *gin.Context) {
if !exists {
index, err = core.NewIndex(indexName, "disk", core.UseNewIndexMeta, nil) // Create a new index with disk storage as default
if err != nil {
log.Print(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
Expand All @@ -50,7 +48,7 @@ func UpdateDocument(c *gin.Context) {

err = index.UpdateDocument(docID, &doc, mintedID)
if err != nil {
c.JSON(http.StatusInternalServerError, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

Expand Down

0 comments on commit 91eab75

Please sign in to comment.