本来想用这个来给自己的网站添加搜索的,但是折腾了很长时间都失败了,想想还是算了吧…… Algolia

Create you first index

An index is an entity within Algolia where you import the data you want to search (indexing) and perform queries against (search). There are 3 main ways to synchronize your data …

  • API Clients. For those who love to build things by themselves, you can use our API clients to interact with the Algolia REST API.

  • Plugins & Extensions. If you’re using a CMS or a platform for your service, you can simply use one of our plugins and integrations to easily implement Algolia.

  • Algolia Dashboard. The dashboard is the easiest way to upload your data and get your first experience with Algolia. Simply drag and drop your JSON or CSV files.

In Algolia, every object is a record and is defined with JSON.1 Let’s simply upload a dataset of actors…

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  [
    {
      "name": "Monica Bellucci",
      "alternative_name": "Monica Anna Maria Bellucci",
      "rating": 3956,
      "image_path": "/z3sLuRKP7hQVr.jpg"
    },
    {
      "name": "Sean Connery",
      "alternative_name": "Sir Sean Connery",
      "rating": 746,
      "image_path": "/ce84udJZ9QRSR44jxwK2apM3DM8.jpg"
    },
    {
      "name": "Will Smith",
      "alternative_name": null,
      "rating": 492,
      "image_path": "/2iYXDlCvLyVO49louRyDDXagZ0G.jpg"
    },
    {...}
  ]

Select your attributes to index

  1. By default, Algolia indexes and searches across all the attributes of your records.

  2. Oops, we also search in the image_path attribute. In order to improve the relevance of the results, it would make more sense to search only in the name and alternative_name attributes.

  3. Click on Add an searchable attribute, then select from the list or type the first letter of the name of the attribute. Use drag and drop to put name before alternative_name in the list to make it more important. Then save.

Select your Custom Ranking attributes

You would want to be able to use the popularity of the actors in how the results are ranked. Algolia has a unique way to add this kind of metric to the relevance calculation. For instance, the rating attribute could be a good metric to use here.

Use the rating attribute as a ranking criterion. By using the rating attribute as your customRanking attribute (ordered by DESCending order), you will see first the “Ben” actors that have the highest ratings. Click Add an attribute, then select rating in the list and save.

Build your UI

Every time you request Algolia’s Search API, it answers with a JSON object that contains the best matching objects: the hits.

We recommend rendering these hits directly from the web browser of your end-users (using JavaScript) or the mobile device instead of going through your backend server.

To help you with this aspect of setting up Algolia, we provide many examples and tools, such as our browser extension.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Raw
{{ hit }}

# Minimalist
<li class="results">
  {{ hit.name }}
</li>

# Highlighted
<li class="results">
  {{{ hit._highlightResult.name.value }}}
</li>

# Detailed
<li class="results">
  <img src="https://image.tmdb.org/t/p/w154/{{ hit.image_path }}" />
  <h3>{{{ hit._highlightResult.name.value }}}</h3>
  {{ hit.alternative_name }}
</li>

  1. Json: Standard format that uses human-readable text to transmit date objects consisting of attribute-value pairs. [return]