๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Third Party

[iOS/API] Algolia API๋ฅผ ์ด์šฉํ•ด์„œ C.R.U.D ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 4. 21.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ Algoria๋ผ๋Š” API์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


์•Œ๊ณ ๋ฆฌ์•„๋ž€?๐Ÿ˜€

 

์•Œ๊ณ ๋ฆฌ์•„๋Š” SaaS(Search as a Service) ํ”Œ๋žซํผ์œผ๋กœ ์ฆ‰, ๊ฒ€์ƒ‰ ์„œ๋น„์Šค ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค.

 

๋ฐฑ์—”๋“œ,ํ”„๋ก ํŠธ์—”๋“œ,API ํด๋ผ์ด์–ธํŠธ ๋“ฑ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์œผ๋กœ API๊ฐ€ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

 

๊ฒ€์ƒ‰ ์„œ๋น„์Šค๋ฟ๋งŒ ์•„๋‹ˆ๋ผ AI,Analytics ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

 

๊ฐ€๊ฒฉ์€ 1000๋ฒˆ ๊ฒ€์ƒ‰์— 1๋‹ฌ๋Ÿฌ์ž…๋‹ˆ๋‹ค.

 

(๊ฒ€์ƒ‰ 1๋ฒˆ์— 1์›์ด๋‹ˆ... ์ข€ ๋น„์‹ผ๊ฑฐ ๊ฐ™๋„ค์š”...)

 


์•Œ๊ณ ๋ฆฌ์•„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ๊ฒƒ์ด ์ข‹์„๊นŒ์š”?๐Ÿค”

 

1. ์ž๋™์™„์„ฑ ๊ฒ€์ƒ‰๊ธฐ๋Šฅ

 

์•Œ๊ณ ๋ฆฌ์•„๋Š” ๊ฒ€์ƒ‰ ํ…์ŠคํŠธ์— ๋”ฐ๋ผ์„œ ๊ด€๋ จ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๊ฑฐ๋‚˜ ๊ด€๋ จ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ 

 

๋น ๋ฅด๊ฒŒ ์ œ๊ณตํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ข‹์€ ๊ฒฝํ—˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

 

2. ๋Œ€์‰ฌ๋ณด๋“œ

 

์•Œ๊ณ ๋ฆฌ์•„๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ์˜ ์ •๋ณด๊ฐ€ ์–ด๋–ค ๊ฒƒ์ด ์žˆ๋Š”์ง€ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ์ œ๊ณตํ•˜๋ฉฐ

 

์• ๋„๋ฆฌํ‹ฑ์Šค๋„ ํ•จ๊ป˜ ์ œ๊ณตํ•˜์—ฌ ์œ ์ €์™€ ๊ฒ€์ƒ‰์„ ์–ผ๋งŒํผ ํ•˜์˜€๋Š”์ง€ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 


๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๋ฐ? ๐Ÿคจ

 

 

๋จผ์ € ์•„๋ž˜ ์•Œ๊ณ ๋ฆฌ์•„ ์‚ฌ์ดํŠธ๋กœ ๊ฐ€์„œ ํšŒ์›๊ฐ€์ž…์„ ํ•ด์ค๋‹ˆ๋‹ค.

 

 

Site Search & Discovery powered by AI

Create AI-powered search & discovery across websites & apps.

www.algolia.com

๋กœ๊ทธ์ธ์„ ํ•˜๊ณ  ํ™ˆํŽ˜์ด์ง€์˜ ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— DASHBOARD๋ฅผ ํด๋ฆญํ•ด์ค๋‹ˆ๋‹ค.

์™ผ์ชฝํƒญ์— API keys๋ฅผ ํƒญํ•ด์ค๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ Application ID์™€ Admin API Key๊ฐ€ ํ•„์š”ํ•˜๋‹ˆ ์ž˜ ์ €์žฅํ•ด์ฃผ์„ธ์š”!

 


Xcode

 

์ด์ œ Xcode๋กœ ์ด๋™ํ•ด์„œ ์ฝ”์ฝ”์•„ํŒŸ์— ์•Œ๊ณ ๋ฆฌ์•„๋ฅผ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.

