-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from algolia-api-client-bot/master
Update README
- Loading branch information
Showing
1 changed file
with
220 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,10 @@ The **Algolia Search API Client for PHP** lets you easily use the [Algolia Searc | |
[![Build Status](https://travis-ci.org/algolia/algoliasearch-client-php.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-client-php) [![Latest Stable Version](https://poser.pugx.org/algolia/algoliasearch-client-php/v/stable.svg)](https://packagist.org/packages/algolia/algoliasearch-client-php) [![Coverage Status](https://coveralls.io/repos/algolia/algoliasearch-client-php/badge.svg)](https://coveralls.io/r/algolia/algoliasearch-client-php) | ||
|
||
|
||
If you're a Symfony or Laravel user, you're probably looking for the following integrations: | ||
|
||
- **Laravel**: [Laravel Scout](/doc/api-client/laravel/algolia-and-scout/) | ||
- **Symfony**: [algolia/AlgoliaSearchBundle](https://github.com/algolia/AlgoliaSearchBundle) | ||
|
||
|
||
|
||
|
@@ -18,22 +22,26 @@ You can find the full reference on [Algolia's website](https://www.algolia.com/d | |
## Table of Contents | ||
|
||
|
||
|
||
1. **[Install](#install)** | ||
|
||
* [With composer (Recommended)](#with-composer-recommended) | ||
* [Without composer](#without-composer) | ||
* [Framework Integrations](#framework-integrations) | ||
|
||
1. **[Quick Start](#quick-start)** | ||
|
||
* [Initialize the client](#initialize-the-client) | ||
* [Push data](#push-data) | ||
* [Search](#search) | ||
* [Configure](#configure) | ||
* [Frontend search](#frontend-search) | ||
|
||
1. **[Getting Help](#getting-help)** | ||
1. **[Push data](#push-data)** | ||
|
||
|
||
1. **[Configure](#configure)** | ||
|
||
|
||
1. **[Search](#search)** | ||
|
||
|
||
1. **[Search UI](#search-ui)** | ||
|
||
|
||
1. **[List of available methods](#list-of-available-methods)** | ||
|
||
|
||
|
||
|
@@ -42,6 +50,7 @@ You can find the full reference on [Algolia's website](https://www.algolia.com/d | |
|
||
|
||
|
||
|
||
## Install | ||
|
||
### With composer (Recommended) | ||
|
@@ -62,68 +71,54 @@ require_once('algoliasearch-client-php-master/algoliasearch.php'); | |
|
||
### Framework Integrations | ||
|
||
If you're a Symfony or Laravel user, you're probably looking for the following integrations | ||
We officially provide support for the **Laravel** and **Symfony** frameworks: | ||
|
||
- **Laravel**: [algolia/algoliasearch-laravel](https://github.com/algolia/algoliasearch-laravel) | ||
- **Symfony**: [algolia/AlgoliaSearchBundle](https://github.com/algolia/AlgoliaSearchBundle) | ||
If you are using one of those two frameworks have a look at our | ||
[Laravel documentation](https://www.algolia.com/doc/api-client/laravel/algolia-and-scout/) or [Symfony documentation](https://www.algolia.com/doc/api-client/symfony/setup/) | ||
|
||
## Quick Start | ||
|
||
In 30 seconds, this quick start tutorial will show you how to index and search objects. | ||
|
||
### Initialize the client | ||
|
||
You first need to initialize the client. For that you need your **Application ID** and **API Key**. | ||
You can find both of them on [your Algolia account](https://www.algolia.com/api-keys). | ||
To begin, you will need to initialize the client. In order to do this you will need your **Application ID** and **API Key**. | ||
You can find both on [your Algolia account](https://www.algolia.com/api-keys). | ||
|
||
```php | ||
// composer autoload | ||
require __DIR__ . '/vendor/autoload.php'; | ||
// if you are not using composer: require_once 'path/to/algoliasearch.php'; | ||
|
||
$client = new \AlgoliaSearch\Client('YourApplicationID', 'YourAPIKey'); | ||
// if you are not using composer | ||
// require_once 'path/to/algoliasearch.php'; | ||
|
||
$client = new \AlgoliaSearch\Client('YourApplicationID', 'YourAdminAPIKey'); | ||
|
||
$index = $client->initIndex('your_index_name'); | ||
``` | ||
|
||
### Push data | ||
## Push data | ||
|
||
Without any prior configuration, you can start indexing [500 contacts](https://github.com/algolia/algoliasearch-client-php/blob/master/contacts.json) in the ```contacts``` index using the following code: | ||
Without any prior configuration, you can start indexing [500 contacts](https://github.com/algolia/datasets/blob/master/contacts/contacts.json) in the ```contacts``` index using the following code: | ||
|
||
```php | ||
$index = $client->initIndex('contacts'); | ||
$batch = json_decode(file_get_contents('contacts.json'), true); | ||
$index->addObjects($batch); | ||
``` | ||
|
||
### Search | ||
|
||
You can now search for contacts using firstname, lastname, company, etc. (even with typos): | ||
|
||
```php | ||
// search by firstname | ||
var_dump($index->search('jimmie')); | ||
|
||
// search a firstname with typo | ||
var_dump($index->search('jimie')); | ||
|
||
// search for a company | ||
var_dump($index->search('california paint')); | ||
|
||
// search for a firstname & company | ||
var_dump($index->search('jimmie paint')); | ||
``` | ||
|
||
### Configure | ||
## Configure | ||
|
||
Settings can be customized to tune the search behavior. For example, you can add a custom sort by number of followers to the already great built-in relevance: | ||
Settings can be customized to fine tune the search behavior. For example, you can add a custom sort by number of followers to further enhance the built-in relevance: | ||
|
||
```php | ||
$index->setSettings(['customRanking' => ['desc(followers)']]); | ||
``` | ||
|
||
You can also configure the list of attributes you want to index by order of importance (first = most important): | ||
You can also configure the list of attributes you want to index by order of importance (most important first). | ||
|
||
**Note:** Since the engine is designed to suggest results as you type, you'll generally search by prefix. | ||
In this case the order of attributes is very important to decide which hit is the best: | ||
**Note:** The Algolia engine is designed to suggest results as you type, which means you'll generally search by prefix. | ||
In this case, the order of attributes is very important to decide which hit is the best: | ||
|
||
```php | ||
$index->setSettings( | ||
|
@@ -140,48 +135,202 @@ $index->setSettings( | |
); | ||
``` | ||
|
||
### Frontend search | ||
## Search | ||
|
||
**Note:** If you are building a web application, you may be more interested in using our [JavaScript client](https://github.com/algolia/algoliasearch-client-javascript) to perform queries. | ||
You can now search for contacts using `firstname`, `lastname`, `company`, etc. (even with typos): | ||
|
||
It brings two benefits: | ||
* Your users get a better response time by not going through your servers | ||
* It will offload unnecessary tasks from your servers | ||
```php | ||
// search by firstname | ||
var_dump($index->search('jimmie')); | ||
|
||
// search a firstname with typo | ||
var_dump($index->search('jimie')); | ||
|
||
// search for a company | ||
var_dump($index->search('california paint')); | ||
|
||
// search for a firstname & company | ||
var_dump($index->search('jimmie paint')); | ||
``` | ||
|
||
## Search UI | ||
|
||
**Warning:** If you are building a web application, you may be more interested in using one of our | ||
[frontend search UI libraries](https://www.algolia.com/doc/guides/search-ui/search-libraries/) | ||
|
||
The following example shows how to build a front-end search quickly using | ||
[InstantSearch.js](https://community.algolia.com/instantsearch.js/) | ||
|
||
### index.html | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script> | ||
<script> | ||
var client = algoliasearch('ApplicationID', 'apiKey'); | ||
var index = client.initIndex('indexName'); | ||
// perform query "jim" | ||
index.search('jim', searchCallback); | ||
// the last optional argument can be used to add search parameters | ||
index.search( | ||
'jim', { | ||
hitsPerPage: 5, | ||
facets: '*', | ||
maxValuesPerFacet: 10 | ||
}, | ||
searchCallback | ||
); | ||
<!doctype html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.css"> | ||
<!-- Always use `2.x` versions in production rather than `2` to mitigate any side effects on your website, | ||
Find the latest version on InstantSearch.js website: https://community.algolia.com/instantsearch.js/v2/guides/usage.html --> | ||
</head> | ||
<body> | ||
<header> | ||
<div> | ||
<input id="search-input" placeholder="Search for products"> | ||
<!-- We use a specific placeholder in the input to guides users in their search. --> | ||
|
||
</header> | ||
<main> | ||
|
||
|
||
</main> | ||
|
||
<script type="text/html" id="hit-template"> | ||
|
||
<p class="hit-name">{{{_highlightResult.firstname.value}}} {{{_highlightResult.lastname.value}}}</p> | ||
|
||
</script> | ||
|
||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.min.js"></script> | ||
<script src="app.js"></script> | ||
</body> | ||
``` | ||
|
||
function searchCallback(err, content) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
### app.js | ||
|
||
```js | ||
var search = instantsearch({ | ||
// Replace with your own values | ||
appId: 'YourApplicationID', | ||
apiKey: 'YourSearchOnlyAPIKey', // search only API key, no ADMIN key | ||
indexName: 'contacts', | ||
urlSync: true, | ||
searchParameters: { | ||
hitsPerPage: 10 | ||
} | ||
}); | ||
|
||
search.addWidget( | ||
instantsearch.widgets.searchBox({ | ||
container: '#search-input' | ||
}) | ||
); | ||
|
||
console.log(content); | ||
} | ||
</script> | ||
search.addWidget( | ||
instantsearch.widgets.hits({ | ||
container: '#hits', | ||
templates: { | ||
item: document.getElementById('hit-template').innerHTML, | ||
empty: "We didn't find any results for the search <em>\"{{query}}\"</em>" | ||
} | ||
}) | ||
); | ||
|
||
search.start(); | ||
``` | ||
|
||
|
||
|
||
|
||
## List of available methods | ||
|
||
|
||
|
||
|
||
|
||
### Search | ||
|
||
- [Search an index](https://algolia.com/doc/api-reference/api-methods/search/?language=php) | ||
- [Search for facet values](https://algolia.com/doc/api-reference/api-methods/search-for-facet-values/?language=php) | ||
- [Search multiple indexes](https://algolia.com/doc/api-reference/api-methods/multiple-queries/?language=php) | ||
- [Browse an index](https://algolia.com/doc/api-reference/api-methods/browse/?language=php) | ||
|
||
|
||
|
||
### Indexing | ||
|
||
- [Add objects](https://algolia.com/doc/api-reference/api-methods/add-objects/?language=php) | ||
- [Update objects](https://algolia.com/doc/api-reference/api-methods/update-objects/?language=php) | ||
- [Partial update objects](https://algolia.com/doc/api-reference/api-methods/partial-update-objects/?language=php) | ||
- [Delete objects](https://algolia.com/doc/api-reference/api-methods/delete-objects/?language=php) | ||
- [Delete by query](https://algolia.com/doc/api-reference/api-methods/delete-by-query/?language=php) | ||
- [Get objects](https://algolia.com/doc/api-reference/api-methods/get-objects/?language=php) | ||
- [Custom batch](https://algolia.com/doc/api-reference/api-methods/batch/?language=php) | ||
- [Wait for operations](https://algolia.com/doc/api-reference/api-methods/wait-task/?language=php) | ||
|
||
|
||
|
||
### Settings | ||
|
||
- [Get settings](https://algolia.com/doc/api-reference/api-methods/get-settings/?language=php) | ||
- [Set settings](https://algolia.com/doc/api-reference/api-methods/set-settings/?language=php) | ||
|
||
|
||
|
||
### Manage indices | ||
|
||
- [List indexes](https://algolia.com/doc/api-reference/api-methods/list-indices/?language=php) | ||
- [Delete index](https://algolia.com/doc/api-reference/api-methods/delete-index/?language=php) | ||
- [Copy index](https://algolia.com/doc/api-reference/api-methods/copy-index/?language=php) | ||
- [Move index](https://algolia.com/doc/api-reference/api-methods/move-index/?language=php) | ||
- [Clear index](https://algolia.com/doc/api-reference/api-methods/clear-index/?language=php) | ||
|
||
|
||
|
||
### API Keys | ||
|
||
- [Create secured API Key](https://algolia.com/doc/api-reference/api-methods/generate-secured-api-key/?language=php) | ||
- [Add API Key](https://algolia.com/doc/api-reference/api-methods/add-api-key/?language=php) | ||
- [Update API Key](https://algolia.com/doc/api-reference/api-methods/update-api-key/?language=php) | ||
- [Delete API Key](https://algolia.com/doc/api-reference/api-methods/delete-api-key/?language=php) | ||
- [Get API Key permissions](https://algolia.com/doc/api-reference/api-methods/get-api-key/?language=php) | ||
- [List API Keys](https://algolia.com/doc/api-reference/api-methods/list-api-keys/?language=php) | ||
|
||
|
||
|
||
### Synonyms | ||
|
||
- [Save synonym](https://algolia.com/doc/api-reference/api-methods/save-synonym/?language=php) | ||
- [Batch synonyms](https://algolia.com/doc/api-reference/api-methods/batch-synonyms/?language=php) | ||
- [Delete synonym](https://algolia.com/doc/api-reference/api-methods/delete-synonym/?language=php) | ||
- [Clear all synonyms](https://algolia.com/doc/api-reference/api-methods/clear-synonyms/?language=php) | ||
- [Get synonym](https://algolia.com/doc/api-reference/api-methods/get-synonym/?language=php) | ||
- [Search synonyms](https://algolia.com/doc/api-reference/api-methods/search-synonyms/?language=php) | ||
|
||
|
||
|
||
### Query rules | ||
|
||
- [Save a single rule](https://algolia.com/doc/api-reference/api-methods/rules-save/?language=php) | ||
- [Batch save multiple rules](https://algolia.com/doc/api-reference/api-methods/rules-save-batch/?language=php) | ||
- [Read a rule](https://algolia.com/doc/api-reference/api-methods/rules-read/?language=php) | ||
- [Delete a single rule](https://algolia.com/doc/api-reference/api-methods/rules-delete/?language=php) | ||
- [Clear all rules](https://algolia.com/doc/api-reference/api-methods/rules-clear/?language=php) | ||
- [Search for rules](https://algolia.com/doc/api-reference/api-methods/rules-search/?language=php) | ||
|
||
|
||
|
||
### MultiClusters | ||
|
||
- [Assign or Move userID](https://algolia.com/doc/api-reference/api-methods/assign-user-id/?language=php) | ||
- [Get top userID](https://algolia.com/doc/api-reference/api-methods/get-top-user-id/?language=php) | ||
- [Get userID](https://algolia.com/doc/api-reference/api-methods/get-user-id/?language=php) | ||
- [List clusters](https://algolia.com/doc/api-reference/api-methods/list-clusters/?language=php) | ||
- [List userID](https://algolia.com/doc/api-reference/api-methods/list-user-id/?language=php) | ||
- [Remove userID](https://algolia.com/doc/api-reference/api-methods/remove-user-id/?language=php) | ||
- [Search userID](https://algolia.com/doc/api-reference/api-methods/search-user-id/?language=php) | ||
|
||
|
||
|
||
### Advanced | ||
|
||
- [Get latest logs](https://algolia.com/doc/api-reference/api-methods/get-logs/?language=php) | ||
- [Set extra header](https://algolia.com/doc/api-reference/api-methods/set-extra-header/?language=php) | ||
|
||
|
||
|
||
|
||
## Getting Help | ||
|
||
- **Need help**? Ask a question to the [Algolia Community](https://discourse.algolia.com/) or on [Stack Overflow](http://stackoverflow.com/questions/tagged/algolia). | ||
- **Found a bug?** You can open a [GitHub issue](https://github.com/algolia/algoliasearch-client-php/issues). | ||
|
||
|
||
|