Skip to content
Posts en inglés. Usá el traductor del navegador para leerlos en tu idioma.

How to Set Up and Use OpenSearch for Data Analysis

Yammbo
· 8 min read
data analysis search engine log management open source analytics platform
How to Set Up and Use OpenSearch for Data Analysis

OpenSearch is a powerful, open-source search and analytics engine that helps users manage, search, and analyze large volumes of data efficiently. Whether you're dealing with application logs, website analytics, or complex business intelligence datasets, OpenSearch provides a scalable and flexible solution. This tutorial will guide you through the process of setting up OpenSearch and OpenSearch Dashboards, indexing your first dataset, performing basic searches, and visualizing your data.

Understanding OpenSearch Fundamentals

Before diving into the setup, it's essential to grasp what OpenSearch is and what it offers. OpenSearch is a distributed, RESTful search and analytics engine built on Apache Lucene. It's designed for scalability, allowing you to handle petabytes of data and millions of queries per second across a cluster of servers. Its core capabilities include full-text search, aggregation, and data exploration, making it suitable for a wide range of use cases from log analytics to real-time application monitoring.

The OpenSearch ecosystem primarily consists of two main components:

  • OpenSearch: The core engine responsible for storing, indexing, and searching your data. It exposes a powerful REST API for interaction.
  • OpenSearch Dashboards: A web-based user interface (UI) for OpenSearch. It allows you to visualize your data, build dashboards, and interact with your OpenSearch clusters through a browser.

OpenSearch operates under the Apache 2.0 license, ensuring it remains free and open for anyone to use, modify, and distribute. This open-source philosophy fosters a vibrant community and ensures all features are accessible without proprietary restrictions.

Deploying OpenSearch and OpenSearch Dashboards with Docker Compose

For ease of setup and local development, we'll use Docker Compose to deploy both OpenSearch and OpenSearch Dashboards. This method simplifies the process of running multi-container Docker applications.

Prerequisites:

Step-by-step Deployment:

  1. Create a docker-compose.yml file: Create a new directory for your project and inside it, create a file named docker-compose.yml. Copy the following content into it:

    version: '3.8'services:  opensearch-node1:    image: opensearchproject/opensearch:2.12.0    container_name: opensearch-node1    environment:      - cluster.name=opensearch-cluster      - node.name=opensearch-node1      - discovery.type=single-node      - bootstrap.memory_lock=true # Disable swap during development      - OPENSEARCH_JAVA_OPTS="-Xms512m -Xmx512m" # Adjust memory as needed      - DISABLE_SECURITY_PLUGIN=true # Disable security for easy local setup    ulimits:      memlock:        soft: -1        hard: -1    volumes:      - opensearch-data:/usr/share/opensearch/data    ports:      - 9200:9200      - 9600:9600 # Required for OpenSearch Performance Analyzer    networks:      - opensearch-net  opensearch-dashboards:    image: opensearchproject/opensearch-dashboards:2.12.0    container_name: opensearch-dashboards    ports:      - 5601:5601    expose:      - "5601"    environment:      - OPENSEARCH_HOSTS=["http://opensearch-node1:9200"]      - DISABLE_SECURITY_PLUGIN=true # Disable security for easy local setup    networks:      - opensearch-net    depends_on:      - opensearch-node1volumes:  opensearch-data:networks:  opensearch-net:

    This configuration defines two services: opensearch-node1 for the OpenSearch engine and opensearch-dashboards for the UI. We've disabled the security plugin for a simpler local setup, but for production environments, enabling and configuring security is crucial.

  2. Start the services: Open your terminal or command prompt, navigate to the directory where you saved docker-compose.yml, and run:

    docker-compose up -d

    The -d flag runs the containers in detached mode, allowing them to run in the background.

  3. Verify the deployment:

    • OpenSearch should be accessible at http://localhost:9200. You can test this by navigating to the URL in your browser or running curl -XGET http://localhost:9200/. You should see a JSON response with cluster information.
    • OpenSearch Dashboards should be accessible at http://localhost:5601. Open this URL in your web browser. You should see the OpenSearch Dashboards login screen (if security is enabled) or the welcome page (if disabled).

Congratulations! You now have a running OpenSearch cluster and its visualization tool.

Indexing Your First Dataset

With OpenSearch running, the next step is to get some data into it. Data in OpenSearch is organized into indices, which are like databases or tables in a relational database. Each index contains multiple documents, and each document is a JSON object representing a unit of data.

