Skip to content

Commit

Permalink
Don't treat '_all' index name as single index, but as pattern equal t…
Browse files Browse the repository at this point in the history
…o '*' (#115)
  • Loading branch information
pivovarit authored May 15, 2024
1 parent d9e3b26 commit 45a6133
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
7 changes: 7 additions & 0 deletions quesma/elasticsearch/index_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func NewIndexResolver(elasticsearchUrl string) IndexResolver {
}
}

func NormalizePattern(p string) string {
if p == "_all" {
return "*"
}
return p
}

func (im *indexResolver) Resolve(indexPattern string) (Sources, bool, error) {
req, _ := http.NewRequest("GET", im.Url+"/_resolve/index/"+indexPattern+"?expand_wildcards=open", bytes.NewBuffer([]byte{}))
response, err := im.httpClient.Do(req)
Expand Down
13 changes: 3 additions & 10 deletions quesma/quesma/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func matchedAgainstBulkBody(configuration config.QuesmaConfiguration) func(m map

func matchedAgainstPattern(configuration config.QuesmaConfiguration) mux.MatchPredicate {
return func(m map[string]string, _ string) bool {
indexPattern := m["index"]
indexPattern := elasticsearch.NormalizePattern(m["index"])
if elasticsearch.IsInternalIndex(indexPattern) {
logger.Debug().Msgf("index %s is an internal Elasticsearch index, skipping", indexPattern)
return false
Expand All @@ -52,7 +52,7 @@ func matchedAgainstPattern(configuration config.QuesmaConfiguration) mux.MatchPr

for _, pattern := range indexPatterns {
for _, indexName := range configuration.IndexConfig {
if config.MatchName(preprocessPattern(pattern), indexName.Name) {
if config.MatchName(elasticsearch.NormalizePattern(pattern), indexName.Name) {
if configuration.IndexConfig[indexName.Name].Enabled {
return true
}
Expand All @@ -62,7 +62,7 @@ func matchedAgainstPattern(configuration config.QuesmaConfiguration) mux.MatchPr
return false
} else {
for _, index := range configuration.IndexConfig {
pattern := preprocessPattern(indexPattern)
pattern := elasticsearch.NormalizePattern(indexPattern)
if config.MatchName(pattern, index.Name) {
if indexConfig, exists := configuration.IndexConfig[index.Name]; exists {
return indexConfig.Enabled
Expand All @@ -74,10 +74,3 @@ func matchedAgainstPattern(configuration config.QuesmaConfiguration) mux.MatchPr
}
}
}

func preprocessPattern(p string) string {
if p == "_all" {
return "*"
}
return p
}
6 changes: 3 additions & 3 deletions quesma/quesma/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func configureRouter(cfg config.QuesmaConfiguration, lm *clickhouse.LogManager,
})

router.RegisterPathMatcher(routes.ResolveIndexPath, []string{"GET"}, always(), func(ctx context.Context, body string, _ string, params map[string]string) (*mux.Result, error) {
pattern := params["index"]
pattern := elasticsearch.NormalizePattern(params["index"])
if elasticsearch.IsIndexPattern(pattern) {
// todo avoid creating new instances all the time
sources, found, err := elasticsearch.NewIndexResolver(cfg.Elasticsearch.Url.String()).Resolve(pattern)
Expand All @@ -78,7 +78,7 @@ func configureRouter(cfg config.QuesmaConfiguration, lm *clickhouse.LogManager,
})
definitions.Range(
func(name string, table *clickhouse.Table) bool {
if config.MatchName(preprocessPattern(pattern), name) {
if config.MatchName(elasticsearch.NormalizePattern(pattern), name) {
sources.DataStreams = append(sources.DataStreams, elasticsearch.DataStream{
Name: name,
BackingIndices: []string{name},
Expand All @@ -91,7 +91,7 @@ func configureRouter(cfg config.QuesmaConfiguration, lm *clickhouse.LogManager,

return resolveIndexResult(sources), nil
} else {
if config.MatchName(preprocessPattern(pattern), pattern) {
if config.MatchName(elasticsearch.NormalizePattern(pattern), pattern) {
definitions := lm.GetTableDefinitions()
if definitions.Has(pattern) {
return resolveIndexResult(elasticsearch.Sources{
Expand Down

0 comments on commit 45a6133

Please sign in to comment.