I have a small android app which uses firestore. I have a collection of documents (~5,000) where each document has a "client name". One of the requirements of the applications is to search documents by client name by contains query. (ie where search term appears anywhere in name).
I opted to ignore the advice of using a full text search solution because of cost, and decided to roll my own by storing permutations of the name in an array field on each document, and running a contains query on the array.
For example the name John Smith
, an array is generated: [J, JO, JOH, JOHN, JOHN S, JOHN SM, JOHN SMI, JOHN SMIT, JOHN SMITH, S, SM, SMI, SMIT, SMITH]
. This is enough permutations to be able to emulate a "starts with" query on any word in the name, which is good enough for my use case.
I create an index on this array field called searchTerms
and run queries like this:
query.whereArrayContains("searchTerms", term)
.orderBy("date", Query.Direction.Descending)
.limit(20)
This ran fine initially, however my client has started to complain the searches being slow. I ran some tests on android, with query.get(Source.SERVER)
and regularly get query times of up to 2 seconds. Searching by short terms (eg. on
or al
) produced the slowest results.
My question is, how inefficient is this exactly? doing some research led me to believe it's not as inefficient as it looks on the surface. It sounds like each element in the array is added to the index, so when I run an array contains query, am I just querying a huge index which is the size of my collection * the average array size? Is it index size slowing down the query?
And finally, is there any other native solution other than this?