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.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Scala
To get started, you first need to install algoliasearch
(or any other available API client package).
All of our clients comes with type definition, and are available for both browser
and node
environments.
yarn add algoliasearch@alpha
# or
npm install algoliasearch@alpha
Or use a specific package:
yarn add @algolia/client-search@alpha
# or
npm install @algolia/client-search@alpha
Without a package manager
Add the following JavaScript snippet to the <head>
of your website:
<script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch.umd.js"></script>
First, install Algolia Python API Client via the pip package manager:
pip install --upgrade 'algoliasearch>=4.0,<5.0'
First, install Algolia PHP API Client via the composer package manager:
composer require algolia/algoliasearch-client-php
To get started, add the algoliasearch-client-java dependency to your project, either with Maven:
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch</artifactId>
<version>4.0.0-beta.19</version>
</dependency>
or Gradle:
dependencies {
implementation 'com.algolia:algoliasearch:4.0.0-beta.19'
}
JVM
- Install the Kotlin client by adding the following dependency to your
gradle.build
file:
repositories {
mavenCentral()
}
dependencies {
implementation "com.algolia:algoliasearch-client-kotlin:3.0.0-beta.13"
}
- Choose and add to your dependencies one of Ktor's engines.
-
BOM
Alternatively, you can use
algoliasearch-client-kotlin-bom
by adding the following dependency to yourbuild.gradle
file
dependencies {
// import Kotlin API client BOM
implementation platform("com.algolia:algoliasearch-client-kotlin-bom:3.0.0-beta.13}")
// define dependencies without versions
implementation 'com.algolia:algoliasearch-client-kotlin'
runtimeOnly 'io.ktor:ktor-client-okhttp'
}
Multiplaform
In multiplatform projects, add Algolia API client dependency to commonMain
, and choose an engine for each target.
First, install the Algolia API Go Client via the go get
command:
go get github.com/algolia/algoliasearch-client-go/v4
Using the client
You can now import the Algolia API client in your project and play with it.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Scala
import { algoliasearch } from 'algoliasearch';
// Instantiate the client
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');
// Add a new record to your Algolia index
const { taskID } = await client.saveObject({
indexName: '<YOUR_INDEX_NAME>',
body: {
title: 'My Algolia Object',
},
});
// Poll the task status to know when it has been indexed
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });
// Fetch search results
const { results } = await client.search({
requests: [
{
indexName: '<YOUR_INDEX_NAME>',
// You can make typos, we handle it
query: 'my aloglia ojbect',
hitsPerPage: 50,
},
],
});
console.log('[Results]', results);
from algoliasearch.search.client import SearchClient
client = SearchClient("YOUR_APP_ID", "YOUR_API_KEY")
Add new object to your index:
save_resp = await client.save_object(index_name="nvim", body={"description": "blazing fast"})
Wait for the task to be processed on the Algolia side
await client.wait_for_task(index_name="nvim", task_id=save_resp.task_id)
Finally, you may begin searching a object using the search
method:
# using a raw dict
search_resp = await client.search(search_method_params={"requests": [{"indexName": "nvim"}]})
# using the given models
from algoliasearch.search.models.search_method_params import SearchMethodParams
from algoliasearch.search.models.search_for_hits import SearchForHits
from algoliasearch.search.models.search_query import SearchQuery
search_resp = await client.search(
search_method_params=SearchMethodParams(
requests=[
SearchQuery(SearchForHits(index_name="nvim")),
],
),
)
print(search_resp.to_json())
// 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 results
var_dump(
$client->search([
'requests' => [
['indexName' => '<YOUR_INDEX_NAME>', 'query' => 'my aloglia ojbect'],
],
])
);
import com.algolia.api.SearchClient;
import com.algolia.model.search.*;
import java.util.*;
// Instantiate the client
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");
// Add a new record to your Algolia index
Map<String, String> body = new HashMap<>();
body.put("objectID", "id1");
body.put("title", "My Algolia Object");
client.saveObject("<YOUR_INDEX_NAME>", body);
// Fetch search results
SearchForHits request = new SearchForHits()
.setIndexName("<YOUR_INDEX_NAME>")
.setQuery("my aloglia ojbect");
SearchMethodParams params = new SearchMethodParams().addRequests(request);
SearchResponses responses = client.search(params);
System.out.println(responses);
import com.algolia.client.api.SearchClient
import com.algolia.client.model.search.*
import kotlinx.serialization.json.*
// Instantiate the client
val client = SearchClient(appId = "<YOUR_APP_ID>", apiKey = "<YOUR_API_KEY>")
// Add a new record to your Algolia index
val record = buildJsonObject {
put("objectID", "id1")
put("title", "My Algolia Object")
}
client.saveObject(indexName = "<YOUR_INDEX_NAME>", body = record)
// Fetch search results
val response = client.search(
searchMethodParams = SearchMethodParams(
requests = listOf(
SearchForHits(
indexName = "<YOUR_INDEX_NAME>",
query = "<YOUR_QUERY>" // Typo tolerant search
),
),
)
)
println(response)
import 'package:algolia_client_search/algolia_client_search.dart';
void main() async {
// Instantiate the client
var client = SearchClient(appId: '<YOUR_APP_ID>', apiKey: '<YOUR_API_KEY>');
// Add a new record to your Algolia index.
// Can also be an instance of a class with a `toJson()` method.
var record = {
'objectID': 'id1',
'title': 'My Algolia Object',
};
await client.saveObject(indexName: '<YOUR_INDEX_NAME>', body: record);
// Fetch search results
var response = await client.search(
searchMethodParams: SearchMethodParams(
requests: [
SearchForHits(
indexName: '<YOUR_INDEX_NAME>',
query: '<YOUR_QUERY>', // Typo tolerant search
hitsPerPage: 50,
),
],
),
);
print(response);
// Close the client and dispose of all underlying resources.
client.dispose();
}
package main
import (
"fmt"
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
func main() {
// Instantiate the client
searchClient, _ := search.NewClient("<YOUR_APP_ID>", "<YOUR_API_KEY>")
// Add a new record to your Algolia index.
response, err := searchClient.AddOrUpdateObject(
searchClient.NewApiAddOrUpdateObjectRequest(
"<YOUR_INDEX_NAME>",
"1",
map[string]interface{}{
"name": "Foo",
"age": 42,
"city": "Paris",
},
),
)
if err != nil {
panic(err)
}
// Wait for the save operation to complete.
_, err = searchClient.WaitForTask(
indexName,
*response.TaskID,
nil,
nil,
nil,
)
if err != nil {
panic(err)
}
// Fetch search results.
searchResponse, err := searchClient.Search(
searchClient.NewApiSearchRequest(
search.NewSearchMethodParams(
[]search.SearchQuery{
search.SearchForHitsAsSearchQuery(
search.NewSearchForHits(
indexName,
search.WithSearchForHitsQuery("<YOUR_SEARCH_QUERY>"), // Typo tolerant search.
),
),
},
),
),
)
if err != nil {
panic(err)
}
for _, result := range searchResponse.Results {
fmt.Printf("Result: %v", result.SearchResponse)
}
}
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.