Here's what I'd do: When creating (or updating) the doc, use the string that you would have used as the title to create another property, maybe call it searchTerms
:
let docName = 'green apple';
let ref = /* some path */.collection('items').doc(); // optionally use docName here
let doc = { /* however you initialize your doc */ }
doc.searchTerms = docName.split(' ').reduce((acc, term) => {
acc[term] = true;
return acc;
}, {});
// that will set searchTerms to { 'green':true, 'apple':true }
ref.set(doc)
Later, you can query those docs like this:
let targetTerm = 'apple'
let key = `searchTerms.${targetTerm}`;
let query = /* some path */.collection('items').where(key, '==', true)
In August, the product introduced support for searching arrays, so you can skip the reduce when creating the object, and ask where-contains
on the query, like this:
// as before
doc.searchTerms = docName.split(' ');
ref.set(doc)
and later:
let targetTerm = 'apple'
let query = /* some path */.collection('items').where('searchTerms', 'array-contains', targetTerm)
You might also use a cloud trigger onCreate
to build the search terms field, so all the points in code where creation is done can not worry about this.