pod 'AlgoliaSearchClient', '~> 8.0'

 

User

 

์œ ์ € ์ •๋ณด๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

import Foundation

struct User:Codable {
    let name:String
    let age:Int
    let description:String
    
    init(name:String,age:Int,desc:String) {
        self.name = name
        self.age = age
        self.description = desc
    }
}

Storyboard

 

Searchbar์™€ ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 


ViewController

 

AlgoliaSearchClient๋ฅผ import ํ•ด์ฃผ์„ธ์š”!

 

import AlgoliaSearchClient

 

์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ์„œ์น˜๋ฐ”์™€ ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

   @IBOutlet weak var searchBar: UISearchBar!
   @IBOutlet weak var table: UITableView!

 

์œ„์—์„œ ์ €์žฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•„์ด๋””์™€ ์–ด๋“œ๋ฏผ ํ‚ค๋ฅผ ์ ์–ด์„œ client๋ฅผ ์„ธํŒ…ํ•ด์ฃผ๊ณ 

 

index๋ฅผ users๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.( ์—ฌ๊ธฐ์„œ๋Š” user์ •๋ณด๋ฅผ ๋‹ด์•„ users๋ผ๊ณ  ํ–ˆ์ง€๋งŒ ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค!)

 

๊ทธ๋ฆฌ๊ณค user๋“ค์„ ๋‹ด์„ users๋ฐฐ์—ด๊ณผ ๊ฒ€์ƒ‰ํ•œ ์œ ์ €๋“ค์ด ๋‹ด๊ธธ filterUsers๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

    let client = SearchClient(appID: "Application ID", apiKey: "Admin API Key")
    lazy var index = client.index(withName: "users")
    var users:[User] = []
    var filterUsers:[User] = []

์„œ์น˜๋ฐ” ๋”œ๋ฆฌ๊ฒŒ์ดํŠธ์™€ ํ…Œ์ด๋ธ”๋ทฐ์˜ ๋ฐ์ดํ„ฐ์†Œ์Šค๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 private func configure() {
        searchBar.delegate = self
        table.dataSource = self
    }

 

ํ…Œ์ด๋ธ”๋ทฐ ๋ฐ์ดํ„ฐ์†Œ์Šค์™€ ์„œ์น˜๋ฐ”๋”œ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

extension ViewController:UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filterUsers.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        return cell
    }
}

extension ViewController:UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    
    }
}

 

์œ ์ €๋“ค์„ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

   private func setUsers() {
        let user1 = User(name: "Foma", age: 26, desc: "์•ˆ๋…•ํ•˜์„ธ์š” ์ €๋Š” ํฌ๋งˆ์ž…๋‹ˆ๋‹ค. ๋งŒ๋‚˜์„œ ๋ฐ˜๊ฐ€์›Œ์š”~")
        let user2 = User(name: "gran", age: 27, desc: "์•ˆ๋…•ํ•˜์„ธ์š” ์ €๋Š” ๊ทธ๋žœ์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ต™๊ฒ์Šต๋‹ˆ๋‹ค.")
        let user3 = User(name: "Fomagran", age: 28, desc: "ํฌ๋งˆ๊ทธ๋žœ์ž…๋‹ˆ๋‹ค!!")
        
        users = [user1,user2,user3]
    }

 


๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๊ธฐ

 

์œ„์—์„œ ๋งŒ๋“  users๋ฅผ algolia์— ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

private func saveToAlgolia(){
        index.saveObjects(users, autoGeneratingObjectID: true) { result in
            if case .success(let response) = result {
                print("Response: \(response)")
            }
        }
    }

์‹คํ–‰์‹œํ‚จ ๋’ค ์•Œ๊ณ ๋ฆฌ์•„ ๋Œ€์‰ฌ๋ณด๋“œ๋กœ ์ด๋™ํ•ด indices๋ฅผ ๋ณด๋ฉด

 

index๋กœ ๋งŒ๋“ค์–ด์ค€ users๊ฐ€ ์žˆ๊ณ 

