I\'ve the below custom model for firebase, I need to query for the cities with specific tags, I tried the below code but did not work :(
data class City(
My approach is to add some data to the searchable object on its onCreate
trigger.
// for structured data...
// const myCity = { name: 'Baltimore', state: 'Maryland', country: 'USA' }
// const names = [myCity.name, myCity.state, myCity.country]
// or for strings, like names...
const names = ["Benjamin Franklin", "Franklin Delano Roosevelt"]
const searchArray = _.flatten(names.map(n => n.toLowerCase().split(' ')))
const searchObject = searchArray.reduce((acc, n) => {
acc[n] = true;
return acc
}, {})
console.log(searchObject)
// update the created object with searchObject
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js"></script>
On the client (in JS), to do a search...
// say you're searching for "Franklin"
let searchTerm = "Franklin"
const key = `searchObject.${searchTerm.toLowerCase()}`
const query = db.collection("MyCollection").where(key, '==', true)
Firestore queries only support equality and range operations. They don't support regular expressions. You will have to perform two queries and merge the result on the client.
See: