Skip to content

#22 | Add getOrCreateIndex functionality #45

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

Merged
merged 24 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c0a72a0
Implementation of delete list of documents from index.
Oct 22, 2020
7ab7a7f
Add tests for deleteDocuments , fixed existing errors in test module.
Oct 22, 2020
863b84d
#21 | Add getOrCreateIndex functionality
Oct 27, 2020
616747c
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Oct 27, 2020
099dad0
#22 | Get index only if the exception has index_already_exists messag…
Oct 27, 2020
90de15b
#22 | Correct the package of TestData and AbstractIT
Oct 28, 2020
211471f
#22 | Add integration tests for getOrCreateIndex functionality
Oct 28, 2020
fc50754
Revert "#22 | Correct the package of TestData and AbstractIT"
Oct 28, 2020
ddef140
fixed IllegalStateException.
Oct 29, 2020
04f855c
Custom Exceptions: MeiliSearchException => MeiliSearchApiException + …
eskombro Nov 4, 2020
37424c7
Merge remote-tracking branch 'upstream/master' into add_delete_docume…
Nov 4, 2020
9768950
change name of package com.meilisearch.sdk.Exception -> com.meilisear…
Nov 4, 2020
c883833
add missing import
Nov 4, 2020
b3d55b7
Merge pull request #44 from widlok/add_delete_documents_by_id
eskombro Nov 4, 2020
f3b270e
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Nov 4, 2020
4ba02d6
#21 | Add getOrCreateIndex functionality
Oct 27, 2020
52f8598
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Oct 27, 2020
faa21dd
#22 | Get index only if the exception has index_already_exists messag…
Oct 27, 2020
e01af91
#22 | Correct the package of TestData and AbstractIT
Oct 28, 2020
7459368
#22 | Add integration tests for getOrCreateIndex functionality
Oct 28, 2020
8e52501
Revert "#22 | Correct the package of TestData and AbstractIT"
Oct 28, 2020
b5c4880
Update src/main/java/com/meilisearch/sdk/Client.java
arjunrc143 Nov 4, 2020
1a71145
#22 | Add getOrCreateIndex method without primary key
Nov 4, 2020
defa8a8
Merge branch 'meilisearch-22' of github.com:arjunrc143/meilisearch-ja…
Nov 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,23 @@ public String updateIndex(String uid, String primaryKey) throws Exception {
public String deleteIndex(String uid) throws Exception {
return this.indexesHandler.delete(uid);
}

/**
* Get single index by uid or if it does not exists, Create index
*
* @param uid Unique identifier for the index to create
* @param primaryKey The primary key of the documents in that index
* @return Index instance
* @throws Exception If an error occurss
*/
public Index getOrCreateIndex(String uid, String primaryKey) throws Exception {
try {
return this.createIndex(uid, primaryKey);
} catch (Exception e) {
if(e.getMessage().contains("index_already_exists")) {
return this.getIndex(uid);
}
throw e;
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/meilisearch/integration/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
client.deleteIndex(index.getUid());
}

@Test
public void testCreateIndexWithPrimaryKeyIfIndexDoesNotExists() throws Exception {
String indexUid = "dummyIndexUid";
Index index = client.getOrCreateIndex(indexUid, this.primaryKey);
assertEquals(index.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);
client.deleteIndex(index.getUid());
}

@Test
public void testGetIndexWithPrimaryKeyIfIndexAlreadyExists() throws Exception {
String indexUid = "dummyIndexUid";
Index createdIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
assertEquals(createdIndex.getUid(), indexUid);

Index retrievedIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
assertEquals(retrievedIndex.getUid(), indexUid);
assertEquals(createdIndex.getUid(), retrievedIndex.getUid());

client.deleteIndex(createdIndex.getUid());
}

@Test
public void testGetOrCreateIndexShouldNotThrowAnyException() throws Exception {
String indexUid = "dummyIndexUid";
Index createdIndex = null;
try {
createdIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
Index retrievedIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
} catch (Exception e) {
client.deleteIndex(createdIndex.getUid());
fail("Should Not Throw Any Exception");
}
client.deleteIndex(createdIndex.getUid());
}

/**
* Test update Index PrimaryKey
*/
Expand Down