name: CI

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    strategy:
      matrix:
        feature: [official_es7, official_es8]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: cargo-build-release-${{ hashFiles('**/Cargo.toml') }}
      - uses: dtolnay/rust-toolchain@stable
      - name: Build Release
        run: cargo build --release --features=${{matrix.feature}}

  format:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: cargo-build-debug-${{ hashFiles('**/Cargo.toml') }}
      - uses: dtolnay/rust-toolchain@stable
      - name: Check Format
        run: cargo fmt --check

  tests:
    strategy:
      matrix:
        feature: [official_es7, official_es8]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: cargo-build-test-${{ hashFiles('**/Cargo.toml') }}
      - uses: dtolnay/rust-toolchain@stable
      - name: Run Tests
        run: cargo test --features=${{matrix.feature}}

  clippy:
    strategy:
      matrix:
        feature: [official_es7, official_es8]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: cargo-build-clippy-${{ hashFiles('**/Cargo.toml') }}
      - uses: dtolnay/rust-toolchain@stable
      - name: Run Tests
        run: cargo clippy --features=${{matrix.feature}}

  examples:
    strategy:
      matrix:
        feature: [official_es7, official_es8]
    runs-on: ubuntu-latest
    steps:
      - name: Configure sysctl limits
        run: |
          sudo swapoff -a
          sudo sysctl -w vm.swappiness=1
          sudo sysctl -w fs.file-max=262144
          sudo sysctl -w vm.max_map_count=262144

      - name: Runs Elasticsearch
        uses: elastic/elastic-github-actions/elasticsearch@master
        with:
          stack-version: 7.11.2
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: cargo-build-examples-${{ hashFiles('**/Cargo.toml') }}
      - uses: dtolnay/rust-toolchain@stable
      - name: build
        run: cargo build
      - name: load data
        run: ./bin/setup_index.sh
      - name: 'Example: fetch_a_document'
        run: cargo run --features=${{matrix.feature}} --example fetch_a_document
      - name: 'Example: simple_search'
        run: cargo run --features=${{matrix.feature}} --example simple_search
      - name: 'Example: simple_aggs'
        run: cargo run --features=${{matrix.feature}} --example simple_aggs
      - name: 'Example: filter_aggs'
        run: cargo run --features=${{matrix.feature}} --example filter_aggs
      - name: 'Example: multi_search'
        run: cargo run --features=${{matrix.feature}} --example multi_search
      - name: 'Example: simple_sort'
        run: cargo run --features=${{matrix.feature}} --example simple_sort
      - name: 'Example: script_score'
        run: cargo run --features=${{matrix.feature}} --example script_score
      - name: 'Example: script_sort'
        run: cargo run --features=${{matrix.feature}} --example script_sort
      - name: 'Example: nested_filter'
        run: cargo run --features=${{matrix.feature}} --example nested_filter
      - name: 'Example: scroll_search'
        run: cargo run --features=${{matrix.feature}} --example scroll_search
      - name: 'Example: collapse_by'
        run: cargo run --features=${{matrix.feature}} --example collapse_by
      - name: 'Example: cardinality_agg'
        run: cargo run --features=${{matrix.feature}} --example cardinality_agg
      - name: 'Example: script_filter'
        run: cargo run --features=${{matrix.feature}} --example script_filter
