3.6. Setting Up the Index
An index is a server service that makes searching for data faster, especially considering concurrency and complex querying. Originally, SQL based servers were designed for a limited amount of concurrency.
In this case we use ElasticSearch (an index) on top of the SQL database in order to relieve the load of concurrency. This clearly defines spearate purposes for data stores in Cradle.
- SQL - Represents the original data in which the Index and Cache can derive from. We should do everything we can to make sure this is always up and running.
- Index - Is a wrapper for SQL (also a NoSQL solution) that handles high amounts of concurrent queries without pressuring the SQL data store.
- Cache - is a wrapper for both SQL and Index and since it doesn’t compute anything happens to the fastest from the other two. Caches, however are designed to be volatile which implies cannot be trusted over time.
Figure 3.6.A. config/services.php
...
'elastic-main' => [
'<ELASTIC HOST:PORT>'
]
...
INFORMATION: If ElasticSearch was enabled when you started the project, there
generally isn't anything you need to do. It will just work.
If you enabled ElasticSearch after the schemas were created you can still issue the following command to sync up the data structure.
Figure 3.6.B. Mapping Schema Files
$ bin/cradle elastic map
$ bin/cradle elastic populate
DANGER: Mapping schemas will remove the data on ElasticSearch for that schema
Mapping (map) is the same as building a database schema but for ElasticSearch. We need to map a schema before populating it’s data. Alternatively, If you just need to map a particular schema (let’s say profile), you can also specify the with the following command.
Figure 3.6.C. Mapping the Profile Schema
$ bin/cradle elastic map --schema profile
$ bin/cradle elastic populate --schema profile
If you need to truncate data from ElasticSearch you can do so with the following commands.
Figure 3.6.D. Truncating ElasticSearch
$ bin/cradle elastic flush
$ bin/cradle elastic flush --schema profile