Firestore Query Across Multiple Fields for Same Value

…衆ロ難τιáo~ 提交于 2019-12-19 10:47:12

问题


Is it possible to query multiple fields in a firestore document for the same value?

Imagine I have numerous documents where each described a sport:

sportsRef.collection("sports").documents [
["bowling" : [
"equipment": "bowling ball, bowling alley, bowling shoes",
"description": "bowl the ball" ]]

["football": [
"equipment": "ball, boots, shin pads, goalie gloves",
"description": "kick the ball in the goal" ]]

["darts": [
"equipment": "darts, dartboard",
"description": "throw the dart at the board" ]]

["boxing": [
"equipment": "boxing gloves, ring",
"description": "punch your contender" ]]

["shot put": [
"equipment": "shot put",
"description": "throw the shot put, looks like a ball" ]]
]

Is there any way to make a query that, if I searched for a value of "ball", would return all the documents that have "ball" within their values. So in this case I would be returned bowling, football and shot put ("ball" is in the description of shot put).

The reason why I ask is because I have implemented a UISearchController in my iOS app and am trying to do the search in my Firestore by constructing a query in my app.


回答1:


Firestore doesn't support substring, regular expression, or full text search types of queries. Those kind of searches don't scale with the way Firestore manages its indexes.

If you need fully text searching, consider duplicating your data into a search engine such as Algolia. This type of solution is discussed in the Firebase docs.




回答2:


One way to do something like that would be to restructure your data like so:

["bowling" : [
"ball": true, "bowling alley": true, "bowling shoes": true,
"description": "bowl the ball" ]]

and then query for documents where "ball" is equal to true:

Firestore.firestore().collection("sports").whereField("ball", isEqualTo: true)


来源:https://stackoverflow.com/questions/49141813/firestore-query-across-multiple-fields-for-same-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!