Google Firestore: Query on substring of a property value (text search)

前端 未结 16 1566
抹茶落季
抹茶落季 2020-11-22 12:08

I am looking to add a simple search field, would like to use something like

collectionRef.where(\'name\', \'contains\', \'searchTerm\')

I tried

16条回答
  •  温柔的废话
    2020-11-22 12:46

    While Kuba's answer is true as far as restrictions go, you can partially emulate this with a set-like structure:

    {
      'terms': {
        'reebok': true,
        'mens': true,
        'tennis': true,
        'racket': true
      }
    }
    

    Now you can query with

    collectionRef.where('terms.tennis', '==', true)
    

    This works because Firestore will automatically create an index for every field. Unfortunately this doesn't work directly for compound queries because Firestore doesn't automatically create composite indexes.

    You can still work around this by storing combinations of words but this gets ugly fast.

    You're still probably better off with an outboard full text search.

提交回复
热议问题