Skip to content

Commit

Permalink
Add FieldData for indexed fields, remove enum (#347)
Browse files Browse the repository at this point in the history
* add FieldData

* reorder imports

* make FieldData fields private

* move trigger_data to separate file

* small cleanup and comments
  • Loading branch information
kamaev authored and borovskyav committed Jul 29, 2019
1 parent 716eee2 commit 3bb49bc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 65 deletions.
3 changes: 2 additions & 1 deletion datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ type MetricEvent struct {

// SearchHighlight represents highlight
type SearchHighlight struct {
Field, Value string
Field string
Value string
}

// SearchResult represents fulltext search result
Expand Down
18 changes: 9 additions & 9 deletions index/bleve/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ func buildSearchQuery(filterTags, searchTerms []string, onlyErrors bool) query.Q
func buildQueryForTags(filterTags []string) (searchQueries []query.Query) {
for _, tag := range filterTags {
qr := bleve.NewTermQuery(tag)
qr.FieldVal = mapping.TriggerTags.String()
qr.FieldVal = mapping.TriggerTags.GetName()
searchQueries = append(searchQueries, qr)
}
return
}

func buildQueryForTerms(searchTerms []string) (searchQueries []query.Query) {
for _, term := range searchTerms {
qr := bleve.NewFuzzyQuery(term)
qr.SetField(mapping.TriggerName.String())
qr.SetBoost(3)
qr1 := bleve.NewFuzzyQuery(term)
qr1.SetField(mapping.TriggerDesc.String())
qr1.SetBoost(1)
searchQueries = append(searchQueries, bleve.NewDisjunctionQuery(qr, qr1))
nameQuery, nameField := bleve.NewFuzzyQuery(term), mapping.TriggerName
nameQuery.SetField(nameField.GetName())
nameQuery.SetBoost(nameField.GetPriority())
descQuery, descField := bleve.NewFuzzyQuery(term), mapping.TriggerDesc
descQuery.SetField(descField.GetName())
descQuery.SetBoost(descField.GetPriority())
searchQueries = append(searchQueries, bleve.NewDisjunctionQuery(nameQuery, descQuery))
}
return
}
Expand All @@ -48,6 +48,6 @@ func buildQueryForOnlyErrors(onlyErrors bool) (searchQueries []query.Query) {
}
minScore := float64(1)
qr := bleve.NewNumericRangeQuery(&minScore, nil)
qr.FieldVal = mapping.TriggerLastCheckScore.String()
qr.FieldVal = mapping.TriggerLastCheckScore.GetName()
return append(searchQueries, qr)
}
6 changes: 3 additions & 3 deletions index/bleve/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func (index *TriggerIndex) Search(filterTags []string, searchString string, only
return
}

func getHighlights(fragmentsMap search.FieldFragmentMap, triggerFields ...mapping.TriggerField) []moira.SearchHighlight {
func getHighlights(fragmentsMap search.FieldFragmentMap, triggerFields ...mapping.FieldData) []moira.SearchHighlight {
highlights := make([]moira.SearchHighlight, 0)
for _, triggerField := range triggerFields {
var highlightValue string
if fragments, ok := fragmentsMap[triggerField.String()]; ok {
if fragments, ok := fragmentsMap[triggerField.GetName()]; ok {
for _, fragment := range fragments {
highlightValue += fragment
}
Expand All @@ -73,7 +73,7 @@ func buildSearchRequest(filterTags []string, searchString string, onlyErrors boo
// TriggerCheck.Score (desc)
// Relevance (asc)
// Trigger.Name (asc)
req.SortBy([]string{fmt.Sprintf("-%s", mapping.TriggerLastCheckScore.String()), "-_score", mapping.TriggerName.String()})
req.SortBy([]string{fmt.Sprintf("-%s", mapping.TriggerLastCheckScore.GetName()), "-_score", mapping.TriggerName.GetName()})
req.Highlight = bleve.NewHighlight()

return req
Expand Down
26 changes: 26 additions & 0 deletions index/mapping/field_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mapping

// FieldData is container for field-related parameters
// name represents indexed object field name
// nameTag represents highlight field name for given field in search result, if value is empty then the highlight for this field is not used
// priority represents sort priority for given field
type FieldData struct {
name string
nameTag string
priority float64
}

// GetName returns TriggerField name.
func (field FieldData) GetName() string {
return field.name
}

// GetTagValue returns TriggerField value used in marshalling.
func (field FieldData) GetTagValue() string {
return field.nameTag
}

// GetPriority returns field priority
func (field FieldData) GetPriority() float64 {
return field.priority
}
55 changes: 14 additions & 41 deletions index/mapping/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@ import (
"github.com/moira-alert/moira"
)

// TriggerField is used as enum
type TriggerField int

// Constants used as enum
const (
TriggerID TriggerField = iota
TriggerName
TriggerDesc
TriggerTags
TriggerLastCheckScore
)

var (
triggerFieldNames = []string{
"ID",
"Name",
"Desc",
"Tags",
"LastCheckScore",
}
triggerFieldTagValues = []string{
"id",
"name",
"desc",
"tags",
"",
}
// TriggerID represents field data for moira.Trigger.ID
TriggerID = FieldData{"ID", "id", 5}
// TriggerName represents field data for moira.Trigger.Name
TriggerName = FieldData{"Name", "name", 3}
// TriggerDesc represents field data for moira.Trigger.Desc
TriggerDesc = FieldData{"Desc", "desc", 1}
// TriggerTags represents field data for moira.Trigger.Tags
TriggerTags = FieldData{"Tags", "tags", 0}
// TriggerLastCheckScore represents field data for moira.CheckData score
TriggerLastCheckScore = FieldData{"LastCheckScore", "", 0}
)

// Trigger represents Moira.Trigger type for full-text search index. It includes only indexed fields
Expand All @@ -49,25 +33,14 @@ func (Trigger) Type() string {
return "moira.indexed.trigger"
}

// String returns TriggerField name. It works like enum
func (field TriggerField) String() string {
return triggerFieldNames[field]
}

// GetTagValue returns TriggerField value used in marshalling. It works like enum
func (field TriggerField) GetTagValue() string {
return triggerFieldTagValues[field]
}

// GetDocumentMapping returns Bleve.mapping.DocumentMapping for Trigger type
func (Trigger) GetDocumentMapping() *mapping.DocumentMapping {

triggerMapping := bleve.NewDocumentStaticMapping()

triggerMapping.AddFieldMappingsAt(TriggerName.String(), getStandardMapping())
triggerMapping.AddFieldMappingsAt(TriggerTags.String(), getKeywordMapping())
triggerMapping.AddFieldMappingsAt(TriggerDesc.String(), getStandardMapping())
triggerMapping.AddFieldMappingsAt(TriggerLastCheckScore.String(), getNumericMapping())
triggerMapping.AddFieldMappingsAt(TriggerName.GetName(), getStandardMapping())
triggerMapping.AddFieldMappingsAt(TriggerTags.GetName(), getKeywordMapping())
triggerMapping.AddFieldMappingsAt(TriggerDesc.GetName(), getStandardMapping())
triggerMapping.AddFieldMappingsAt(TriggerLastCheckScore.GetName(), getNumericMapping())

return triggerMapping

Expand Down
38 changes: 27 additions & 11 deletions index/mapping/trigger_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package mapping

import (
"github.com/moira-alert/moira"
. "github.com/smartystreets/goconvey/convey"
"log"
"reflect"
"strings"
"testing"

"github.com/moira-alert/moira"
. "github.com/smartystreets/goconvey/convey"
)

var testTriggerFields = []FieldData{
TriggerID,
TriggerName,
TriggerDesc,
TriggerTags,
TriggerLastCheckScore,
}

func TestTriggerField_GetPriority(t *testing.T) {
expected := []float64{5, 3, 1, 0, 0}
actual := make([]float64, 0, len(testTriggerFields))
Convey("Test GetPriority returns correct field priority", t, func() {
for _, triggerField := range testTriggerFields {
fieldName, fieldPriority := triggerField.GetName(), triggerField.GetPriority()
log.Printf("field: %s priority: %f", fieldName, fieldPriority)
actual = append(actual, triggerField.GetPriority())
}
So(actual, ShouldResemble, expected)
})
}

func TestTriggerField_GetTagValue(t *testing.T) {
// This test is necessary to make sure that
// SearchResult will contain highlights for actual moira.Trigger structure
triggerFields := []TriggerField{
TriggerID,
TriggerName,
TriggerDesc,
TriggerTags,
TriggerLastCheckScore,
}
Convey("Test GetTagValue returns correct JSON tag", t, func() {
for _, triggerField := range triggerFields {
actual := getTagByFieldName(triggerField.String())
for _, triggerField := range testTriggerFields {
actual := getTagByFieldName(triggerField.GetName())
expected := triggerField.GetTagValue()
So(actual, ShouldEqual, expected)
}
Expand Down

0 comments on commit 3bb49bc

Please sign in to comment.