Info
This page has not yet been updated for Airflow 3. The concepts shown are relevant, but some code may need to be updated. If you run any examples, take care to update import statements and watch for any other breaking changes.
OpenSearch is an open source distributed search and analytics engine based on Apache Lucene. It offers advanced search capabilities on large bodies of text alongside powerful machine learning plugins. The OpenSearch Airflow provider offers modules to easily integrate OpenSearch with Airflow.
In this tutorial you’ll use Airflow to create an index in OpenSearch, ingest the lyrics of the musical Hamilton into the index, and run a search query on the index to see which character most often sings a specific word.
OpenSearch allows you to perform complex search queries on indexed text documents. Additionally, the tool comes with a variety of plugins for use cases such as security analytics, semantic search, and neural search.
Integrating OpenSearch with Airflow allows you to:
This tutorial takes approximately 30 minutes to complete.
To get the most out of this tutorial, make sure you have an understanding of:
This tutorial uses a local OpenSearch instance created as a Docker container. You do not need to install OpenSearch on your machine.
Info
The example code from this tutorial is also available on GitHub.
Create a new Astro project:
Add the following two lines to your Astro project requirements.txt file to install the OpenSearch Airflow provider and the pandas package in your Astro project:
This tutorial uses a local OpenSearch instance running in a Docker container. To run an OpenSearch container as part of your Airflow environment, create a new file in your Astro project root directory called docker-compose.override.yml and copy and paste the following into it:
Add the following configuration to your .env file to create an Airflow connection between Airflow and your OpenSearch instance. If you already have a cloud-based OpenSearch instance, you can connect to that instead of the local instance by adjusting the values in the connection.
The DAG in this tutorial uses a Kaggle dataset that contains the lyrics of the musical Hamilton.
Download the hamilton_lyrics.csv from Astronomer’s GitHub.
Save the file in your Astro project include folder.
In your dags folder, create a file called search_hamilton.py.
Copy the following code into the file.
This DAG consists of seven tasks to make a simple ML orchestration pipeline.
check_if_index_exists task uses a @task.branch decorator to check if the index OPENSEARCH_INDEX_NAME exists in your OpenSearch instance. If it does not exist, the task returns the string create_index causing the downstream create_index task to run. If the index exists, the task causes the empty index_exists task to run instead.create_index task defined with the OpenSearchCreateIndexOperator creates the index OPENSEARCH_INDEX_NAME in your OpenSearch instance with the three properties title, speaker and lines.csv_to_dict_list task uses the @task decorator to ingest the lyrics of the musical Hamilton from the hamilton_lyrics.csv file into a list of Python dictionaries. Each dictionary represents a line of the musical and will be one document in the OpenSearch index.add_lines_as_documents task is a dynamically mapped task using the OpenSearchAddDocumentOperator to create one mapped task instance for each document to ingest.search_for_keyword task is defined with the OpenSearchQueryOperator and performs a fuzzy query on the OpenSearch index to find the character and song that mention the KEYWORD_TO_SEARCH the most.print_query_result prints the query results to the task logs.
Tip
For information on more advanced search techniques in OpenSearch, see the OpenSearch documentation.
Run astro dev start in your Astro project to start Airflow and open the Airflow UI at localhost:8080.
In the Airflow UI, run the search_hamilton DAG by clicking the play button. By default the DAG will search the lyrics for the word write, but you can change the search term by updating the KEYWORD_TO_SEARCH variable in your DAG file.
View your song results in the task logs of the print_query_result task:
(Optional) Listen to the song that mentions your keyword the most.
Congratulations! You used Airflow and OpenSearch to analyze the lyrics of Hamilton! You can now use Airflow to orchestrate OpenSearch operations in your own machine learning pipelines. History has its eyes on you.