php [algolia php] algolia搜索php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [algolia php] algolia搜索php相关的知识,希望对你有一定的参考价值。

https://github.com/algolia/algoliasearch-client-php
<?php
/*
https://github.com/algolia/algoliasearch-client-php
composer require algolia/algoliasearch-client-php
*/
$client = Algolia\AlgoliaSearch\SearchClient::create(
  'YourApplicationID',
  'YourAdminAPIKey'
);

//in `contacts` index
$index = $client->initIndex('contacts');
# start indexing 500 contacts
$batch = json_decode(file_get_contents('contacts.json'), true);
$index->saveObjects($batch);

/**--------------------------------------------------------------
	Configure
*/
//order of attributes is crucial to decide which hit is the best
$index->setSettings(
  [
    'searchableAttributes' => [
      'lastname',
      'firstname',
      'company',
      'email',
      'city',
      'address'
    ]
  ]
);

/**--------------------------------------------------------------
	search: https://www.algolia.com/doc/api-reference/api-methods/search/#examples
*/
//search for contacts by firstname, lastname, company,..
var_dump($index->search('jimmie'));

$res = $index->search('query string', [
  'attributesToRetrieve' => [
    'firstname',
    'lastname',
  ],
  'hitsPerPage' => 50
]);

# Search the values of the "category" facet matching "phone".
$index->searchForFacetValues("category", "phone");

//with an additional searchParameters: having "Apple" in their "brand" facet.
$index->searchForFacetValues("category", "phone", [
  'filters' => 'brand:Apple'
]);

//search in multiple indices
$queries = [
  [
    'indexName' => 'categories',
    'query' => $myQueryString,
    'hitsPerPage' => 3
  ],
  [
    'indexName' => 'products',
    'query' => $myQueryString,
    'hitsPerPage' => 3,
    'facetFilters' => 'promotion'
  ],
  [
    'indexName' => 'products',
    'query' => $myQueryString,
    'hitsPerPage' => 10
  ]
];

$results = $client->multipleQueries($queries);
var_dump($results['results']);

//iterate over the whole index
$query = ''; // Empty query will match all records

foreach ($index->browseObjects($query, ['filters' => 'i<42']) as $hit) {
  var_dump($hit);
}

以上是关于php [algolia php] algolia搜索php的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress Algolia wp-config设置

algolia facets 自动隐藏产品

VuePress - Algolia 增加结果

Hexo next主题安装algolia

使用 Algolia 的 Firestore 数据库

在产品视图中显示 Algolia 结果