Step-by-step Data Indexing:

  1. Create an index: You can create an index explicitly using the REST API. Let's create an index named products:

    curl -XPUT "http://localhost:9200/products" -H 'Content-Type: application/json' -d'{}'

    This command sends an HTTP PUT request to create an empty index named products. OpenSearch will respond with an acknowledgment.

  2. Add a single document: You can add individual documents to an index. OpenSearch will automatically generate a document ID if you don't provide one. Let's add a product document:

    curl -XPOST "http://localhost:9200/products/_doc" -H 'Content-Type: application/json' -d'{  "name": "Laptop Pro",  "brand": "TechCorp",  "price": 1200.00,  "in_stock": true,  "description": "High-performance laptop for professionals."}'

    To specify your own ID, use _doc/<your_id>:

    curl -XPUT "http://localhost:9200/products/_doc/1" -H 'Content-Type: application/json' -d'{  "name": "Wireless Mouse",  "brand": "TechCorp",  "price": 25.50,  "in_stock": true,  "description": "Ergonomic wireless mouse with long battery life."}'

    OpenSearch uses dynamic mapping by default, meaning it infers the data type of each field (e.g., string, number, boolean) from the first document indexed. For more control over how your data is stored and indexed, you can define explicit mappings.

  3. Add multiple documents using the Bulk API: For indexing large amounts of data, the Bulk API is far more efficient. It allows you to send multiple index, update, or delete operations in a single request.

    curl -XPOST "http://localhost:9200/_bulk" -H 'Content-Type: application/json' -d'{"index":{"_index":"products","_id":"2"}}{  "name": "Mechanical Keyboard",  "brand": "KeyMaster",  "price": 85.00,  "in_stock": true,  "description": "RGB mechanical keyboard with tactile switches."}{"index":{"_index":"products","_id":"3"}}{  "name": "External SSD 1TB",  "brand": "DataStore",  "price": 150.00,  "in_stock": false,  "description": "Fast and portable external solid-state drive."}'

    Each operation (index, create, update, delete) must be followed by a new-line character, and the document source must also be on a new line. The entire request must end with a new-line character.

  4. Verify indexed data: To confirm your documents have been indexed, you can query the index:

    curl -XGET "http://localhost:9200/products/_search?pretty"

    The ?pretty parameter formats the JSON output for readability. You should see the documents you just added in the hits array of the response.

Performing Basic Searches and Aggregations

Now that you have data in OpenSearch, you can start querying it. OpenSearch provides a rich Query Domain Specific Language (Query DSL) that allows for complex search queries and aggregations.

Basic Search Queries:

  1. Match all documents: The simplest query retrieves all documents in an index.

    curl -XGET "http://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'{  "query": {    "match_all": {}  }}'
  2. Term query (exact match): Use a term query to find documents where a field contains an exact value. This is typically used for keyword fields (not analyzed text).

    curl -XGET "http://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'{  "query": {    "term": {      "brand.keyword": "TechCorp"    }  }}'

    Note the .keyword suffix, which ensures an exact match on the non-analyzed version of the field.

  3. Match query (full-text search): For full-text search on analyzed text fields, use a match query. This will analyze your search term and match it against the analyzed content of the field.

    curl -XGET "http://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'{  "query": {    "match": {      "description": "high performance"    }  }}'
  4. Range query: Find documents where a numeric or date field falls within a specified range.

    curl -XGET "http://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'{  "query": {    "range": {      "price": {        "gte": 50,        "lte": 100      }    }  }}'

Aggregations:

Aggregations allow you to get analytical insights from your data, such as counting items, calculating averages, or finding the most frequent values. They are similar to SQL's GROUP BY and aggregate functions.

  1. Terms aggregation (count by category): To find out how many products belong to each brand:

    curl -XGET "http://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'{  "size": 0,  "aggs": {    "products_by_brand": {      "terms": {        "field": "brand.keyword"      }    }  }}'

    The "size": 0 parameter tells OpenSearch not to return any documents, only the aggregation results, making the response lighter.

  2. Average aggregation: To calculate the average price of all products:

    curl -XGET "http://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'{  "size": 0,  "aggs": {    "average_price": {      "avg": {        "field": "price"      }    }  }}'

These examples provide a glimpse into the powerful querying and aggregation capabilities of OpenSearch. The responses will contain the aggregated data under the aggregations key.

Visualizing Data with OpenSearch Dashboards

While the REST API is powerful, visualizing your data makes insights more accessible. OpenSearch Dashboards provides a rich interface for exploring, visualizing, and sharing your data.

Step-by-step Data Visualization:

  1. Access OpenSearch Dashboards: Open your web browser and navigate to http://localhost:5601.

  2. Create an Index Pattern:

    • In the left navigation pane, click on Stack Management (gear icon).
    • Under Kibana, select Index Patterns.
    • Click Create index pattern.
    • In the Index pattern name field, type products.
    • Click Next step.
    • Since our data doesn't have a time-based field for this tutorial, select I don't want to use Time filter.
    • Click Create index pattern.

    This tells Dashboards which indices to use for data exploration and visualization.

  3. Explore Data in Discover:

    • In the left navigation pane, click on Discover.
    • You should now see a list of your products documents. You can use the search bar at the top to perform simple queries and filter your data.
    • Expand individual documents to see their fields and values.
  4. Create a Visualization:

    • In the left navigation pane, click on Visualize.
    • Click Create visualization.
    • Choose a visualization type, for example, Vertical bar.
    • Select your products index pattern.
    • In the visualization builder, you'll configure your chart:
    • For the Y-axis, select Metric: Count.
    • For the X-axis, select Buckets: Terms, and set the Field to brand.keyword.
    • Click the Update button (play icon) to render your bar chart showing product counts by brand.
  5. Build a Dashboard:

    • In the left navigation pane, click on Dashboard.
    • Click Create new dashboard.
    • Click Add and select the visualization you just created.
    • You can add more visualizations and arrange them on the dashboard. Save your dashboard for future use.

OpenSearch Dashboards provides a powerful and intuitive way to transform raw data into actionable insights, making it an invaluable tool for data analysis.

Conclusion

This tutorial has walked you through the fundamental steps of setting up OpenSearch and OpenSearch Dashboards, from deployment using Docker Compose to indexing your data, performing searches, and creating visualizations. You've seen how OpenSearch can serve as a robust platform for managing and analyzing diverse datasets, laying the groundwork for more advanced applications, including those leveraging its machine learning and AI capabilities.

As you continue your journey with OpenSearch, explore its extensive documentation for more advanced features like security configuration, cluster management, and integrating with various data sources. To learn more about Yammbo and how we empower businesses with technology, visit https://yammbo.com.