mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-01 00:05:43 +08:00
### What problem does this PR solve? Refact functions in engine in GO ### Type of change - [x] Refactoring
98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
//
|
|
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
package elasticsearch
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/elastic/go-elasticsearch/v8/esapi"
|
|
)
|
|
|
|
// dropIndex deletes an index
|
|
func (e *elasticsearchEngine) dropIndex(ctx context.Context, indexName string) error {
|
|
if indexName == "" {
|
|
return fmt.Errorf("index name cannot be empty")
|
|
}
|
|
|
|
// Check if index exists
|
|
exists, err := e.indexExists(ctx, indexName)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to check index existence: %w", err)
|
|
}
|
|
if !exists {
|
|
return fmt.Errorf("index '%s' does not exist", indexName)
|
|
}
|
|
|
|
// Delete index
|
|
req := esapi.IndicesDeleteRequest{
|
|
Index: []string{indexName},
|
|
}
|
|
|
|
res, err := req.Do(ctx, e.client)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete index: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.IsError() {
|
|
bodyBytes, _ := io.ReadAll(res.Body)
|
|
reason := extractErrorReason(bodyBytes)
|
|
if reason != "" {
|
|
return fmt.Errorf("elasticsearch error: %s", reason)
|
|
}
|
|
return fmt.Errorf("elasticsearch returned error: %s", res.Status())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// indexExists checks if index exists
|
|
func (e *elasticsearchEngine) indexExists(ctx context.Context, indexName string) (bool, error) {
|
|
if indexName == "" {
|
|
return false, fmt.Errorf("index name cannot be empty")
|
|
}
|
|
|
|
req := esapi.IndicesExistsRequest{
|
|
Index: []string{indexName},
|
|
}
|
|
|
|
res, err := req.Do(ctx, e.client)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to check index existence: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode == 200 {
|
|
return true, nil
|
|
} else if res.StatusCode == 404 {
|
|
return false, nil
|
|
}
|
|
|
|
bodyBytes, _ := io.ReadAll(res.Body)
|
|
reason := extractErrorReason(bodyBytes)
|
|
if reason != "" {
|
|
return false, fmt.Errorf("elasticsearch error: %s", reason)
|
|
}
|
|
return false, fmt.Errorf("elasticsearch returned error: %s", res.Status())
|
|
}
|
|
|
|
// buildMetadataIndexName returns the metadata index name for a tenant
|
|
func buildMetadataIndexName(tenantID string) string {
|
|
return fmt.Sprintf("ragflow_doc_meta_%s", tenantID)
|
|
} |