Skip to content

Commit f05cb9c

Browse files
authored
#22 | Add getOrCreateIndex functionality (#45)
* Add getOrCreateIndex functionality
1 parent b3d55b7 commit f05cb9c

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/main/java/com/meilisearch/sdk/Client.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,34 @@ public String updateIndex(String uid, String primaryKey) throws Exception {
106106
public String deleteIndex(String uid) throws Exception {
107107
return this.indexesHandler.delete(uid);
108108
}
109+
110+
/**
111+
* Get single index by uid or if it does not exists, Create index
112+
*
113+
* @param uid Unique identifier for the index to create
114+
* @param primaryKey The primary key of the documents in that index
115+
* @return Index instance
116+
* @throws Exception If an error occurss
117+
*/
118+
public Index getOrCreateIndex(String uid, String primaryKey) throws Exception {
119+
try {
120+
return this.createIndex(uid, primaryKey);
121+
} catch (MeiliSearchApiException e) {
122+
if(e.getErrorCode().equals("index_already_exists")) {
123+
return this.getIndex(uid);
124+
}
125+
throw e;
126+
}
127+
}
128+
129+
/**
130+
* Get single index by uid or if it does not exists, Create index
131+
*
132+
* @param uid Unique identifier for the index to create
133+
* @return Index instance
134+
* @throws Exception If an error occurss
135+
*/
136+
public Index getOrCreateIndex(String uid) throws Exception {
137+
return getOrCreateIndex(uid, null);
138+
}
109139
}

src/test/java/com/meilisearch/integration/ClientTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,42 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
5757
client.deleteIndex(index.getUid());
5858
}
5959

60+
@Test
61+
public void testCreateIndexWithPrimaryKeyIfIndexDoesNotExists() throws Exception {
62+
String indexUid = "dummyIndexUid";
63+
Index index = client.getOrCreateIndex(indexUid, this.primaryKey);
64+
assertEquals(index.getUid(), indexUid);
65+
assertEquals(index.getPrimaryKey(), this.primaryKey);
66+
client.deleteIndex(index.getUid());
67+
}
68+
69+
@Test
70+
public void testGetIndexWithPrimaryKeyIfIndexAlreadyExists() throws Exception {
71+
String indexUid = "dummyIndexUid";
72+
Index createdIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
73+
assertEquals(createdIndex.getUid(), indexUid);
74+
75+
Index retrievedIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
76+
assertEquals(retrievedIndex.getUid(), indexUid);
77+
assertEquals(createdIndex.getUid(), retrievedIndex.getUid());
78+
79+
client.deleteIndex(createdIndex.getUid());
80+
}
81+
82+
@Test
83+
public void testGetOrCreateIndexShouldNotThrowAnyException() throws Exception {
84+
String indexUid = "dummyIndexUid";
85+
Index createdIndex = null;
86+
try {
87+
createdIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
88+
Index retrievedIndex = client.getOrCreateIndex(indexUid, this.primaryKey);
89+
} catch (Exception e) {
90+
client.deleteIndex(createdIndex.getUid());
91+
fail("Should Not Throw Any Exception");
92+
}
93+
client.deleteIndex(createdIndex.getUid());
94+
}
95+
6096
/**
6197
* Test Index creation error: already exists
6298
*/

0 commit comments

Comments
 (0)