-
Notifications
You must be signed in to change notification settings - Fork 697
Open
Description
If you tests the code from https://github.com/blevesearch/bleve/blob/master/docs/synonyms.md you get the expected result:
package main
import (
"fmt"
"github.com/blevesearch/bleve/v2"
_ "github.com/blevesearch/bleve/v2/analysis/lang/en"
"testing"
)
func TestIndex(t *testing.T) {
// Define a document to be indexed.
doc := struct {
Text string `json:"text"`
}{
Text: "hardworking employee",
}
// Define a synonym definition where "hardworking" has equivalent terms.
synDef := &bleve.SynonymDefinition{
Synonyms: []string{
"hardworking",
"industrious",
"conscientious",
"persistent",
},
}
// Define the name of the `synonym collection`.
// This collection groups multiple synonym definitions.
synonymCollection := "collection1"
// Define the name of the `synonym source`.
// This source will be associated with specific field mappings.
synonymSourceName := "english"
// Define the analyzer to process terms in the synonym definitions.
// This analyzer must match the one applied to the field using the synonym source.
analyzer := "en"
// Configure the synonym source by associating it with the synonym collection and analyzer.
synonymSourceConfig := map[string]interface{}{
"collection": synonymCollection,
"analyzer": analyzer,
}
// Create a new index mapping.
bleveMapping := bleve.NewIndexMapping()
// Add the synonym source configuration to the index mapping.
err := bleveMapping.AddSynonymSource(synonymSourceName, synonymSourceConfig)
if err != nil {
panic(err)
}
// Create a text field mapping with the specified analyzer and synonym source.
textFieldMapping := bleve.NewTextFieldMapping()
textFieldMapping.Analyzer = analyzer
textFieldMapping.SynonymSource = synonymSourceName
// Associate the text field mapping with the "text" field in the default document mapping.
bleveMapping.DefaultMapping.AddFieldMappingsAt("text", textFieldMapping)
// Create a new index with the specified mapping.
index, err := bleve.New("example.bleve", bleveMapping)
if err != nil {
panic(err)
}
// Index the document into the created index.
err = index.Index("doc1", doc)
if err != nil {
panic(err)
}
// Check if the index supports synonym indexing and add the synonym definition.
if synIndex, ok := index.(bleve.SynonymIndex); ok {
err = synIndex.IndexSynonym("synDoc1", synonymCollection, synDef)
if err != nil {
panic(err)
}
} else {
// If the index does not support synonym indexing, raise an error.
panic("expected synonym index")
}
// Query the index created above.
// Create a match query for the term "persistent".
query := bleve.NewMatchQuery("persistent")
// Specify the field to search within, in this case, the "text" field.
query.SetField("text")
// Create a search request with the query and enable explanation to understand how results are scored.
searchRequest := bleve.NewSearchRequest(query)
searchRequest.Explain = true
// Execute the search on the index.
searchResult, err := index.Search(searchRequest)
if err != nil {
// Handle any errors that occur during the search.
panic(err)
}
// The search result will contain one match: "doc1". This document includes the term "hardworking",
// which is a synonym for the queried term "persistent". The synonym relationship is based on
// the user-defined thesaurus associated with the index.
// Print the search results, which will include the explanation for the match.
fmt.Println(searchResult)
}you get:
1 matches, showing 1 through 1, took 599µs
1. doc1 (0.041437)but if you just change this line:
index, err := bleve.NewMemOnly(bleveMapping)
you get
No matches
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels