Installation
The amount of changes in this new version is significant. If you are upgrading for v4, you should thoroughly test your application before deploying to production.
- 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 algoliasearch@alpha# ornpm install algoliasearch@alpha
bash
yarn add algoliasearch@alpha# ornpm install algoliasearch@alpha
Or use a specific package:
bash
yarn add @algolia/client-search@alpha# ornpm install @algolia/client-search@alpha
bash
yarn add @algolia/client-search@alpha# ornpm install @algolia/client-search@alpha
Without a package manager
Add the following JavaScript snippet to the <head>
of your website:
html
<script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch.umd.js"></script>
html
<script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch.umd.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
<dependency><groupId>com.algolia</groupId><artifactId>algoliasearch-client-java</artifactId><version>4.0.0-beta.1</version></dependency>
xml
<dependency><groupId>com.algolia</groupId><artifactId>algoliasearch-client-java</artifactId><version>4.0.0-beta.1</version></dependency>
or Gradle:
groovy
dependencies {testImplementation 'com.algolia:algoliasearch-client-java:4.0.0-beta.1'}
groovy
dependencies {testImplementation 'com.algolia:algoliasearch-client-java:4.0.0-beta.1'}
Using the client
You can now import the Algolia API client in your project and play with it.
- JavaScript
- PHP
- Java
js
import { algoliasearch } from 'algoliasearch';// Instantiate the clientconst client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');// Add a new record to your Algolia indexconst { taskID } = await client.saveObject({indexName: '<YOUR_INDEX_NAME>',body: {title: 'My Algolia Object',},});// Poll the task status to know when it has been indexedawait client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });// Fetch search resultsconst { results } = await client.search({requests: [{indexName: '<YOUR_INDEX_NAME>',// You can make typos, we handle itquery: 'my aloglia ojbect',hitsPerPage: 50,},],});console.log('[Results]', results);
js
import { algoliasearch } from 'algoliasearch';// Instantiate the clientconst client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');// Add a new record to your Algolia indexconst { taskID } = await client.saveObject({indexName: '<YOUR_INDEX_NAME>',body: {title: 'My Algolia Object',},});// Poll the task status to know when it has been indexedawait client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });// Fetch search resultsconst { results } = await client.search({requests: [{indexName: '<YOUR_INDEX_NAME>',// You can make typos, we handle itquery: 'my aloglia ojbect',hitsPerPage: 50,},],});console.log('[Results]', results);
php
// Instantiate the client$client = Algolia\AlgoliaSearch\Api\SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');// Add a new record to your Algolia index$client->saveObject('<YOUR_INDEX_NAME>',['objectID' => 'id', 'title' => 'My Algolia Object']);// Fetch search resultsvar_dump($client->search(['requests' => [['indexName' => '<YOUR_INDEX_NAME>', 'query' => 'my aloglia ojbect'],],]));
php
// Instantiate the client$client = Algolia\AlgoliaSearch\Api\SearchClient::create('<YOUR_APP_ID>','<YOUR_API_KEY>');// Add a new record to your Algolia index$client->saveObject('<YOUR_INDEX_NAME>',['objectID' => 'id', 'title' => 'My Algolia Object']);// Fetch search resultsvar_dump($client->search(['requests' => [['indexName' => '<YOUR_INDEX_NAME>', 'query' => 'my aloglia ojbect'],],]));
java
import com.algolia.model.search.*;import java.util.*;// Instantiate the clientSearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");// Add a new record to your Algolia indexMap<String, String> body = new HashMap<>();body.put("objectID", "id1");body.put("title", "My Algolia Object");client.saveObject("<YOUR_INDEX_NAME>", body);// Fetch search resultsSearchMethodParams searchMethodParams = new SearchMethodParams();SearchQueries requests = new SearchQueries();requests.setIndexName("<YOUR_INDEX_NAME>");// Typo tolerant searchrequests.setQuery("my aloglia ojbect");searchMethodParams.setRequests(requests);try {SearchResponse<MyObject> result = client.search(searchMethodParams,MyObject.class);System.out.println(result);} catch (AlgoliaRuntimeException e) {e.printStackTrace();}
java
import com.algolia.model.search.*;import java.util.*;// Instantiate the clientSearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");// Add a new record to your Algolia indexMap<String, String> body = new HashMap<>();body.put("objectID", "id1");body.put("title", "My Algolia Object");client.saveObject("<YOUR_INDEX_NAME>", body);// Fetch search resultsSearchMethodParams searchMethodParams = new SearchMethodParams();SearchQueries requests = new SearchQueries();requests.setIndexName("<YOUR_INDEX_NAME>");// Typo tolerant searchrequests.setQuery("my aloglia ojbect");searchMethodParams.setRequests(requests);try {SearchResponse<MyObject> result = client.search(searchMethodParams,MyObject.class);System.out.println(result);} catch (AlgoliaRuntimeException e) {e.printStackTrace();}
Advanced use cases
If you don't find a use case that suits your needs, please request it.
You can learn more on how to use Algolia in your project by reading our dedicated guides for advanced use cases.