Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new swap index post route to the meilisearch_routes #436

Open
sanders41 opened this issue Apr 16, 2023 · 2 comments
Open

Add new swap index post route to the meilisearch_routes #436

sanders41 opened this issue Apr 16, 2023 · 2 comments
Labels
good first issue Good for newcomers

Comments

@sanders41
Copy link
Owner

Add a new swap_indexes post route to meilisearch_routes. See swap_indexes for implementation details.

Tests should also be added for this new route.

@sanders41 sanders41 added the good first issue Good for newcomers label Apr 16, 2023
@Himanshuch1708
Copy link

I call it swap_indexes and it will accept two index_uid parameters: index_uids and index_uids_to_swap_with. Here's what the route code would look like:

from fastapi import APIRouter

from meilisearch_python_async import MeiliSearch

router = APIRouter()

@router.post("/indexes/swap")
async def swap_indexes(index_uids: list[str], index_uids_to_swap_with: list[str], client: MeiliSearch):
    return await client.swap_indexes(index_uids, index_uids_to_swap_with)

This route will call the swap_indexes method on the MeiliSearch client, passing in the index_uids and index_uids_to_swap_with parameters.


We'll create a new file called test_swap_indexes.py in the tests directory, and add the following code:

import pytest
from httpx import AsyncClient
from fastapi import FastAPI

from meilisearch_python_async import MeiliSearch
from meilisearch_async.fastapi import MeiliSearchAsync
from meilisearch_async.fastapi.routes import router as meilisearch_router

@pytest.fixture(scope="module")
async def meilisearch_client():
    client = MeiliSearch("http://localhost:7700", "masterKey")
    yield client
    await client.delete_index("test_index")

@pytest.fixture(scope="module")
def app(meilisearch_client):
    app = FastAPI()
    app.include_router(meilisearch_router, prefix="/search")
    app.include_router(meilisearch_router, prefix="/indexes")
    app.state.client = meilisearch_client
    return app

@pytest.fixture(scope="module")
async def async_client(app):
    async with AsyncClient(app=app, base_url="http://testserver") as client:
        yield client

@pytest.mark.asyncio
async def test_swap_indexes(async_client, meilisearch_client):
    # Create two test indexes
    index1 = await meilisearch_client.create_index("test_index1")
    index2 = await meilisearch_client.create_index("test_index2")

    # Add some documents to the first index
    await index1.add_documents([
        {"id": 1, "title": "Document 1", "body": "This is the body of document 1."},
        {"id": 2, "title": "Document 2", "body": "This is the body of document 2."},
        {"id": 3, "title": "Document 3", "body": "This is the body of document 3."},
    ])

    # Add some documents to the second index
    await index2.add_documents([
        {"id": 4, "title": "Document 4", "body": "This is the body of document 4."},
        {"id": 5, "title": "Document 5", "body": "This is the body of document 5."},
        {"id": 6, "title": "Document 6", "body": "This is the body of document 6."},
    ])

    # Swap the indexes
    response = await async_client.post("/indexes/swap", json={
        "index_uids": ["test_index1", "test_index2"],
        "index_uids_to_swap_with": ["test_index2", "test_index1"],
    })

    # Check that the response was successful

If you have confirmed that all of the code changes i made are correct and functioning as expected, then yes, i can create a pull request to merge my changes into the main codebase. However, before creating the pull request,

Should I create a new Python test within it?

@sanders41
Copy link
Owner Author

sanders41 commented Apr 18, 2023

I call it swap_indexes and it will accept two index_uid parameters: index_uids and index_uids_to_swap_with. Here's what the route code would look like:

The new route can go in meiliesarch_routes.py so we won't need a new file. Also, there isn't a MeiliSearch class in meilisearch_python_async. The route should look like:

@router.post("/swap-indexes", response_mode=TaskInfo, tags=["Meilisearch"])
async def swap_indexes(indexes: List[SwapIndexes], client: Client = Depends(meilisarch_client)) -> TaskInfo:
    swap = [(x.from_index, x.to_index) for x in indexes]
    return await client.swap_indexes(swap)

A new SwapIndexes model needs to be created that has from_index and to_index string fields. This can be added in models/index.py

For tests a test_swap_indexes tests needs to be added to tests/test_meilisearch_routes.py that is the FastAPI equivalent of this tests. The fixtures you have listed are already created so no need to create them again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants