Ejemplo n.º 1
0
def milvus_test(usr_features, IS_INFER, mov_features=None, ids=None):
    _HOST = '127.0.0.1'
    _PORT = '19530'  # default value
    table_name = 'recommender_demo'
    milvus = Milvus()

    param = {'host': _HOST, 'port': _PORT}
    status = milvus.connect(**param)
    if status.OK():
        print("Server connected.")
    else:
        print("Server connect fail.")
        sys.exit(1)

    if IS_INFER:
        status = milvus.drop_table(table_name)
        time.sleep(3)

    status, ok = milvus.has_table(table_name)
    if not ok:
        if mov_features is None:
            print("Insert vectors is none!")
            sys.exit(1)
        param = {
            'table_name': table_name,
            'dimension': 200,
            'index_file_size': 1024,  # optional
            'metric_type': MetricType.IP  # optional
        }

        print(milvus.create_table(param))

        insert_vectors = normaliz_data(mov_features)
        status, ids = milvus.insert(table_name=table_name,
                                    records=insert_vectors,
                                    ids=ids)

        time.sleep(1)

    status, result = milvus.count_table(table_name)
    print("rows in table recommender_demo:", result)

    search_vectors = normaliz_data(usr_features)
    param = {
        'table_name': table_name,
        'query_records': search_vectors,
        'top_k': 5,
        'nprobe': 16
    }
    time1 = time.time()
    status, results = milvus.search_vectors(**param)
    time2 = time.time()

    print("Top\t", "Ids\t", "Title\t", "Score")
    for i, re in enumerate(results[0]):
        title = paddle.dataset.movielens.movie_info()[int(re.id)].title
        print(i, "\t", re.id, "\t", title, "\t", float(re.distance) * 5)
Ejemplo n.º 2
0
    def test_not_connect(self):
        client = Milvus()

        with pytest.raises(NotConnectError):
            client.create_table({})

        with pytest.raises(NotConnectError):
            client.has_table("a")

        with pytest.raises(NotConnectError):
            client.describe_table("a")

        with pytest.raises(NotConnectError):
            client.drop_table("a")

        with pytest.raises(NotConnectError):
            client.create_index("a")

        with pytest.raises(NotConnectError):
            client.insert("a", [], None)

        with pytest.raises(NotConnectError):
            client.count_table("a")

        with pytest.raises(NotConnectError):
            client.show_tables()

        with pytest.raises(NotConnectError):
            client.search("a", 1, 2, [])

        with pytest.raises(NotConnectError):
            client.search_in_files("a", [], [], 2, 1)

        with pytest.raises(NotConnectError):
            client._cmd("")

        with pytest.raises(NotConnectError):
            client.preload_table("a")

        with pytest.raises(NotConnectError):
            client.describe_index("a")

        with pytest.raises(NotConnectError):
            client.drop_index("")
def milvus_test(usr_features, mov_features, ids):
    _HOST = '127.0.0.1'
    _PORT = '19530'  # default value
    milvus = Milvus()

    param = {'host': _HOST, 'port': _PORT}
    status = milvus.connect(**param)
    if status.OK():
        print("\nServer connected.")
    else:
        print("\nServer connect fail.")
        sys.exit(1)

    table_name = 'paddle_demo1'

    status, ok = milvus.has_table(table_name)
    if not ok:
        param = {
            'table_name': table_name,
            'dimension': 200,
            'index_file_size': 1024,  # optional
            'metric_type': MetricType.IP  # optional
        }

        milvus.create_table(param)

    insert_vectors = normaliz_data([usr_features.tolist()])
    status, ids = milvus.insert(table_name=table_name,
                                records=insert_vectors,
                                ids=ids)

    time.sleep(1)

    status, result = milvus.count_table(table_name)
    print("rows in table paddle_demo1:", result)

    status, table = milvus.describe_table(table_name)

    search_vectors = normaliz_data([mov_features.tolist()])
    param = {
        'table_name': table_name,
        'query_records': search_vectors,
        'top_k': 1,
        'nprobe': 16
    }
    status, results = milvus.search_vectors(**param)
    print("Searched ids:", results[0][0].id)
    print("Score:", float(results[0][0].distance) * 5)

    status = milvus.drop_table(table_name)
Ejemplo n.º 4
0
def main():
    milvus = Milvus(handler="HTTP")

    # Connect to Milvus server
    # You may need to change _HOST and _PORT accordingly
    param = {'host': _HOST, 'port': _PORT}
    status = milvus.connect(**param)
    if status.OK():
        print("Server connected.")
    else:
        print("Server connect fail.")
        sys.exit(1)

    # Create table demo_table if it dosen't exist.
    table_name = 'demo_tables'

    status, ok = milvus.has_table(table_name)
    if not ok:
        param = {
            'table_name': table_name,
            'dimension': _DIM,
            'index_file_size': _INDEX_FILE_SIZE,  # optional
            'metric_type': MetricType.L2  # optional
        }

        milvus.create_table(param)

    # Show tables in Milvus server
    _, tables = milvus.show_tables()

    # Describe demo_table
    _, table = milvus.describe_table(table_name)
    print(table)

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

    # Insert vectors into demo_table, return status and vectors id list
    status, ids = milvus.insert(table_name=table_name, records=vectors)

    # Wait for 6 seconds, until Milvus server persist vector data.
    time.sleep(6)

    # Get demo_table row count
    status, result = milvus.count_table(table_name)

    # create index of vectors, search more rapidly
    index_param = {
        'index_type': IndexType.IVFLAT,  # choice ivflat index
        'nlist': 2048
    }

    # Create ivflat index in demo_table
    # You can search vectors without creating index. however, Creating index help to
    # search faster
    status = milvus.create_index(table_name, index_param)

    # describe index, get information of index
    status, index = milvus.describe_index(table_name)
    print(index)

    # Use the top 10 vectors for similarity search
    query_vectors = vectors[0:10]

    # execute vector similarity search
    param = {
        'table_name': table_name,
        'query_records': query_vectors,
        'top_k': 1,
        'nprobe': 16
    }
    status, results = milvus.search(**param)

    if status.OK():
        # 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')

    # print results
    print(results)

    # Delete demo_table
    status = milvus.drop_table(table_name)

    # Disconnect from Milvus
    status = milvus.disconnect()