Tencent/WeKnora · error
invalid search result format
Error message
invalid search result format
What it means
In CopyIndices' querySourceBatch, the raw search response map is expected to contain a nested 'hits' object. If the response lacks that shape (or it is not a map), the code logs 'Invalid search result format: hits object missing' and returns this error. It means Elasticsearch responded but not with the expected search-result JSON structure.
Source
Thrown at internal/application/repository/retriever/elasticsearch/v7/repository.go:1099
defer response.Body.Close()
if response.IsError() {
log.Errorf("[ElasticsearchV7] Failed to query source index data: %s", response.String())
return nil, fmt.Errorf("failed to query source index data: %s", response.String())
}
// 解析搜索结果
var searchResult map[string]interface{}
if err := json.NewDecoder(response.Body).Decode(&searchResult); err != nil {
log.Errorf("[ElasticsearchV7] Failed to parse query result: %v", err)
return nil, err
}
// 提取结果列表
hitsObj, ok := searchResult["hits"].(map[string]interface{})
if !ok {
log.Errorf("[ElasticsearchV7] Invalid search result format: 'hits' object missing")
return nil, fmt.Errorf("invalid search result format")
}
hitsList, ok := hitsObj["hits"].([]interface{})
if !ok || len(hitsList) == 0 {
if from == 0 {
log.Warnf("[ElasticsearchV7] No source index data found")
}
return []interface{}{}, nil
}
return hitsList, nil
}
// processSourceBatch processes a batch of source data and creates index information
func (e *elasticsearchRepository) processSourceBatch(ctx context.Context,
hitsList []interface{},
sourceToTargetKBIDMap map[string]string,
sourceToTargetChunkIDMap map[string]string,View on GitHub (pinned to 988cbb0330)
Solutions
- Log/inspect the full raw response body to see what Elasticsearch actually returned
- Check the cluster is healthy and the endpoint is the _search API (GET/POST <index>/_search) not another route
- Verify credentials and that no proxy/WAF is rewriting the response
- Confirm ES client/server major versions match (this is the v7 repository); retry CopyIndices once the cluster is stable
Example fix
// before
resp, err := es.PerformRequest(...)
searchResult := decode(resp)
// after: check HTTP status and errors first
if resp.StatusCode != 200 {
return nil, fmt.Errorf("search failed: status %d body %s", resp.StatusCode, string(resp.Body))
}
searchResult := decode(resp) Defensive patterns
Strategy: try-catch
Validate before calling
resp, err := client.Search(ctx, req)
if err != nil { return err }
if resp.StatusCode != 200 || !json.Valid(resp.Body) {
return fmt.Errorf("bad ES response: status=%d", resp.StatusCode)
}
var probe struct{ Hits map[string]json.RawMessage `json:"hits"` }
if json.Unmarshal(resp.Body, &probe) != nil || probe.Hits == nil {
return fmt.Errorf("response missing hits object")
} Type guard
func hasValidHitsShape(searchResult map[string]interface{}) bool {
_, ok := searchResult["hits"].(map[string]interface{})
return ok
} Try / catch
batch, err := querySourceBatch(ctx, index, from, size)
if err != nil {
if strings.Contains(err.Error(), "invalid search result format") {
logRawResponse(lastResponse) // diagnose actual ES output before retry
}
} Prevention
- Check HTTP status and decode ES error bodies before interpreting results
- Pin ES client and server to the same major version (v7 repo ↔ v7 cluster)
- Ensure no proxy/WAF rewrites JSON responses
- Add integration tests that assert the hits shape for CopyIndices
When it happens
Trigger: searchResult["hits"] fails the map[string]interface{} type assertion — e.g. an error/ack response, an empty body, or a proxy-mangled response passed as the search result.
Common situations: ES node returning an error JSON (no hits field) during index copy/migration; wrong endpoint or security interceptor altering the response; version mismatch where the client hit a non-search API; network middleware returning an HTML error page decoded into a map without 'hits'.
Related errors
- invalid hit object format
- failed to do bulk: %w
- failed to delete by query: %w
- invalid retriever type: %v
- failed to marshal query embedding: %w
AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02).
Data as JSON: /api/errors/197aaddfc4ca6b9b.
Report an issue: GitHub.