dgraph-io/dgraph · error

Value for 'ef' must be positive, got: %d

Error message

Value for 'ef' must be positive, got: %d

What it means

After 'ef' parses as an integer, the parser requires it to be strictly positive because ef controls the HNSW search candidate list size and zero/negative values are meaningless. A parsed value <= 0 triggers this error and aborts applying the similar_to options.

Source

Thrown at worker/task.go:2804

			k = strings.TrimSuffix(k, ":")
		}
		if len(k) == 0 {
			return errors.Errorf("Malformed option in similar_to: empty key")
		}
		if _, dup := seen[k]; dup {
			return errors.Errorf("Duplicate key in similar_to options: %q", k)
		}
		seen[k] = struct{}{}

		v = strings.Trim(v, "\"'")
		switch k {
		case "ef":
			n, perr := strconv.ParseInt(v, 10, 32)
			if perr != nil {
				return errors.Errorf("Invalid value for 'ef' in similar_to: %q", v)
			}
			if n <= 0 {
				return errors.Errorf("Value for 'ef' must be positive, got: %d", n)
			}
			fc.vsEfOverride = int(n)
		case "distance_threshold":
			f, perr := strconv.ParseFloat(v, 64)
			if perr != nil {
				return errors.Errorf("Invalid value for 'distance_threshold' in similar_to: %q", v)
			}
			if f < 0 {
				return errors.Errorf("Value for 'distance_threshold' must be non-negative, got: %v", f)
			}
			fc.vsDistanceThreshold = new(float64)
			*fc.vsDistanceThreshold = f
		default:
			return errors.Errorf("Unknown option in similar_to: %q", k)
		}
	}
	return nil
}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Use a positive ef value (e.g. 'ef: 10' or higher); omit the 'ef' option entirely if you want the index default.
  2. Guard client-side: only emit the ef option when the computed value is > 0.
  3. Treat 0 as 'not set' in your builder and drop the key.

Example fix

// before
if ef > -1 { opts += "; ef: " + strconv.Itoa(ef) }
// after
if ef > 0 { opts += "; ef: " + strconv.Itoa(ef) }
Defensive patterns

Strategy: validation

Validate before calling

if ef <= 0 { ef = defaultEf } // drop or substitute before serializing
opts := fmt.Sprintf("; ef: %d", ef)

Try / catch

if err := runQuery(ctx); err != nil && strings.Contains(err.Error(), "'ef' must be positive") {
    return retryWithDefaultEf(ctx)
}

Prevention

When it happens

Trigger: similar_to option string like 'ef: 0' or 'ef: -5'. The value parses as an int32 but fails the n <= 0 check.

Common situations: Default-value placeholders left at 0 in a query template; sign errors when computing ef from a formula; UI form allowing 0 as 'unset' which is then serialized literally.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/672035eb186e8a80. Report an issue: GitHub.