Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opensearch): add amount as filter and handle name for different indexes #7073

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tsdk02
Copy link
Contributor

@tsdk02 tsdk02 commented Jan 20, 2025

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

Add amount as a filter / category for global search.
Payments / Refunds / Disputes can now be searched in a better manner by applying an amount filter in the following format:

amount:100

Handling of the field name while building the opensearch query is done.
PaymentAttempts / SessionizerPaymentAttempts / PaymentIntents / SessionizerPaymentIntents - amount
Refunds / SessionizerRefunds - refund_amount
Disputes / SessionizerDisputes - dispute_amount

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

For better search experience based on filters

How did you test it?

Hit the curl:

curl --location 'http://localhost:8080/analytics/v1/search' \
--header 'sec-ch-ua-platform: "macOS"' \
--header 'authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNmNkZTA0NzYtMTFlMi00NGE5LTlkMjUtOTA5M2QzNDQwZjhlIiwibWVyY2hhbnRfaWQiOiJtZXJjaGFudF8xNzM1MDQxMjkzIiwicm9sZV9pZCI6Im9yZ19hZG1pbiIsImV4cCI6MTczNzUzMjA3OSwib3JnX2lkIjoib3JnX2pwanI5TkFEWlhqSENNYTU5MmF3IiwicHJvZmlsZV9pZCI6InByb19QRHUydVA3aWNuM2lXY0I3V0VVSSIsInRlbmFudF9pZCI6InB1YmxpYyJ9.1DAbjUFNOCjpZM_K_NlKj6hOvzP28xLvl01XACE4jPg' \
--header 'Referer: http://localhost:9000/' \
--header 'sec-ch-ua: "Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"' \
--header 'sec-ch-ua-mobile: ?0' \
--header 'api-key: dev_ssyiqBXK7Ou1HUQuH0Z80BsHuV5pojc0uWli4QbNXGM1f4pn7jCIyb9VFiHlyATQ' \
--header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36' \
--header 'Content-Type: application/json' \
--data '{
    "query": "usd",
    "filters": {
        "amount": [
            600
        ]
    }
}'

You can see the amount filter getting applied, and should get results after the amount filter is applied.

Sample opensearch query structure:
Sessionizer Payment Intents Index: (Notice the amount field in the payload)

Index: SessionizerPaymentIntents
Payload: {
  "query": {
    "bool": {
      "filter": [
        {
          "multi_match": {
            "type": "phrase",
            "query": "usd",
            "lenient": true
          }
        },
        {
          "terms": {
            "amount": [
              600
            ]
          }
        }
      ],
      "must": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "organization_id.keyword": {
                                "value": "org_jpjr9NADZXjHCMa592aw"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ]
}

Sessionizer Refunds Index: (Notice the refund_amount being used here)

Index: SessionizerRefunds
Payload: {
  "query": {
    "bool": {
      "filter": [
        {
          "multi_match": {
            "type": "phrase",
            "query": "usd",
            "lenient": true
          }
        },
        {
          "terms": {
            "refund_amount": [
              600
            ]
          }
        }
      ],
      "must": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "organization_id.keyword": {
                                "value": "org_jpjr9NADZXjHCMa592aw"
                              }
                            }
                          }
                        ]
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ]
}

Sample response:

[
    {
        "count": 0,
        "index": "payment_attempts",
        "hits": [],
        "status": "Success"
    },
    {
        "count": 0,
        "index": "payment_intents",
        "hits": [],
        "status": "Success"
    },
    {
        "count": 1,
        "index": "refunds",
        "hits": [
            {
                "@timestamp": "2025-01-20T13:02:17Z",
                "attempt_id": "pay_ND5OdiSMSt6lhWovQup1_1",
                "connector": "stripe_test",
                "connector_refund_id": "dummy_ref_isNEfXmc0SW2r0cvaHvc",
                "connector_transaction_id": "pay_dWLQFQ9O5CcNP73haAqF",
                "created_at": 1737378137,
                "currency": "USD",
                "description": "Customer returned product",
                "external_reference_id": "ref_VIVDwLPl0zwwrRSJtnMP",
                "headers": {},
                "internal_reference_id": "refid_EqKX3XCwLUAw9vd8bk9Z",
                "merchant_id": "merchant_1735041293",
                "modified_at": 1737378138,
                "organization_id": "org_jpjr9NADZXjHCMa592aw",
                "payment_id": "pay_ND5OdiSMSt6lhWovQup1",
                "profile_id": "pro_PDu2uP7icn3iWcB7WEUI",
                "refund_amount": 600,
                "refund_arn": "",
                "refund_error_code": null,
                "refund_error_message": null,
                "refund_id": "ref_VIVDwLPl0zwwrRSJtnMP",
                "refund_reason": "Customer returned product",
                "refund_status": "success",
                "refund_type": "instant_refund",
                "sent_to_gateway": true,
                "source_type": "kafka",
                "tenant_id": "public",
                "timestamp": "2025-01-20T13:02:17Z",
                "total_amount": 10000
            }
        ],
        "status": "Success"
    },
    {
        "count": 0,
        "index": "disputes",
        "hits": [],
        "status": "Failure"
    },
    {
        "count": 0,
        "index": "sessionizer_payment_attempts",
        "hits": [],
        "status": "Failure"
    },
    {
        "count": 0,
        "index": "sessionizer_payment_intents",
        "hits": [],
        "status": "Failure"
    },
    {
        "count": 0,
        "index": "sessionizer_refunds",
        "hits": [],
        "status": "Failure"
    },
    {
        "count": 0,
        "index": "sessionizer_disputes",
        "hits": [],
        "status": "Failure"
    }
]

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@tsdk02 tsdk02 requested a review from a team as a code owner January 20, 2025 10:18
Copy link

semanticdiff-com bot commented Jan 20, 2025

Review changes with  SemanticDiff

Changed Files
File Status
  crates/analytics/src/search.rs  79% smaller
  crates/analytics/src/opensearch.rs  28% smaller
  crates/api_models/src/analytics/search.rs  0% smaller

@tsdk02 tsdk02 self-assigned this Jan 20, 2025
@tsdk02 tsdk02 added C-feature Category: Feature request or enhancement A-Analytics labels Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Analytics C-feature Category: Feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(opensearch): add amount as filter and handle name for different indexes
2 participants