File tree Expand file tree Collapse file tree 12 files changed +157
-28
lines changed
templates/php/guides/search Expand file tree Collapse file tree 12 files changed +157
-28
lines changed Original file line number Diff line number Diff line change 22
33require __DIR__.'/../vendor/autoload.php';
44{ {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\Action;
6+ use Algolia\AlgoliaSearch\Model\Search\MultipleBatchRequest;
57
68try {
79 // You need an API key with `deleteIndex`
2224 // Delete primary indices first
2325 if (!empty($primaryIndices)) {
2426 $requests = array_map(function ($index ) {
25- return [
26- ' action' => ' delete' ,
27- ' indexName' => $index [' name' ],
28- ];
27+ return (new MultipleBatchRequest())
28+ -> setAction ((new Action())::DELETE)
29+ -> setIndexName ($index [' name' ]);
2930 } , $primaryIndices);
3031
3132 { {#dynamicSnippet} }deleteMultipleIndicesPrimary{ {/dynamicSnippet} };
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ require __DIR__ . '/../vendor/autoload.php';
4+
5+ use Algolia\AlgoliaSearch\Model\Search\Anchoring;
6+ use Algolia\AlgoliaSearch\Model\Search\Condition;
7+ use Algolia\AlgoliaSearch\Model\Search\Consequence;
8+ use Algolia\AlgoliaSearch\Model\Search\Rule;
9+
10+ $condition = (new Condition())
11+ ->setAnchoring((new Anchoring())::IS)
12+ ->setPattern('{ facet:brand} ');
13+
14+ $consequence = (new Consequence())
15+ ->setFilterPromotes(true);
16+
17+ $rule = (new Rule())
18+ ->setObjectID('rule_with_filterPromotes')
19+ ->setConditions([$condition])
20+ ->setConsequence($consequence);
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ require __DIR__ . '/../vendor/autoload.php';
4+ { {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
6+
7+ try {
8+ {{> snippets/init} }
9+
10+ $query = 'query';
11+
12+ // 1. Change the sort dynamically based on the UI events
13+ $sortByPrice = false;
14+
15+ // 2. Get the index name based on sortByPrice
16+ $indexName = $sortByPrice ? 'products_price_desc' : 'products';
17+
18+ // 3. Search on dynamic index name (primary or replica)
19+ { {#dynamicSnippet} }searchWithIndexNameVar{ {/dynamicSnippet} };
20+ } catch (Exception $e) {
21+ echo $e -> getMessage () . PHP_EOL;
22+ }
23+
Original file line number Diff line number Diff line change 22
33require __DIR__.'/../vendor/autoload.php';
44{ {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56
67try {
78 {{> snippets/init} }
89
910 $dateTimestamp = time();
10- $searchParams = [
11- 'query' => '<YOUR _SEARCH_QUERY >',
12- 'filters' => "date_timestamp > $dateTimestamp",
13- ];
11+ $searchParams = (new SearchParamsObject())
12+ ->setQuery('<YOUR _SEARCH_QUERY >')
13+ ->setFilters("date_timestamp > " . $dateTimestamp);
1414
1515 { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
1616} catch (Exception $e) {
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ require __DIR__ . '/../vendor/autoload.php';
4+ { {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
6+
7+ try {
8+ {{> snippets/init} }
9+
10+ /*
11+ '94.228.178.246' should be replaced with your user's IP address.
12+ Depending on your stack there are multiple ways to get this information.
13+ */
14+ $ip = '94.228.178.246';
15+ $query = 'query';
16+
17+ $searchParams = (new SearchParamsObject())
18+ ->setQuery($query)
19+ ->setAnalytics(true);
20+
21+ { {#dynamicSnippet} }searchWithSearchParamsAndForwardedHeader{ {/dynamicSnippet} };
22+ } catch (Exception $e) {
23+ echo $e -> getMessage () . PHP_EOL;
24+ }
25+
Original file line number Diff line number Diff line change 22
33require __DIR__.'/../vendor/autoload.php';
44{ {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56
67$getGoogleAnalyticsUserIdFromBrowserCookie = function (string $cookieName): string {
78 // Implement your logic here
@@ -12,17 +13,16 @@ try {
1213 {{> snippets/init} }
1314
1415 $userToken = $getGoogleAnalyticsUserIdFromBrowserCookie('_ga');
15- $searchParams = [
16- 'query' => '<YOUR _SEARCH_QUERY >',
17- 'userToken' => $userToken,
18- ];
16+ $searchParams = (new SearchParamsObject())
17+ ->setQuery('<YOUR _SEARCH_QUERY >')
18+ ->setUserToken($userToken);
1919
2020 { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
2121
2222 /** @var string|null $loggedInUser */
2323 $loggedInUser = null;
2424
25- $searchParams['userToken'] = $loggedInUser ?? $userToken;
25+ $searchParams->setUserToken( $loggedInUser ?? $userToken) ;
2626
2727 { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
2828} catch (Exception $e) {
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ require __DIR__ . '/../vendor/autoload.php';
4+ { {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\OptionalWords;
6+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
7+
8+ try {
9+ {{> snippets/init} }
10+
11+ $query = 'the query';
12+
13+ $optionalWords = new OptionalWords();
14+ $optionalWords[] = $query;
15+
16+ $searchParams = (new SearchParamsObject())
17+ ->setQuery($query)
18+ ->setOptionalWords($optionalWords);
19+
20+ { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
21+ } catch (Exception $e) {
22+ echo $e -> getMessage () . PHP_EOL;
23+ }
24+
Original file line number Diff line number Diff line change 22
33require __DIR__.'/../vendor/autoload.php';
44{ {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\OptionalFilters;
6+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
57
68$labels = []; // A list of labels
7- $reduceLabelsToFilters = function (array $labels): array {
9+ $reduceLabelsToFilters = function (array $labels): OptionalFilters {
810 // Implement your logic here
9- return [] ;
11+ return new OptionalFilters() ;
1012} ;
1113
1214try {
1315 {{> snippets/init} }
1416
1517 $optionalFilters = $reduceLabelsToFilters($labels);
16- $searchParams = [
17- 'query' => '<YOUR _SEARCH_QUERY >',
18- 'optionalFilters' => $optionalFilters,
19- ];
18+ $searchParams = (new SearchParamsObject())
19+ ->setQuery('<YOUR _SEARCH_QUERY >')
20+ ->setOptionalFilters($optionalFilters);
2021
2122 { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
2223} catch (Exception $e) {
Original file line number Diff line number Diff line change 22
33require __DIR__.'/../vendor/autoload.php';
44{ {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56
67$getBuyerAccountId = function (): string {
78 // Implement your logic here
1314
1415 // get the buyer account information
1516 $buyer = $getBuyerAccountId();
16- $searchParams = [
17- 'query' => '<YOUR _SEARCH_QUERY >',
18- 'ruleContexts' => [$buyer],
19- ];
17+ $searchParams = (new SearchParamsObject())
18+ ->setQuery('<YOUR _SEARCH_QUERY >')
19+ ->setRuleContexts([$buyer]);
2020
2121 { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
2222} catch (Exception $e) {
Original file line number Diff line number Diff line change 22
33require __DIR__.'/../vendor/autoload.php';
44{ {> snippets/import} }
5+ use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56
67$getPlatformTag = function (): string {
78 // Implement your logic here
1314
1415 // get the buyer account information
1516 $platformTag = $getPlatformTag();
16- $searchParams = [
17- 'query' => '<YOUR _SEARCH_QUERY >',
18- 'ruleContexts' => [$platformTag],
19- ];
17+ $searchParams = (new SearchParamsObject())
18+ ->setQuery('<YOUR _SEARCH_QUERY >')
19+ ->setRuleContexts([$platformTag]);
2020
2121 { {#dynamicSnippet} }searchWithSearchParams{ {/dynamicSnippet} };
2222} catch (Exception $e) {
You can’t perform that action at this time.
0 commit comments