enhancement(search): allow to set the resource index name

This commit is contained in:
fschade
2025-08-06 14:59:54 +02:00
parent 1586f7fcbb
commit a9d21bbb15
4 changed files with 50 additions and 33 deletions
@@ -40,7 +40,12 @@ func DefaultConfig() *config.Config {
Datapath: filepath.Join(defaults.BaseDataPath(), "search"),
},
OpenSearch: config.EngineOpenSearch{
Addresses: []string{"http://localhost:9201"},
Client: config.EngineOpenSearchClient{
Addresses: []string{"http://localhost:9201"},
},
ResourceIndex: config.EngineOpenSearchResourceIndex{
Name: "opencloud-resource",
},
},
},
Extractor: config.Extractor{
+25 -14
View File
@@ -19,18 +19,29 @@ type EngineBleve struct {
// 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%%"`
Client EngineOpenSearchClient `yaml:"client"`
ResourceIndex EngineOpenSearchResourceIndex `yaml:"resource_index"`
}
// EngineOpenSearchResourceIndex defines the OpenSearch index for resources
type EngineOpenSearchResourceIndex struct {
Name string `yaml:"name" env:"SEARCH_ENGINE_OPEN_SEARCH_RESOURCE_INDEX_NAME" desc:"The name of the OpenSearch index for resources." introductionVersion:"%%NEXT%%"`
}
// EngineOpenSearchClient configures the OpenSearch client
type EngineOpenSearchClient struct {
Addresses []string `yaml:"addresses" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_ADDRESSES" desc:"The addresses of the OpenSearch nodes.." introductionVersion:"%%NEXT%%"`
Username string `yaml:"username" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_USERNAME" desc:"Username for HTTP Basic Authentication." introductionVersion:"%%NEXT%%"`
Password string `yaml:"password" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_PASSWORD" desc:"Password for HTTP Basic Authentication." introductionVersion:"%%NEXT%%"`
Header http.Header `yaml:"header" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_HEADER" desc:"HTTP headers to include in requests." introductionVersion:"%%NEXT%%"`
CACert []byte `yaml:"ca_cert" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_CA_CERT" desc:"CA certificate for TLS connections." introductionVersion:"%%NEXT%%"`
RetryOnStatus []int `yaml:"retry_on_status" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_RETRY_ON_STATUS" desc:"HTTP status codes that trigger a retry." introductionVersion:"%%NEXT%%"`
DisableRetry bool `yaml:"disable_retry" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_DISABLE_RETRY" desc:"Disable retries on errors." introductionVersion:"%%NEXT%%"`
EnableRetryOnTimeout bool `yaml:"enable_retry_on_timeout" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_ENABLE_RETRY_ON_TIMEOUT" desc:"Enable retries on timeout." introductionVersion:"%%NEXT%%"`
MaxRetries int `yaml:"max_retries" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_MAX_RETRIES" desc:"Maximum number of retries for requests." introductionVersion:"%%NEXT%%"`
CompressRequestBody bool `yaml:"compress_request_body" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_COMPRESS_REQUEST_BODY" desc:"Compress request bodies." introductionVersion:"%%NEXT%%"`
DiscoverNodesOnStart bool `yaml:"discover_nodes_on_start" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_DISCOVER_NODES_ON_START" desc:"Discover nodes on service start." introductionVersion:"%%NEXT%%"`
DiscoverNodesInterval time.Duration `yaml:"discover_nodes_interval" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_DISCOVER_NODES_INTERVAL" desc:"Interval for discovering nodes." introductionVersion:"%%NEXT%%"`
EnableMetrics bool `yaml:"enable_metrics" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_ENABLE_METRICS" desc:"Enable metrics collection." introductionVersion:"%%NEXT%%"`
EnableDebugLogger bool `yaml:"enable_debug_logger" env:"SEARCH_ENGINE_OPEN_SEARCH_CLIENT_ENABLE_DEBUG_LOGGER" desc:"Enable debug logging." introductionVersion:"%%NEXT%%"`
}
@@ -8,10 +8,11 @@ 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"
"github.com/opencloud-eu/opencloud/services/search/pkg/config/defaults"
)
type TestClient struct {
@@ -22,7 +23,7 @@ type TestClient struct {
func NewDefaultTestClient(t *testing.T) *TestClient {
client, err := opensearchgoAPI.NewClient(opensearchgoAPI.Config{
Client: opensearchgo.Config{
Addresses: defaults.DefaultConfig().Engine.OpenSearch.Addresses,
Addresses: defaults.DefaultConfig().Engine.OpenSearch.Client.Addresses,
},
})
require.NoError(t, err, "failed to create OpenSearch client")
+16 -16
View File
@@ -60,32 +60,32 @@ func NewHandler(opts ...Option) (searchsvc.SearchProviderHandler, func(), error)
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,
Addresses: cfg.Engine.OpenSearch.Client.Addresses,
Username: cfg.Engine.OpenSearch.Client.Username,
Password: cfg.Engine.OpenSearch.Client.Password,
Header: cfg.Engine.OpenSearch.Client.Header,
CACert: cfg.Engine.OpenSearch.Client.CACert,
RetryOnStatus: cfg.Engine.OpenSearch.Client.RetryOnStatus,
DisableRetry: cfg.Engine.OpenSearch.Client.DisableRetry,
EnableRetryOnTimeout: cfg.Engine.OpenSearch.Client.EnableRetryOnTimeout,
MaxRetries: cfg.Engine.OpenSearch.Client.MaxRetries,
CompressRequestBody: cfg.Engine.OpenSearch.Client.CompressRequestBody,
DiscoverNodesOnStart: cfg.Engine.OpenSearch.Client.DiscoverNodesOnStart,
DiscoverNodesInterval: cfg.Engine.OpenSearch.Client.DiscoverNodesInterval,
EnableMetrics: cfg.Engine.OpenSearch.Client.EnableMetrics,
EnableDebugLogger: cfg.Engine.OpenSearch.Client.EnableDebugLogger,
},
})
if err != nil {
return nil, teardown, fmt.Errorf("failed to create OpenSearch client: %w", err)
}
ose, err := opensearch.NewEngine("opencloud-default-resource", client)
backend, err := opensearch.NewEngine(cfg.Engine.OpenSearch.ResourceIndex.Name, client)
if err != nil {
return nil, teardown, fmt.Errorf("failed to create OpenSearch engine: %w", err)
}
eng = ose
eng = backend
default:
return nil, teardown, fmt.Errorf("unknown search engine: %s", cfg.Engine.Type)
}