Algolia doesn’t search directly into your own data source. For data to be searchable, you need to send it to Algolia’s servers.
This happens right after retrieving your data from your data source and reformatting it. If you need more guidance with this step, please read the Format and Structure Your Data guide.
Once your data is ready, you can push it to Algolia using the saveObjects method.
If you want to get started with the most common indexing operations,
explore the quickstart samples on GitHub.
These are self-contained code samples that you can run directly from your code editor.
To push data to Algolia, you need an Application ID and a valid API key with the right access level. You can find them in the Algolia dashboard in the API Keys section. These credentials let you connect to Algolia and perform operations on your data.
The Admin API key is the one you need to create, update, and delete records. This is the most sensitive key, as it provides full control of all your indices and data. Make sure to keep it secret and secure, don’t release it to anyone, and only use it in back-end code.
For added security, it’s better not to use your Admin API key directly to handle indices, but generate more restrictive keys from it and use them instead.
// composer autoloadrequire__DIR__.'/vendor/autoload.php';// if you are not using composer// require_once 'path/to/algoliasearch.php';$client=\Algolia\AlgoliaSearch\SearchClient::create('AJ0P3S7DWQ','••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb');$index=$client->initIndex('your_index_name');
// for the default versionconstalgoliasearch=require('algoliasearch');// for the default versionimportalgoliasearchfrom'algoliasearch';// for the search only versionimportalgoliasearchfrom'algoliasearch/lite';// or just use algoliasearch if you are using a <script> tag// if you are using AMD module loader, algoliasearch will not be defined in window,// but in the AMD modules of the pageconstclient=algoliasearch('AJ0P3S7DWQ','••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb');constindex=client.initIndex('your_index_name');
Before sending anything to Algolia, you need to retrieve your data. You can do this in several ways, depending on the nature of your application. Here are potential examples:
functionfetchDataFromDatabase(){$actors=// Fetch data from your databasereturn$actors;}$records=fetchDataFromDatabase();
1
2
3
deffetch_data_from_databaseActors.all# Fetch data from your databaseend
1
2
3
4
5
6
constfetchDataFromDatabase=()=>{constactors=// Fetch data from your databasereturnactors;}constrecords=fetchDataFromDatabase();
1
2
3
4
5
deffetch_data_from_database():actors=# Fetch data from your database
returnactorsactors=fetch_data_from_database()
1
2
3
4
5
6
funcfetchDataFromDataBase()->[[String:Any]]{letrecords=// Fetch data from your databasereturnrecords}letrecords=fetchDataFromDataBase()
1
2
3
4
5
6
publicIEnumerable<Actor>FetchDataFromDataBase(){IEnumerable<Actor>actors=// Fetch data from your databasereturnactors;}varactors=FetchDataFromDataBase();
1
2
3
4
5
6
publicList<Actor>fetchDataFromDatabase(){List<Actor>actors=// Fetch data from your databasereturnactors;}List<Actor>actors=fetchDataFromDataBase();
1
2
3
4
5
6
7
8
funcfetchDataFromDatabase()[]Actor{actors:=// Fetch data from your databasereturnactors}funcmain(){records:=fetchDataFromDatabase()}
1
2
3
4
5
6
deffetchDataFromDatabase():Iterable[Actor]={valactors=// Fetch data from your databaseactors}valactors=fetchDataFromDatabase()
1
2
3
4
5
6
7
funfetchFromDatabase():List<Actor>{valactors:List<Actor>=listOf()// Fetch data from your databasereturnactors}valactors:List<Actor>=fetchFromDatabase()
usingSystem.IO;usingNewtonsoft.Json;usingNewtonsoft.Json.Linq;usingNewtonsoft.Json.Serialization;publicclassActor{publicstringName{get;set;}publicstringObjectID{get;set;}publicintRating{get;set;}publicstringImagePath{get;set;}publicstringAlternativePath{get;set;}}SearchClientclient=newSearchClient("AJ0P3S7DWQ","••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb");SearchIndexindex=client.InitIndex("actors");// Don't forget to set the naming strategy of the serializer to handle Pascal/Camel casing// Or you can set JsonProperty(PropertyName = "")] attribute on each fieldvarsettings=newJsonSerializerSettings{ContractResolver=newDefaultContractResolver{NamingStrategy=newCamelCaseNamingStrategy()}});IEnumerable<Actor>actors=JsonConvert.DeserializeObject<IEnumerable<Actor>>(File.ReadAllText("actors.json"),settings);
For performance reasons, you should send several records at once instead of one by one. If you have many records to index, you should send them in batches.