๊ทธ ์•ˆ์— ๋ฐ์ดํ„ฐ๋“ค์„ ์ž˜ ์ €์žฅ๋œ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ

 

Algolia์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ age๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 private func sortAlgolia() {
        let settings = Settings()
            .set(\.customRanking, to: [.asc("age")])
        index.setSettings(settings) { result in
            switch result {
            case .failure(let error):
                print("Error when applying settings: \(error)")
            case .success:
                print("Success")
            }
        }
    }

 

Algolia ๋Œ€์‰ฌ๋ณด๋“œ๋กœ ๊ฐ€์„œ ํ™•์ธํ•ด๋ณด๋ฉด ๋‚˜์ด๊ฐ€ ์ ์€์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

๋ฐ์ดํ„ฐ ์ˆ˜์ •ํ•˜๊ธฐ

 

์›ํ•˜๋Š” ์˜ค๋ธŒ์ ํŠธID๋ฅผ ๋„ฃ๊ณ  ์ˆ˜์ •ํ•  attribute ์ด๋ฆ„๊ณผ ๊ฐ’์„ ๋„ฃ์€ ๋’ค ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

  private func updateObject() {
        let updates: [(ObjectID, PartialUpdate)] = [
          ("1111212002", .update(attribute: "age", value: 30))
        ]

        index.partialUpdateObjects(updates: updates) { result in
          if case .success(let response) = result {
            print("Response: \(response)")
          }
        }
    }

 

Algolia ๋Œ€์‰ฌ๋ณด๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ํ•ด๋‹น objectID๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด์˜ age๊ฐ€ ์ž˜ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ํ•˜๊ธฐ

 

์•„๋ž˜์™€ ๊ฐ™์ด ์„œ์น˜๋ฐ”์˜ ์ž…๋ ฅํ•œ ๊ธ€์ž๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ filterUsers์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ 

 

ํ…Œ์ด๋ธ”๋ทฐ๋ฅผ reload์‹œ์ผœ์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

  private func searchAlgolia(searchText:String) {
        
        index.search(query: "\(searchText)") { result in
            switch result {
            case .failure(let error):
                print("Error: \(error)")
            case .success(let response):
                print("Response: \(response.hits)")
                do {
                    self.filterUsers = try response.extractHits()
                    DispatchQueue.main.async {
                        self.table.reloadData()
                    }
                } catch let error {
                    print("Contact parsing error: \(error)")
                }
            }
        }
    }

 

UISearchBarDelegate์˜ searchBarSearchButtonClicked ๋ฉ”์†Œ๋“œ์— searchAlgolia ๋ฉ”์†Œ๋“œ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchAlgolia(searchText: searchBar.text ?? "")
    }

 

์ด๋ ‡๊ฒŒ ์„ธํŒ…ํ•œ ๋’ค ๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋ฉด ๊ฒ€์ƒ‰์–ด๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„ํ„ฐ๋ง ๋˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

 

 

๋ฐ์ดํ„ฐ ์‚ญ์ œํ•˜๊ธฐ

 

์‚ญ์ œํ•˜๊ณ ์‹ถ์€ ์˜ค๋ธŒ์ ํŠธ ์•„์ด๋””๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  ์‚ญ์ œํ•ด์ค๋‹ˆ๋‹ค.

 private func deleteObject() {
        index.deleteObject(withID: "1111212002") { result in
          if case .success(let response) = result {
            print("Response: \(response)")
          }
        }
    }

Algolia ๋Œ€์‰ฌ๋ณด๋“œ๋ฅผ ๋ณด๋ฉด ํ•ด๋‹น ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์‚ญ์ œ๋œ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค !

 


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ Algolia๊ฐ€ ๋ฌด์—‡์ด๊ณ  Algolia์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ˆ˜์ •ํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‚ญ์ œํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

 


Reference

 

 

Algolia Docs

Algolia: across 15 languages, read up on Algolia concepts, get access to tutorials with concrete use-cases and sample datasets, or explore our API Reference.

www.algolia.com

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€