コード例 #1
0
    "anns_field": "embedding",
    "param": {
        "metric_type": "L2",
        "params": {
            "nprobe": 8
        }
    },
    "limit": 3,
    "output_fields": ["release_year"],
    "expression": "release_year in [1995, 2002]",
}

# ------
# Basic hybrid search entities
# ------
results = client.search_with_expression(collection_name, **search_param)
for entities in results:
    for topk_film in entities:
        current_entity = topk_film.entity
        print("==")
        print(f"- id: {topk_film.id}")
        print(f"- title: {titles[topk_film.id]}")
        print(f"- distance: {topk_film.distance}")
        print(f"- release_year: {current_entity.release_year}")

# ------
# Basic delete index:
#     You can drop index we create
# ------
client.drop_index(collection_name, "embedding")
コード例 #2
0
    ],
    }, orm=True)

    assert c.has_collection(collection_name)

    ids = c.insert(collection_name, [
        {"name": "f1", "type": DataType.FLOAT_VECTOR, "values": [[1.1, 2.2, 3.3, 4.4], [5.5, 6.6, 7.7, 8.8]]},
        {"name": "age", "type": DataType.FLOAT, "values": [3.45, 8.9]}
    ], orm=True)

    c.flush([collection_name])

    c.load_collection(collection_name)

    #############################################################
    search_params = {"metric_type": "L2", "params": {"nprobe": 1}}

    results = c.search_with_expression(collection_name, [[1.1, 2.2, 3.3, 4.4]],
                                       "f1", param=search_params, limit=2, output_fields=["id"])

    print("search results: ", results[0][0].entity, " + ", results[0][1].entity)
    #############################################################

    ids_expr = ",".join(str(x) for x in ids.primary_keys)

    expr = "id in [ " + ids_expr + " ] "

    print(expr)

    pprint(c.query(collection_name, expr))
コード例 #3
0
ファイル: example.py プロジェクト: DragonDriver/pymilvus
def main():
    # Specify server addr when create milvus client instance
    # milvus client instance maintain a connection pool, param
    # `pool_size` specify the max connection num.
    milvus = Milvus(_HOST, _PORT)

    # Create collection demo_collection if it dosen't exist.
    collection_name = 'example_collection'

    ok = milvus.has_collection(collection_name)
    field_name = 'example_field'
    id_name = "id"
    if not ok:
        fields = {"fields": [{
            "name": field_name,
            "type": DataType.FLOAT_VECTOR,
            "metric_type": "L2",
            "params": {"dim": _DIM},
            "indexes": [{"metric_type": "L2"}]
        },
            {
                "name": id_name,
                "type": DataType.INT64,
                "auto_id": True,
                "is_primary": True,
            }
        ]}

        milvus.create_collection(collection_name=collection_name, fields=fields)
    else:
        milvus.drop_collection(collection_name=collection_name)

    # Show collections in Milvus server
    collections = milvus.list_collections()
    print(collections)

    # Describe demo_collection
    stats = milvus.get_collection_stats(collection_name)
    print(stats)

    # 10000 vectors with 128 dimension
    # element per dimension is float32 type
    # vectors should be a 2-D array
    vectors = [[random.random() for _ in range(_DIM)] for _ in range(10)]
    print(vectors)
    # You can also use numpy to generate random vectors:
    #   vectors = np.random.rand(10000, _DIM).astype(np.float32)

    # Insert vectors into demo_collection, return status and vectors id list
    entities = [{"name": field_name, "type": DataType.FLOAT_VECTOR, "values": vectors}]

    res_ids = milvus.insert(collection_name=collection_name, entities=entities)
    print("ids:", res_ids.primary_keys)

    # Flush collection  inserted data to disk.
    milvus.flush([collection_name])

    # present collection statistics info
    stats = milvus.get_collection_stats(collection_name)
    print(stats)

    # create index of vectors, search more rapidly
    index_param = {
        "metric_type": "L2",
        "index_type": "IVF_FLAT",
        "params": {"nlist": 1024}
    }

    # Create ivflat index in demo_collection
    # You can search vectors without creating index. however, Creating index help to
    # search faster
    print("Creating index: {}".format(index_param))
    status = milvus.create_index(collection_name, field_name, index_param)

    # execute vector similarity search

    print("Searching ... ")

    dsl = {"bool": {"must": [{"vector": {
        field_name: {
            "metric_type": "L2",
            "query": vectors,
            "topk": 10,
            "params": {"nprobe": 16}
        }
    }}]}}

    search_params = {"metric_type": "L2", "params": {"nprobe": 10}}

    milvus.load_collection(collection_name)
    results = milvus.search_with_expression(collection_name, vectors, field_name, param=search_params, limit=10,
                                            output_fields=[id_name])
    print("search results: ", results[0][0].entity)
    # indicate search result
    # also use by:
    #   `results.distance_array[0][0] == 0.0 or results.id_array[0][0] == ids[0]`
    if results[0][0].distance == 0.0 or results[0][0].id == ids[0]:
        print('Query result is correct')
    else:
        print('Query result isn\'t correct')

    milvus.drop_index(collection_name, field_name)
    milvus.release_collection(collection_name)

    # Delete demo_collection
    status = milvus.drop_collection(collection_name)