Monday, 8 June 2026

OS queries

For exact filters like event id, scoring does not matter.

What Works Where

NeedBest OpenSearch Feature
Exact event filterterm on event.keyword
Search by name textmatch on name
Sort/group by stringkeyword field
Sort by amountnumeric field like double
Sort by tiernumeric rank field like tierRank
Get impacted resource rowsnormal search hits
Paginate impacted resourcesfrom + size
Group customersnested + terms aggregation
Sort customer groups by bill amountterms + max(customer.billAmount)
Sort customer groups by tierterms + min(customer.tierRank)
Deep paginate customer groupsnot clean with current index
Deep paginate customers cleanlyseparate customer-as-document index

Our Current Customer Insights Flow

Customer list:

impact documents -> nested customer aggregation -> group by customer -> sort by billAmount/tierRank -> API slices result

Drill-down:

impact documents -> filter by event + customerId -> OpenSearch from/size pagination


Customers query:

{ "size": 0, // dont return normal search hits, only grouped results are needed "query": {//filter "term": {//exact match "event.keyword": {//keyword must be used for exact matches "value": "UE107348" } } }, "aggs": {//group "customers_nested": {//aggr name - can be anything "nested": { "path": "customer"//enter nested customer array inside impact doc }, "aggs": { "by_customer": {//group by customer id now that we are inside nested cust-bucket per cust "terms": { "field": "customer.customerId", "size": 100,//return top 100 cust buckets "order": [ { "customer_bill_amount": "desc"/ "customer_tier_rank": "asc"//sort by metric aggr } ] }, "aggs": {//start sub aggr inside each cust bucket "customer_bill_amount": { "max": { "field": "customer.billAmount" } }, "customer_tier_rank": { "min": { "field": "customer.tierRank" } }, "customer_details": { "top_hits": {//for each bucket, fetch 1 doc to read cust details "size": 1, "_source": { "includes": [ "customer"//only return cust field from doc ] } } }, "impacted_resource_count": { "reverse_nested": {}//move up to parent impact level n return doc_count } } } } } } } 

Response:

"hits": {

  "total": {

    "value": 410,//num of docs matching filter

    "relation": "eq"

  },

  "hits": []//empty bcoz size=0

}

"aggregations": {

  "customers_nested": {

    "doc_count": 187,//num of aggr cust

    "by_customer": {

      "doc_count_error_upper_bound": 0,

        "sum_other_doc_count": 0,//custs not in list bcoz of terms.size limit

"buckets": [//unique buckets could be less than actual aggre count

          {

            "key": "300203",//cust id (grouped by)

            "doc_count": 6, // num of nested cust in bucket

            "customer_details": {

              "hits": {

                "total": {

                  "value": 6,

                  "relation": "eq"

                },

                "max_score": 0.0012172856,

                "hits": [

                  {

                    "_index": "smartsearch-impact-1",

                    "_id": "UE107348:450184",

                    "_nested": {

                      "field": "customer",

                      "offset": 0

                    },

                    "_score": 0.0012172856,

                    "_source": {

                      "customerSince": "2016-09-21",

                      "customerType": "Enterprise Customer",

                      "billAmount": 7180,

                      "phone": "3065557020",

                      "customerId": "300203",

                      "location": "2278 GOFF PLACE REGINA",

                      "sortMode": "billAmount",

                      "customerHref": "http://100.76.172.91:8001/Inventory/RSOpenAPI/party/300203",

                      "customerRole": "CustomerInsightsCustomer",

                      "customerName": "Canola Foods Distribution",

                      "email": "noc@canola-foods.ca"

                    }

                  }

                ]

              }

            },

            "customer_bill_amount": {

              "value": 7180

            },

            "customer_tier_rank": {

              "value": null

            },

            "impacted_resource_count": {

              "doc_count": 6

            }

          },

          {

            "key": "300187",

...

}

    }

  }

}


---

OpenSearch ResponseAPI Model Field
bucket keyCustomerInsight.customerId
customer details _source.customer[0].customerNamecustomerName
customer details _source.customer[0].billAmountbillAmount
customer details _source.customer[0].tiertier
customer_bill_amount.value or customer_tier_rank.valueused for sorting
active sort field valuesortValue
impacted_resource_count.doc_countimpactedResourceCount
number of returned buckets / parsed bucketstotalCustomers


Drill down query:

{
  "from": 0,//pagination offset
  "size": 25,//pagination size
  "query": {
    "bool": {//bool query
      "must": [
        {
          "term": {
            "event.keyword": {
              "value": "UE107348"
            }
          }
        },
        {
          "nested": {
            "path": "customer",
            "query": {
              "term": {
                "customer.customerId": {
                  "value": "CUST001"
                }
              }
            }
          }
        }
      ]
    }
  },
  "_source": {
    "includes": [//return these fields
      "id",
      "name",
      "entityType",
      "specification",
      "impactType",
      "impactSeverity",
      "analysisStatus",
      "href",
      "event"
    ]
  }
}

OpenSearch fieldAPI field
idimpactId
namename
entityTypeentityType
specificationspecification
impactTypeimpactType
impactSeverityimpactSeverity
analysisStatusanalysisStatus
hrefhref
eventeventId