-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Comments
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? |
The new route can go in @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 For tests a |
Add a new
swap_indexes
post
route tomeilisearch_routes
. See swap_indexes for implementation details.Tests should also be added for this new route.
The text was updated successfully, but these errors were encountered: