Icon indexing white

Partial Match Queries

Last updated 01 August 2017

Before starting

Make sure you understand the following Algolia Concepts before starting this tutorial:

Introduction

By design, Algolia doesn’t natively support infix/suffix matching and therefore won’t find substrings starting at a non-zero index within a string. Only prefix matching is natively supported.

This means that the query abc will match the attribute abcdef, but this record won’t be surfaced for the queries cde or def.

Most of the time, such “partial” matches are dangerous for relevance as they generate a “noisier” result set. However, there are occasional use cases where this behavior can be useful (searching through product references or part numbers, for example).

Dataset

For this tutorial let’s use the following JSON dataset.

[
  {
    "objectID": 1,
    "name": "Phone a",
    "product_reference": "abcdef",
    "price": 500,
    "popularity": 10
  },
  {
    "objectID": 2,
    "name": "Phone b",
    "product_reference": "lmnopq",
    "price": 200,
    "popularity": 30
  }
]

For purposes of this tutorial, the goal will be searching in the middle of the product_reference attribute.

You can download the dataset here. For more information on indexing data, see our importing tutorial

Initializing the client

<?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', 'YourAdminAPIKey');

$index = $client->initIndex('your_index_name');
// var algoliasearch = require('algoliasearch');
// var algoliasearch = require('algoliasearch/reactnative');
// var algoliasearch = require('algoliasearch/lite');
// import algoliasearch from 'algoliasearch';
//
// or just use algoliasearch if you are using a <script> tag
// if you are using AMD module loader, algoliasearch will not be defined in window,
// but in the AMD modules of the page

var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var index = client.initIndex('your_index_name');

Generating alternatives

Since Algolia is a prefix search, you will need to add an attribute that contains all the possible suffixes of the product_reference in an array.

<?php
$objects = json_decode(file_get_contents('middle_of_the_word.json'), true);

$objects = array_map(function ($record) {
  $reference = $record['product_reference'];

  $record['product_reference_suffixes'] = [];

  while (mb_strlen($reference) > 1) {
    $reference = substr($reference, 1);

    $record['product_reference_suffixes'][] = $reference;
  }

  return $record;
}, $objects);


$index->addObjects($objects);

Once indexed the records should look like this:

[
  {
    "objectID": 1,
    "name": "Phone a",
    "product_reference": "abcdef",
    "product_reference_suffixes": ["bcdef", "cdef", "def", "ef", "f"],
    "price": 500,
    "popularity": 10
  },
  {
    "objectID": 2,
    "name": "Phone b",
    "product_reference": "lmnopq",
    "product_reference_suffixes": ["mnopq", "nopq", "opq", "pq", "q"],
    "price": 200,
    "popularity": 30
  }
]

As you can see, an extra attribute called product_reference_suffixes has been added. This new attribute contains all suffixes of the product_reference.

Update searchableAttributes

After updating all the records, you will also need to update your “searchableAttributes” to ensure the suffixes as searchable as well as the initial product reference.

<?php
$index->setSettings([
  'searchableAttributes' => [
    'name',
    'product_reference',
    'product_reference_suffixes'
  ]
]);

product_reference_suffixes has been placed after product_reference as it is better to match in the complete reference than the suffixes.

What’s next

Continue building your Algolia knowledge with these tutorials:

© Algolia - Privacy Policy