{"record":{"id":"cb7b323a44e0afcf","repo":"MemPalace/mempalace","slug":"where-document-operator-key-r-not-supported","errorCode":null,"errorMessage":"where_document operator {key!r} not supported","messagePattern":"where_document operator (.+?) not supported","errorType":"exception","errorClass":"UnsupportedFilterError","httpStatus":null,"severity":"error","filePath":"mempalace/backends/milvus.py","lineNumber":203,"sourceCode":"        elif key == \"$and\":\n            if not isinstance(value, list) or not value:\n                raise UnsupportedFilterError(\"$and requires a non-empty list of clauses\")\n            nested = [translate_where_document(item) for item in value]\n            parts.append(\"(\" + \" and \".join(part for part in nested if part) + \")\")\n        elif key == \"$or\":\n            if not isinstance(value, list) or not value:\n                raise UnsupportedFilterError(\"$or requires a non-empty list of clauses\")\n            nested = [translate_where_document(item) for item in value]\n            parts.append(\"(\" + \" or \".join(part for part in nested if part) + \")\")\n        else:\n            raise UnsupportedFilterError(f\"where_document operator {key!r} not supported\")\n    return \" and \".join(part for part in parts if part)\n\n\ndef _combine_filter(*filters: str) -> str:\n    present = [flt for flt in filters if flt]\n    if not present:\n        return \"\"\n    if len(present) == 1:\n        return present[0]\n    return \"(\" + \") and (\".join(present) + \")\"\n\n\ndef _as_vector_array(vector: list[float]) -> np.ndarray:\n    arr = np.asarray(vector, dtype=np.float32)\n    if arr.ndim != 1 or arr.size == 0:\n        raise ValueError(\"embedding must be a non-empty 1D vector\")\n    return arr\n\n\ndef _normalize_vectors(embeddings: list[list[float]]) -> tuple[list[list[float]], int]:\n    vectors = []\n    dims = set()\n    for embedding in embeddings:\n        arr = _as_vector_array(embedding)\n        vectors.append(arr.astype(float).tolist())","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/MemPalace/mempalace/blob/06cb6987f02610784fefbad4b2bd5d026d164ba6/mempalace/backends/milvus.py#L185-L221","documentation":"Raised in translate_where_document() for any key other than $contains, $and, or $or. The document filter subset is deliberately tiny — Milvus can only do substring ('like') matching on the document field — so operators like $eq, $regex, or $not on where_document are rejected with UnsupportedFilterError. Field-level rich operators belong in the metadata where clause, not where_document.","triggerScenarios":"where_document={\"$regex\": \"^palace\"}, where_document={\"$eq\": \"palace\"}, or where_document={\"$not\": {\"$contains\": \"x\"}}.","commonSituations":"Porting ChromaDB/Mongo document filters verbatim; trying exact-match or negation on document text, which Milvus 'like' cannot express.","solutions":["Use $contains for substring matching on documents","Move equality/range conditions to metadata fields in the where clause (ingest the attribute as metadata if needed)","Post-filter results client-side for regex/negation needs"],"exampleFix":"// before\nwhere_document={\"$regex\": \"palace\"}\n\n// after\nwhere_document={\"$contains\": \"palace\"}","handlingStrategy":"validation","validationCode":"DOC_OPS = {\"$contains\", \"$and\", \"$or\"}\n\ndef uses_supported_doc_operators(wd: dict) -> bool:\n    return all(k in DOC_OPS for k in wd)","typeGuard":null,"tryCatchPattern":"from mempalace.backends.base import UnsupportedFilterError\ntry:\n    collection.query(query_texts=[q], where_document=wd, n_results=k)\nexcept UnsupportedFilterError as e:\n    if \"where_document operator\" in str(e):\n        raise ValueError(\"only $contains/$and/$or work on documents; use metadata where for the rest\") from e\n    raise","preventionTips":["Restrict document filtering to substring semantics ($contains)","Put equality/range conditions on metadata fields, not document text","Post-filter regex/negation results in application code"],"tags":["milvus","filter","where-document","unsupported-operator"],"backgroundTag":null,"analyzedSha":"06cb6987f02610784fefbad4b2bd5d026d164ba6","analyzedAt":"2026-08-15T03:03:36.213Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}