enhancement(search): allow to configure open-search as search backend

This commit is contained in:
fschade
2025-08-05 15:54:23 +02:00
parent 2857e54975
commit 63e71b5bc4
4 changed files with 61 additions and 3 deletions

View File

@@ -39,6 +39,9 @@ func DefaultConfig() *config.Config {
Bleve: config.EngineBleve{
Datapath: filepath.Join(defaults.BaseDataPath(), "search"),
},
OpenSearch: config.EngineOpenSearch{
Addresses: []string{"http://localhost:9201"},
},
},
Extractor: config.Extractor{
Type: "basic",

View File

@@ -1,12 +1,36 @@
package config
import (
"net/http"
"time"
)
// Engine defines which search engine to use
type Engine struct {
Type string `yaml:"type" env:"SEARCH_ENGINE_TYPE" desc:"Defines which search engine to use. Defaults to 'bleve'. Supported values are: 'bleve'." introductionVersion:"1.0.0"`
Bleve EngineBleve `yaml:"bleve"`
Type string `yaml:"type" env:"SEARCH_ENGINE_TYPE" desc:"Defines which search engine to use. Defaults to 'bleve'. Supported values are: 'bleve'." introductionVersion:"1.0.0"`
Bleve EngineBleve `yaml:"bleve"`
OpenSearch EngineOpenSearch `yaml:"open_search"`
}
// EngineBleve configures the bleve engine
type EngineBleve struct {
Datapath string `yaml:"data_path" env:"SEARCH_ENGINE_BLEVE_DATA_PATH" desc:"The directory where the filesystem will store search data. If not defined, the root directory derives from $OC_BASE_DATA_PATH/search." introductionVersion:"1.0.0"`
}
// EngineOpenSearch configures the OpenSearch engine
type EngineOpenSearch struct {
Addresses []string `yaml:"addresses" env:"SEARCH_ENGINE_OPEN_SEARCH_ADDRESSES" desc:"The addresses of the OpenSearch nodes.." introductionVersion:"%%NEXT%%"`
Username string `yaml:"username" env:"SEARCH_ENGINE_OPEN_SEARCH_USERNAME" desc:"Username for HTTP Basic Authentication." introductionVersion:"%%NEXT%%"`
Password string `yaml:"password" env:"SEARCH_ENGINE_OPEN_SEARCH_PASSWORD" desc:"Password for HTTP Basic Authentication." introductionVersion:"%%NEXT%%"`
Header http.Header `yaml:"header" env:"SEARCH_ENGINE_OPEN_SEARCH_HEADER" desc:"HTTP headers to include in requests." introductionVersion:"%%NEXT%%"`
CACert []byte `yaml:"ca_cert" env:"SEARCH_ENGINE_OPEN_SEARCH_CA_CERT" desc:"CA certificate for TLS connections." introductionVersion:"%%NEXT%%"`
RetryOnStatus []int `yaml:"retry_on_status" env:"SEARCH_ENGINE_OPEN_SEARCH_RETRY_ON_STATUS" desc:"HTTP status codes that trigger a retry." introductionVersion:"%%NEXT%%"`
DisableRetry bool `yaml:"disable_retry" env:"SEARCH_ENGINE_OPEN_SEARCH_DISABLE_RETRY" desc:"Disable retries on errors." introductionVersion:"%%NEXT%%"`
EnableRetryOnTimeout bool `yaml:"enable_retry_on_timeout" env:"SEARCH_ENGINE_OPEN_SEARCH_ENABLE_RETRY_ON_TIMEOUT" desc:"Enable retries on timeout." introductionVersion:"%%NEXT%%"`
MaxRetries int `yaml:"max_retries" env:"SEARCH_ENGINE_OPEN_SEARCH_MAX_RETRIES" desc:"Maximum number of retries for requests." introductionVersion:"%%NEXT%%"`
CompressRequestBody bool `yaml:"compress_request_body" env:"SEARCH_ENGINE_OPEN_SEARCH_COMPRESS_REQUEST_BODY" desc:"Compress request bodies." introductionVersion:"%%NEXT%%"`
DiscoverNodesOnStart bool `yaml:"discover_nodes_on_start" env:"SEARCH_ENGINE_OPEN_SEARCH_DISCOVER_NODES_ON_START" desc:"Discover nodes on service start." introductionVersion:"%%NEXT%%"`
DiscoverNodesInterval time.Duration `yaml:"discover_nodes_interval" env:"SEARCH_ENGINE_OPEN_SEARCH_DISCOVER_NODES_INTERVAL" desc:"Interval for discovering nodes." introductionVersion:"%%NEXT%%"`
EnableMetrics bool `yaml:"enable_metrics" env:"SEARCH_ENGINE_OPEN_SEARCH_ENABLE_METRICS" desc:"Enable metrics collection." introductionVersion:"%%NEXT%%"`
EnableDebugLogger bool `yaml:"enable_debug_logger" env:"SEARCH_ENGINE_OPEN_SEARCH_ENABLE_DEBUG_LOGGER" desc:"Enable debug logging." introductionVersion:"%%NEXT%%"`
}

View File

@@ -8,6 +8,7 @@ import (
"strings"
"testing"
"github.com/opencloud-eu/opencloud/services/search/pkg/config/defaults"
opensearchgo "github.com/opensearch-project/opensearch-go/v4"
opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/stretchr/testify/require"
@@ -21,7 +22,7 @@ type TestClient struct {
func NewDefaultTestClient(t *testing.T) *TestClient {
client, err := opensearchgoAPI.NewClient(opensearchgoAPI.Config{
Client: opensearchgo.Config{
Addresses: []string{"http://localhost:9200"},
Addresses: defaults.DefaultConfig().Engine.OpenSearch.Addresses,
},
})
require.NoError(t, err, "failed to create OpenSearch client")

View File

@@ -29,6 +29,7 @@ import (
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/opencloud-eu/opencloud/services/search/pkg/content"
"github.com/opencloud-eu/opencloud/services/search/pkg/engine"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch"
"github.com/opencloud-eu/opencloud/services/search/pkg/query/bleve"
"github.com/opencloud-eu/opencloud/services/search/pkg/search"
)
@@ -54,6 +55,35 @@ func NewHandler(opts ...Option) (searchsvc.SearchProviderHandler, func(), error)
}
eng = engine.NewBleveEngine(idx, bleve.DefaultCreator, logger)
case "open-search":
client, err := opensearchgoAPI.NewClient(opensearchgoAPI.Config{
Client: opensearchgo.Config{
Addresses: cfg.Engine.OpenSearch.Addresses,
Username: cfg.Engine.OpenSearch.Username,
Password: cfg.Engine.OpenSearch.Password,
Header: cfg.Engine.OpenSearch.Header,
CACert: cfg.Engine.OpenSearch.CACert,
RetryOnStatus: cfg.Engine.OpenSearch.RetryOnStatus,
DisableRetry: cfg.Engine.OpenSearch.DisableRetry,
EnableRetryOnTimeout: cfg.Engine.OpenSearch.EnableRetryOnTimeout,
MaxRetries: cfg.Engine.OpenSearch.MaxRetries,
CompressRequestBody: cfg.Engine.OpenSearch.CompressRequestBody,
DiscoverNodesOnStart: cfg.Engine.OpenSearch.DiscoverNodesOnStart,
DiscoverNodesInterval: cfg.Engine.OpenSearch.DiscoverNodesInterval,
EnableMetrics: cfg.Engine.OpenSearch.EnableMetrics,
EnableDebugLogger: cfg.Engine.OpenSearch.EnableDebugLogger,
},
})
if err != nil {
return nil, teardown, fmt.Errorf("failed to create OpenSearch client: %w", err)
}
ose, err := opensearch.NewEngine("opencloud-default-resource", client)
if err != nil {
return nil, teardown, fmt.Errorf("failed to create OpenSearch engine: %w", err)
}
eng = ose
default:
return nil, teardown, fmt.Errorf("unknown search engine: %s", cfg.Engine.Type)
}