import os
from pymilvus import (
    connections,
    utility,
    FieldSchema,
    CollectionSchema,
    DataType,
    Collection,
    MilvusClient,
    db,
    Function,
    FunctionType,
    AnnSearchRequest,
    WeightedRanker
)

class Milvus_Database:
    def __init__(self,user=os.getenv("Milvus_USER","root"),password=os.getenv("Milvus_PASSWORD","Milvus"),uri="http://172.18.30.165:19530"):
        self.client = MilvusClient(
        uri="http://172.18.30.165:19530",
        token="root:Milvus"
    )
    def get_chunk_by_resource(self, collection_name, database_name,resource):
        """
        return List :
        """
        self.client.using_database(db_name=database_name)
        iterator = self.client.query_iterator(
            collection_name=collection_name,
            batch_size=5,
            filter=f"resource like \"{resource}\"",
            output_fields=["id","resource"],
        )

        results = []

        while True:
            result = iterator.next()
            if not result:
                iterator.close()
                break
            results += result
        return results

def get_chunk(collection_name,database_name,resource):
    milvus_db=Milvus_Database()
    results = milvus_db.get_chunk_by_resource(collection_name=collection_name,database_name=database_name,resource=resource)

    return {"code": 200, "message": "success", "data": results}

if __name__ == "__main__":
    result = get_chunk(database_name="XIAN",collection_name="ce_shi_2_4dde68",resource="经桡动脉和股动脉穿刺行支架...治疗颅内动脉狭窄的效果比较_邱茜茜(1).pdf")
    print(result)