Installation
- JavaScript
- PHP
- Java
To get started, you first need to install algoliasearch
(or any other available API client package). You can find the full list of available packages here.
All of our clients comes with type definition, and are available for both browser
and node
environments.
bash
yarn add @experimental-api-clients-automation/algoliasearch# ornpm install @experimental-api-clients-automation/algoliasearch
bash
yarn add @experimental-api-clients-automation/algoliasearch# ornpm install @experimental-api-clients-automation/algoliasearch
Or use a specific package:
bash
yarn add @experimental-api-clients-automation/client-search# ornpm install @experimental-api-clients-automation/client-search
bash
yarn add @experimental-api-clients-automation/client-search# ornpm install @experimental-api-clients-automation/client-search
Without a package manager
Add the following JavaScript snippet to the <head>
of your website:
html
<script src="https://cdn.jsdelivr.net/npm/@experimental-api-clients-automation/algoliasearch/dist/algoliasearch.umd.browser.js"></script>
html
<script src="https://cdn.jsdelivr.net/npm/@experimental-api-clients-automation/algoliasearch/dist/algoliasearch.umd.browser.js"></script>
First, install Algolia PHP API Client via the composer package manager:
bash
composer require algolia/algoliasearch-client-php
bash
composer require algolia/algoliasearch-client-php
To get started, add the algoliasearch-client-java dependency to your project, either with Maven:
xml
<repositories><repository><id>oss.sonatype.org-snapshot</id><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><dependency><groupId>com.algolia</groupId><artifactId>algoliasearch-client-java</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
xml
<repositories><repository><id>oss.sonatype.org-snapshot</id><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><dependency><groupId>com.algolia</groupId><artifactId>algoliasearch-client-java</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
or Gradle:
groovy
repositories() {maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }}dependencies {testImplementation 'com.algolia:algoliasearch-client-java:0.0.1-SNAPSHOT'}
groovy
repositories() {maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }}dependencies {testImplementation 'com.algolia:algoliasearch-client-java:0.0.1-SNAPSHOT'}
Using the client
- JavaScript
- PHP
- Java
Then you need to import the correct package:
js
import { algoliasearch } from '@experimental-api-clients-automation/algoliasearch';const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');// And access analytics or personalization clientconst analyticsClient = client.initAnalytics('<YOUR_APP_ID>', '<YOUR_API_KEY>');const personalizationClient = client.initPersonalization('<YOUR_APP_ID>','<YOUR_API_KEY>','eu');const searchRes = await client.search({requests: [{indexName: '<YOUR_INDEX_NAME>',query: '<YOUR_QUERY>',},],});const analyticsRes = await analyticsClient.getTopFilterForAttribute({attribute: 'myAttribute1,myAttribute2',index: '<YOUR_INDEX_NAME>',});console.log('[Results]', searchRes, analyticsRes);
js
import { algoliasearch } from '@experimental-api-clients-automation/algoliasearch';const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');// And access analytics or personalization clientconst analyticsClient = client.initAnalytics('<YOUR_APP_ID>', '<YOUR_API_KEY>');const personalizationClient = client.initPersonalization('<YOUR_APP_ID>','<YOUR_API_KEY>','eu');const searchRes = await client.search({requests: [{indexName: '<YOUR_INDEX_NAME>',query: '<YOUR_QUERY>',},],});const analyticsRes = await analyticsClient.getTopFilterForAttribute({attribute: 'myAttribute1,myAttribute2',index: '<YOUR_INDEX_NAME>',});console.log('[Results]', searchRes, analyticsRes);
js
import { searchClient } from '@experimental-api-clients-automation/client-search';const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>');
js
import { searchClient } from '@experimental-api-clients-automation/client-search';const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>');
It is possible to customize the client by passing optional parameters:
js
const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>', {requester: customRequester(),hosts: [{url: 'my.custom.host.net',accept: 'read',protocol: 'https',},],});
js
const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>', {requester: customRequester(),hosts: [{url: 'my.custom.host.net',accept: 'read',protocol: 'https',},],});
Once the client is setup, you can play with the Algolia API!
js
const res = await client.search({requests: [{indexName: '<YOUR_INDEX_NAME>',query: '<YOUR_QUERY>',},],});console.log('[Results]', res);
js
const res = await client.search({requests: [{indexName: '<YOUR_INDEX_NAME>',query: '<YOUR_QUERY>',},],});console.log('[Results]', res);
Or with the personalization
client
js
const res = await personalizationClient.getUserTokenProfile({userToken: 'token',});console.log('[Results]', res);
js
const res = await personalizationClient.getUserTokenProfile({userToken: 'token',});console.log('[Results]', res);
Then, create objects on your index:
php
$client = Algolia\AlgoliaSearch\Api\SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');$client->saveObject('<YOUR_INDEX_NAME>', ['objectID' => 1, 'name' => 'Foo']);
php
$client = Algolia\AlgoliaSearch\Api\SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');$client->saveObject('<YOUR_INDEX_NAME>', ['objectID' => 1, 'name' => 'Foo']);
Finally, you may begin searching an object using the search
method:
php
$client->search(['requests' => [['indexName' => '<YOUR_INDEX_NAME>', 'query' => '<YOUR_QUERY>'],],])
php
$client->search(['requests' => [['indexName' => '<YOUR_INDEX_NAME>', 'query' => '<YOUR_QUERY>'],],])
Another example with the personalization client:
php
$client = Algolia\AlgoliaSearch\Api\PersonalizationClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');$res = $client->getUserTokenProfile('<YOUR_TOKEN>');
php
$client = Algolia\AlgoliaSearch\Api\PersonalizationClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');$res = $client->getUserTokenProfile('<YOUR_TOKEN>');
The package is divided into multiples API Clients, you can find the list here. To instanciate a client, prepare your credentials and follow this example:
java
import com.algolia.api.SearchClient;public class AlgoliaTest {public static void main(String[] args) {SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");}}
java
import com.algolia.api.SearchClient;public class AlgoliaTest {public static void main(String[] args) {SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");}}
You can add segments to the User Agent
:
java
import com.algolia.utils.AlgoliaAgent;SearchClient client = new SearchClient("<YOUR_APP_ID>","<YOUR_API_KEY>",new AlgoliaAgent.Segment[] {new AlgoliaAgent.Segment("tracker", "8.0.0")});
java
import com.algolia.utils.AlgoliaAgent;SearchClient client = new SearchClient("<YOUR_APP_ID>","<YOUR_API_KEY>",new AlgoliaAgent.Segment[] {new AlgoliaAgent.Segment("tracker", "8.0.0")});
And also specify a custom Requester
:
java
import com.algolia.utils.echo.EchoRequester;SearchClient client = new SearchClient("<YOUR_APP_ID>","<YOUR_API_KEY>",new EchoRequester());
java
import com.algolia.utils.echo.EchoRequester;SearchClient client = new SearchClient("<YOUR_APP_ID>","<YOUR_API_KEY>",new EchoRequester());
Or use a completely different client (some clients might require a region
parameter):
java
import com.algolia.api.AbtestingClient;AbtestingClient client = new AbtestingClient("<YOUR_APP_ID>", "<YOUR_API_KEY>", "us");
java
import com.algolia.api.AbtestingClient;AbtestingClient client = new AbtestingClient("<YOUR_APP_ID>", "<YOUR_API_KEY>", "us");
Once the client is setup, you can play with the Algolia API!
java
import com.algolia.model.search.*;SearchMethodParams searchMethodParams = new SearchMethodParams();SearchQueries requests = new SearchQueries();requests.setIndexName("<YOUR_INDEX_NAME>");requests.setQuery("<YOUR_QUERY>");searchMethodParams.setRequests(requests);try {SearchResponse result = client.search(searchMethodParams);System.out.println(result);} catch (AlgoliaRuntimeException e) {e.printStackTrace();}
java
import com.algolia.model.search.*;SearchMethodParams searchMethodParams = new SearchMethodParams();SearchQueries requests = new SearchQueries();requests.setIndexName("<YOUR_INDEX_NAME>");requests.setQuery("<YOUR_QUERY>");searchMethodParams.setRequests(requests);try {SearchResponse result = client.search(searchMethodParams);System.out.println(result);} catch (AlgoliaRuntimeException e) {e.printStackTrace();}