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

Transcript

45 slides
  1. 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 Ͱษڧத
  2. Cookpad Inc. All Rights Reserved. Agenda wࣗݾ঺հ wKomerco ʹ͍ͭͯ wͳͥ

    Algolia Λ࢖͏Α͏ʹͳ͔ͬͨ wAlgolia ͱ Firebase ͷ࿈ܞ wOSS w·ͱΊ
  3. 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 Λϑϧʹ׆༻ͨ͠ΞϓϦ
  4. Cookpad Inc. All Rights Reserved. Firestore ͷΫΤϦ Firebase ެࣜʹҊ಺͕͋ͬͨͷ͕͖͔͚ͬͰ࠾༻ ManagedͳαʔϏεʹ೚ͤΒΕΔͷ͸ॿ͔Δ

    Algolia ͍ͬͯ͏ͷΛ࢖͏ͱྑ͍Β͍͠ https://firebase.google.com/docs/firestore/solutions/search?hl=ja
  5. Cookpad Inc. All Rights Reserved. Cloud Functions export const productCreated

    = functions .firestore .document('products/{productID}') .onCreate(async (snapshot, context) => { const firProduct = snapshot.data() })
  6. 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) } }) }) }
  7. 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) })
  8. Cookpad Inc. All Rights Reserved. Index Λߋ৽͢ΔλΠϛϯά wKomerco Ͱ͸౎౓ߋ৽ͷΈΛ͍ͯ͠Δ wKomerco

    Ͱ͸·ͩͦΜͳʹ৘ใߋ৽ͷස౓͕ߴ͘ͳ͍ͷͰAPI ίʔϧͷճ਺͕গͳ͍ wCloud Functions ࣗମ͸ఆظ࣮ߦͷ࢓૊Έ͕͋ΔͷͰߋ৽ස౓͕ ૿͖͑ͯͨΒҠߦ༧ఆ
  9. 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, …ʯ
  10. 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)") } }
  11. Cookpad Inc. All Rights Reserved. ܕ͕ͳ͍ Algolia ݕࡧ݁Ռʹܕ͕ͳ͍ʢ͖ͬ͞ͷresponse͕ [String: Any]ʣ


    Swift Ͱॻ͘ͳΒܕ͕ཉ͍͠
 Kotlin Ͱ΋ Java Ͱ΋ TypeScript Ͱ΋ಉ͚ͩ͡Ͳ
  12. Cookpad Inc. All Rights Reserved. Algent Type safe Algolia search

    client for Swift. (Compatible with Swift5)
 https://github.com/miuP/Algent
  13. 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] }
  14. 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 } }
  15. 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) } }
  16. Cookpad Inc. All Rights Reserved. ·ͱΊ wKomerco Ͱͳͥ Algolia Λ࢖͍ͬͯΔͷ͔

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

More Decks by miup

Other Decks in Programming