跳转到主要内容
getquery 方法中的 where_document 参数用于根据文档内容过滤记录。我们支持使用 $contains$not_contains 操作符进行全文搜索。我们还支持使用 $regex$not_regex 操作符进行 正则表达式 模式匹配。例如,这里我们获取所有文档包含特定搜索字符串的记录:
collection.get(
   where_document={"$contains": "search string"}
)
注意:全文搜索是区分大小写的。这里我们获取所有文档匹配电子邮件地址正则模式的记录:
collection.get(
   where_document={
       "$regex": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
   }
)

使用逻辑操作符

您还可以使用逻辑操作符 $and$or 来组合多个过滤器。$and 操作符将返回匹配列表中所有过滤器的结果:
collection.query(
    query_texts=["query1", "query2"],
    where_document={
        "$and": [
            {"$contains": "search_string_1"},
            {"$regex": "[a-z]+"},
        ]
    }
)
$or 操作符将返回匹配列表中任意一个过滤器的结果
collection.query(
    query_texts=["query1", "query2"],
    where_document={
        "$or": [
            {"$contains": "search_string_1"},
            {"$not_contains": "search_string_2"},
        ]
    }
)

与元数据过滤结合使用

.get.query 可以处理结合了 元数据过滤where_document 搜索
collection.query(
    query_texts=["doc10", "thus spake zarathustra", ...],
    n_results=10,
    where={"metadata_field": "is_equal_to_this"},
    where_document={"$contains":"search_string"}
)