Create an index
To create an index, you need to perform any indexing operation like:
- set settings
- add object
List indices -
You can list all your indices along with their associated information (number of entries, disk size, etc.) with the
//Sync & Async version client.listIndices();
Delete an index -
You can delete an index using its name:
//Sync version Index contacts = client.initIndex("contacts"); contacts.delete();
//Async version AsyncIndex contacts = client.initIndex("contacts"); contacts.delete();
Clear an index -
You can delete the index contents without removing settings and index specific API keys by using the
//Sync & Async version index.clear();
Copy index -
You can copy an existing index using the
Warning: The copy command will overwrite the destination index.
//Sync version Index myIndex = client.initIndex("MyIndex"); // Copy MyIndex in MyIndexCopy myIndex.copyTo("MyIndexCopy");
//Async version AsyncIndex myIndex = client.initIndex("MyIndex"); // Copy MyIndex in MyIndexCopy myIndex.copyTo("MyIndexCopy");
Move index -
In some cases, you may want to totally reindex all your data. In order to keep your existing service running while re-importing your data we recommend the usage of a temporary index plus an atomical move using the
//For the sync version Index myNewIndex = client.initIndex("MyNewIndex"); // Rename MyNewIndex in MyIndex (and overwrite it) myNewIndex.moveTo("MyIndex");
//For the async version AsyncIndex myNewIndex = client.initIndex("MyNewIndex"); // Rename MyNewIndex in MyIndex (and overwrite it) myNewIndex.moveTo("MyIndex");
The moveTo method will overwrite the destination index, and delete the temporary index.
The moveTo operation will override all settings of the destination, There is one exception for the replicas parameter which is not impacted.
For example, if you want to fully update your index
MyIndex every night, we recommend the following process:
- Get settings and synonyms from the old index using Get settings and Get synonym.
- Apply settings and synonyms to the temporary index
MyTmpIndex, (this will create the
MyTmpIndexindex) using Set settings and Batch synonyms (make sure to remove the replicas parameter from the settings if it exists).
- Import your records into a new index using Add Objects.
- Atomically replace the index
MyIndexwith the content and settings of the index
MyTmpIndexusing the Move index method. This will automatically override the old index without any downtime on the search.
- You’ll end up with only one index called
MyIndex, that contains the records and settings pushed to
MyTmpIndexand the replica-indices that were initially attached to
MyIndexwill be in sync with the new data.