How to filter multiple attributes by 'contains' instead of exact string?

Hi,

Say I have a list of books that i want to search through. I want to find books where the title contains the word “World” by any author that contains the word “King”, how would I go about accomplishing this?

example:

search all attributes of a book - title, description, subject, author, etc etc by keyword: “Dragon”
author contains “King”
title contains “World”

Hi,

Assuming you are using InstantSearch, the solution would be to use RefinementLists for the author, title, and keyword.

You can read more about them here: refinementList | InstantSearch.js | API parameters | API Reference | Algolia Documentation

const search = instantsearch({
  appID: ..., // check the documentation here: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/
  ...
});

const authorList = instantsearch.widgets.refinementList({
  container: '#author-refinement-list',
  attribute: 'author', // assuming you have a field called author in your records
  searchable: true // assuming the facet is defined as searchable in your configuration
});

const keywordList = instantsearch.widgets.refinementList({
  container: '#keyword-refinement-list',
  attribute: 'keyword', // assuming you have a field called keyword in your records
  searchable: true, // assuming the facet is defined as searchable in your configuration
});

const titleList = instantsearch.widgets.refinementList({
  container: '#title-refinement-list',
  attribute: 'title', // assuming you have a field called title in your records
  searchable: true, // assuming the facet is defined as searchable in your configuration
});

search.addWidgets([titleList, authorList, refinementList])

... // rest of your setup

search.start() // enable search

This should do what you are looking for, but it might be more natural for you users to have a main search bar that searches in all those fields at the same time as well (using the searchBox widget: searchBox | InstantSearch.js | API parameters | API Reference | Algolia Documentation). The users could then rely on the refinement lists as a fallback if they want to restrict their searches to a specific author / keyword, etc.

I hope that helps!

Hi Jonathan,

Unfortunately I am not using instant search. I am using the javascript api client. how would I accomplish this without using instant search?

Hi @john8 !

Using the Javascipt API client, you could first look for every record where author contains King using the searchForFacetValues method. This method will return matching facetHits.

Then, you can run a standard search for Dragon using the facetHits as facetFilters to get all books that match the query Dragon where author contains King.

Best,