How to add search to your iOS app with Elastic App Search

This is a short and sweet, to-the-point, tutorial series that shows how to add search functionality to an iOS app using Elasticsearch for free! More specifically, in this two-part blog series, we will be creating an iOS app that uses Elastic App Search for searching a movie database. By the end of this tutorial, whether you […]

This is a short and sweet, to-the-point, tutorial series that shows how to add search functionality to an iOS app using Elasticsearch for free! More specifically, in this two-part blog series, we will be creating an iOS app that uses Elastic App Search for searching a movie database. By the end of this tutorial, whether you need to search through a small movie database or through petabytes of cloud metrics, you’ll have the knowledge you need to build an amazing search experience for iOS mobile users.

Note: This tutorial is based on Elasticsearch version 7.12.x

Setup Elastic Cloud trial

  1. Navigate to https://www.elastic.co/cloud/.
  2. Choose Start free trial.
  3. Sign up and login.
  4. Choose Start your free trial.
  5. Select the Enterprise Search option.
  6. Choose to Expand the settings.
  7. Choose version 7.12.x.
  8. Choose Create deployment.
  9. Download the credentials.

Download dataset

  1. Navigate to https://www.kaggle.com/edgartanaka1/tmdb-movies-an….
  2. Download the dataset.
  3. Unzip the data.

Setup App Search

  1. Once your deployment has been created, choose Open Enterprise Search.
  2. Select Launch App Search.
  3. Type the engine name movies
  4. Choose Continue

Ingest data

  1. Select Upload a JSON file.
  2. Drag and drop one of the movie files from the downloaded dataset.
  3. Choose Continue.
  4. Once the data is indexed successfully, choose Visit the Dashboard.
  5. In the list of engines, choose movies.
  6. In the sidebar, choose Documents to see the document you indexed.

Configure Schema

  1. In the sidebar, choose Schema.
  2. Set the following fields to be of type Number using the drop-down options on the right:
    1. revenue
    2. popularity
    3. vote_count
    4. budget
    5. runtime
    6. vote_average
  3. Set the following fields to be of type Date using the drop-down options on the right:
    1. release_date
  4. Choose Confirm Types

Bulk ingest data

  1. In the sidebar, choose Credentials.
  2. Note the following fields as we will use these later:
    1. API Endpoint
    2. private-key
  3. In your command line terminal run: git clone [email protected]:elastic/tutorials.git
    1. Github Link: https://github.com/elastic/tutorials
  4. Open tutorials/app-search/app_search_ingest.py in your favorite text editor
  5. from elastic_enterprise_search import AppSearch
    import glob, os
    import json
    app_search = AppSearch(
        "app_search_api_endpoint",
        http_auth="api_private_key"
    )
    response = []
    print("Uploading movies to App Search...")
    os.chdir("movies_directory")
    for file in glob.glob("*.json"):
      with open(file, 'r') as json_file:
        try:
          response = app_search.index_documents(engine_name="movies",documents=json.load(json_file))
          print(".", end='', flush=True)
        except:
          print("Fail!")
          print(response)
          break
  1. In the python script, replace:
    1. app_search_api_endpoint with the API Endpoint from the credentials tab in your Enterprise Search deployment
    2. api_private_key with the private-key from the credentials tab in your Enterprise Search deployment
    3. movies_directory with the directory path of the unzipped movies data
      1. Example directory: "/Users/jsmith/Downloads/archive/movies/movies"
  2. In your command line terminal, install the Elastic Enterprise Search Python library
    1. pip3 install elastic-enterprise-search
  3. Run the python script
    1. python3 ./tutorials/app-search/app_search_ingest.py

Build iOS app

Wonderful! 🎉 You should now be ingesting documents into your App Search instance. To build the iOS app, see part 2 of this blog series.

Source: Elastic