Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Algolia with Firebase

miup
May 14, 2019

Algolia with Firebase

miup

May 14, 2019
Tweet

More Decks by miup

Other Decks in Programming

Transcript

  1. Cookpad Inc. Algolia with Firebase Kazuya Miura

  2. Cookpad Inc. All Rights Reserved. About me wࡾӜ࿨໵ʢΈ͏ͽʔʣ wCookpad৽ଔ3೥໨ wLanguage:

    Japanese, Swift, Typescript wTwitter: __miup wGithub: miuP wHobby: Fishing, Snowboard, Guitar, Drive wݕࡧʹ͍ͭͯ͸ Algolia Ͱษڧத
  3. Cookpad Inc. All Rights Reserved. Agenda wࣗݾ঺հ wKomerco ʹ͍ͭͯ wͳͥ

    Algolia Λ࢖͏Α͏ʹͳ͔ͬͨ wAlgolia ͱ Firebase ͷ࿈ܞ wOSS w·ͱΊ
  4. Cookpad Inc. All Rights Reserved. About Komerco

  5. Cookpad Inc. All Rights Reserved.

  6. Cookpad Inc. All Rights Reserved. ։ൃϝϯόʔʢ্ཱͪ͛࣌ʣ wσΟϨΫλʔ wσβΠφʔ wΤϯδχΞ(iOS) wΤϯδχΞ(iOS)

    wΤϯδχΞ(iOS) αʔόʔαΠυΈΜͳॻ͚ͳ͍
 Πϯϑϥ΋͋·ΓΘ͔Βͳ͍
  7. Cookpad Inc. All Rights Reserved. About Komerco Auth, Firestore, Storage,

    Hosting, CloudFunctions,
 Crashlytics, Analytics, Performance Monitoring
 Predictions, A/B Testing, Cloud Messaging, Remote Config, Dynamic Links Firebase Λϑϧʹ׆༻ͨ͠ΞϓϦ
  8. Cookpad Inc. All Rights Reserved. Firestore

  9. Cookpad Inc. All Rights Reserved. Firestore wDocument-oriented database (NoSQL) wτϥϯβΫγϣϯɺόονॻ͖ࠐΈΛαϙʔτ

    wϦΞϧλΠϜ
  10. Cookpad Inc. All Rights Reserved. Firestore ͷΫΤϦ wͰ͖Δ͜ͱ͕গͳ͍ w!=ԋࢉࢠ͕ͳ͍ wҰͭͷΫΤϦͰෳ਺ͷΧϥϜʹରͯ͠>=΍<=౳Λ࢖͑ͳ͍

    wOR ͕ͳ͍ wLIKE ͕ͳ͍ ؆୯ͳ͜ͱ͸Ͱ͖Δ͚Ͳ
 ೉͍͜͠ͱ͕Ͱ͖ͳ͍
  11. Cookpad Inc. All Rights Reserved. ެࣜʹιϦϡʔγϣϯ͕͋ͬͨ

  12. Cookpad Inc. All Rights Reserved. Firestore ͷΫΤϦ Firebase ެࣜʹҊ಺͕͋ͬͨͷ͕͖͔͚ͬͰ࠾༻ ManagedͳαʔϏεʹ೚ͤΒΕΔͷ͸ॿ͔Δ

    Algolia ͍ͬͯ͏ͷΛ࢖͏ͱྑ͍Β͍͠ https://firebase.google.com/docs/firestore/solutions/search?hl=ja
  13. Cookpad Inc. All Rights Reserved. ࢖͏͔͠ͳ͍ʂ

  14. Cookpad Inc. All Rights Reserved. ࣮ࡍʹ࢖͍ͬͯΔͱ͜Ζ wΧελϚʔΞϓϦʮ͕͢͞ʯը໘ wFacet Filter wTag

    Filter wNumeric Filter wReplica wSynonym
  15. Cookpad Inc. All Rights Reserved. ࣮૷ͷ͓࿩

  16. Cookpad Inc. All Rights Reserved. Firebase ͱͷ࿈ܞ

  17. Cookpad Inc. All Rights Reserved. ྲྀΕ Cloud Firestore Cloud Functions

    Algolia Save Indexing Trigger Search
  18. Cookpad Inc. All Rights Reserved. Cloud Functions export const productCreated

    = functions .firestore .document('products/{productID}') .onCreate(async (snapshot, context) => { const firProduct = snapshot.data() })
  19. Cookpad Inc. All Rights Reserved. ྲྀΕ Cloud Firestore Cloud Functions

    Algolia Save Indexing Trigger Search
  20. Cookpad Inc. All Rights Reserved. Cloud Functions algoliasearch(functions.config().algolia.app_id, functions.config().algolia.api_key) const

    productsIndex = algolia.default.initIndex('products') async function indexProduct(firProduct: Firebase.Product) { const product = new Product(firProduct) return new Promise((resolve, reject) => { productsIndex.addObject(product, async (error, response) => { if (error) { throw error } else { resolve(response) } }) }) }
  21. Cookpad Inc. All Rights Reserved. Cloud Functions export const productCreated

    = functions .firestore .document('products/{productID}') .onCreate(async (snapshot, context) => { const firProduct = snapshot.data() await indexProduct(firProduct) })
  22. Cookpad Inc. All Rights Reserved. Index Λߋ৽͢ΔλΠϛϯά " ͋ΔΦϒδΣΫτʹߋ৽͕͔͔ͬͨͱ͖ #

    ఆظతʹ࣮ߦ͢Δ
  23. Cookpad Inc. All Rights Reserved. Index Λߋ৽͢ΔλΠϛϯά wKomerco Ͱ͸౎౓ߋ৽ͷΈΛ͍ͯ͠Δ wKomerco

    Ͱ͸·ͩͦΜͳʹ৘ใߋ৽ͷස౓͕ߴ͘ͳ͍ͷͰAPI ίʔϧͷճ਺͕গͳ͍ wCloud Functions ࣗମ͸ఆظ࣮ߦͷ࢓૊Έ͕͋ΔͷͰߋ৽ස౓͕ ૿͖͑ͯͨΒҠߦ༧ఆ
  24. Cookpad Inc. All Rights Reserved. Index ͷઃఆ

  25. Cookpad Inc. All Rights Reserved. Index ͷઃఆ wݕࡧՄೳͳkeyΛઃఆ wෳ਺ͷSortΛ͢ΔͳΒReplicaΛ࡞੒ wΧςΰϦ෼͚Λ͢ΔͳΒFacet

    attributeΛઃఆ wSynonymͷઃఆ
  26. Cookpad Inc. All Rights Reserved. Searchable attributes

  27. Cookpad Inc. All Rights Reserved. Index ͷઃఆ wݕࡧՄೳͳkeyΛઃఆ wෳ਺ͷSortΛ͢ΔͳΒReplicaΛ࡞੒ wΧςΰϦ෼͚Λ͢ΔͳΒFacet

    attributeΛઃఆ wSynonymͷઃఆ
  28. Cookpad Inc. All Rights Reserved. Replica

  29. Cookpad Inc. All Rights Reserved. Replica

  30. Cookpad Inc. All Rights Reserved. Index ͷઃఆ wݕࡧՄೳͳkeyΛઃఆ wෳ਺ͷSortΛ͢ΔͳΒReplicaΛ࡞੒ wΧςΰϦ෼͚Λ͢ΔͳΒFacet

    attributeΛઃఆ wSynonymͷઃఆ
  31. Cookpad Inc. All Rights Reserved. Facet attribute

  32. Cookpad Inc. All Rights Reserved. Index ͷઃఆ wݕࡧՄೳͳkeyΛઃఆ wෳ਺ͷSortΛ͢ΔͳΒReplicaΛ࡞੒ wΧςΰϦ෼͚Λ͢ΔͳΒFacet

    attributeΛઃఆ wSynonymͷઃఆ
  33. Cookpad Inc. All Rights Reserved. Synonym wಉٛޠ wʮφεɺ՘ࢠɺͳ͢ʯʮΰʔϠɺʹ͕͏Γʯ
 ͸ͦΕͧΕಉ͡Α͏ͳ݁ՌΛग़͍ͨ͠ wछྨ͕͍͔ͭ͋͘Δ

    wOne-way Synonym =>ʮ͏ͭΘ>ಃثɺ࣓ثʯ wAlternate Correction => λΠϙΛڐ͢ One-way Synonym wPlaceholder =>ʮiPhone 7 => iPhone XS, iPhone 6 Plus, …ʯ
  34. Cookpad Inc. All Rights Reserved. ΞϓϦ͔Βͷݕࡧ

  35. Cookpad Inc. All Rights Reserved. ྲྀΕ Cloud Firestore Cloud Functions

    Algolia 4BWF *OEFYJOH 5SJHHFS 4FBSDI
  36. Cookpad Inc. All Rights Reserved. Algolia SDK let client =

    Client(appID: "YourApplicationID", apiKey: "YourAPIKey") let index = client.index(withName: "users") index.search(Query(query: "tarou")) { (response, error) in if let response = response { print("Result: \(response)") } }
  37. Cookpad Inc. All Rights Reserved. ܕ͕ͳ͍ Algolia ݕࡧ݁Ռʹܕ͕ͳ͍ʢ͖ͬ͞ͷresponse͕ [String: Any]ʣ


    Swift Ͱॻ͘ͳΒܕ͕ཉ͍͠
 Kotlin Ͱ΋ Java Ͱ΋ TypeScript Ͱ΋ಉ͚ͩ͡Ͳ
  38. Cookpad Inc. All Rights Reserved. ϥΠϒϥϦ࡞ͬͨ

  39. Cookpad Inc. All Rights Reserved. Algent Type safe Algolia search

    client for Swift. (Compatible with Swift5)
 https://github.com/miuP/Algent
  40. Cookpad Inc. All Rights Reserved. ࢖͍ํ wσʔλͷܕΛ࡞Δ wRequest Λ࡞Δ wIndex

    ໊ wΫΤϦͷ಺༰ w౤͛Δ
  41. Cookpad Inc. All Rights Reserved. Data Type struct User: Decodable

    { let objectID: String let name: String let bio: String let followerCount: Int let followeeCount: Int let _tags: [String] }
  42. Cookpad Inc. All Rights Reserved. Request struct SarchUserRequest: AlgoliaRequestProtocol {

    typealias HitType = User let page: Int let per: Int let text: String? let hashtags: [String]? var indexName: String { return "user" } var query: AlgentQuery { let query = AlgentQuery(query: text) query.page = UInt(page) query.hitsPerPage = UInt(per) if let hashtags = hashtags { query.tagFilters = hashtags } return query } }
  43. Cookpad Inc. All Rights Reserved. Search let request = SarchUserRequest(page:

    0, per: 20, text: "", hashtags: []) Algent.shared.search(request: request) { result in switch result { case .success(let response): // AlgoliaResponse<Request.HitType> print(response.hits) // see hit object case .failure( let error): print(error) } }
  44. Cookpad Inc. All Rights Reserved. ݕࡧͰ͖ͨ

  45. Cookpad Inc. All Rights Reserved. ·ͱΊ wKomerco Ͱͳͥ Algolia Λ࢖͍ͬͯΔͷ͔

    wSaaS Λ͏·͘ར༻ͯ͠Πϯϑϥ΍αʔόαΠυʹ
 ϦιʔεΛׂ͔ͣʹߴ଎ʹαʔϏε։ൃΛਐΊ͔ͨͬͨ wFirebaseΛ࢖͍ͬͯͯ΋ Cloud Functions Ͱ࿈ܞͰ͖Δ wiOS SDK ͷϥούʔͱͳΔ OSS ࡞ͬͨͷͰ࢖ͬͯΈ͍ͯͩ͘͞