Skip to content

Commit

Permalink
bleve APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Dec 9, 2024
1 parent f3d0ac5 commit e3b1d5b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion document/field_synonym.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func processSynonymData(input []string, synonyms []string) map[string][]string {
// Map each term to the same list of synonyms.
synonymMap = make(map[string][]string, len(input))
for _, term := range input {
synonymMap[term] = append([]string(nil), synonyms...) // Avoid sharing slices.
synonymMap[term] = synonyms
}
} else {
synonymMap = make(map[string][]string, len(synonyms))
Expand Down
8 changes: 8 additions & 0 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,11 @@ func (sd *SynonymDefinition) Validate() error {
}
return nil
}

// SynonymIndex supports indexing synonym definitions alongside regular documents.
// Synonyms, grouped by collection name, define term relationships for query expansion in searches.
type SynonymIndex interface {
Index
// IndexSynonym indexes a synonym definition, with the specified id and belonging to the specified collection.
IndexSynonym(id string, collection string, definition *SynonymDefinition) error
}
19 changes: 19 additions & 0 deletions index_alias_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ func (i *indexAliasImpl) Index(id string, data interface{}) error {
return i.indexes[0].Index(id, data)
}

func (i *indexAliasImpl) IndexSynonym(id string, collection string, definition *SynonymDefinition) error {
i.mutex.RLock()
defer i.mutex.RUnlock()

if !i.open {
return ErrorIndexClosed
}

err := i.isAliasToSingleIndex()
if err != nil {
return err
}

if si, ok := i.indexes[0].(SynonymIndex); ok {
return si.IndexSynonym(id, collection, definition)
}
return ErrorSynonymSearchNotSupported
}

func (i *indexAliasImpl) Delete(id string) error {
i.mutex.RLock()
defer i.mutex.RUnlock()
Expand Down
34 changes: 34 additions & 0 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,40 @@ func (i *indexImpl) Index(id string, data interface{}) (err error) {
return
}

// IndexSynonym indexes a synonym definition, with the specified id and belonging to the specified collection.
// Synonym definition defines term relationships for query expansion in searches.
func (i *indexImpl) IndexSynonym(id string, collection string, definition *SynonymDefinition) error {
if id == "" {
return ErrorEmptyID
}

i.mutex.RLock()
defer i.mutex.RUnlock()

if !i.open {
return ErrorIndexClosed
}

i.FireIndexEvent()

synMap, ok := i.m.(mapping.SynonymMapping)
if !ok {
return ErrorSynonymSearchNotSupported
}

if err := definition.Validate(); err != nil {
return err
}

doc := document.NewSynonymDocument(id)
err := synMap.MapSynonymDocument(doc, collection, definition.Input, definition.Synonyms)
if err != nil {
return err
}
err = i.i.Update(doc)
return err
}

// IndexAdvanced takes a document.Document object
// skips the mapping and indexes it.
func (i *indexImpl) IndexAdvanced(doc *document.Document) (err error) {
Expand Down

0 comments on commit e3b1d5b

Please sign in to comment.