You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求提供ElasticSearch分组查询等语句实例(附示例结构与数据)

Hey there! I’ve put together a bunch of practical Elasticsearch query examples based on your sales index mapping and sample data to help you get up to speed quickly. These cover basic searches, filtering, grouping/aggregations, and more common use cases:

1. Basic Match & Term Queries

These are your go-to for simple lookups:

GET /sales/_search
{
  "query": {
    "match": {
      "productTitle": "Widget"
    }
  }
}

This will return all documents where productTitle contains "Widget" (works with partial matches if you're using a standard analyzer).

Exact match on product code

GET /sales/_search
{
  "query": {
    "term": {
      "productCode": "A"
    }
  }
}

Use term when you need an exact match for keyword-like fields (note: if your productCode was mapped as keyword instead of string, this works even better—though in your mapping it's string, which still works for exact matches here).

2. Filtering Queries

Filter results without affecting relevance scoring:

Find products with quantity greater than 3

GET /sales/_search
{
  "query": {
    "bool": {
      "filter": [
        { "range": { "quantity": { "gt": 3 } } }
      ]
    }
  }
}

Filters are cached, so they're great for repeated queries. This returns all docs where quantity is more than 3.

Filter by unit price range ($5 to $6)

GET /sales/_search
{
  "query": {
    "bool": {
      "filter": [
        { "range": { "unitPrice": { "gte": 5, "lte": 6 } } }
      ]
    }
  }
}

Use gte (greater than or equal) and lte (less than or equal) to define price ranges.

3. Grouping & Aggregation Queries

This is where you'll get insights from grouped data:

Group by product code, total quantity sold

GET /sales/_search
{
  "size": 0, // Don't return individual docs, just aggregations
  "aggs": {
    "products_by_code": {
      "terms": { "field": "productCode" },
      "aggs": {
        "total_quantity": { "sum": { "field": "quantity" } }
      }
    }
  }
}

This groups all sales by productCode and calculates the total quantity sold for each product. The size:0 skips returning individual documents since we only care about the aggregation results.

Group by product code, calculate total sales revenue

GET /sales/_search
{
  "size": 0,
  "aggs": {
    "products_by_code": {
      "terms": { "field": "productCode" },
      "aggs": {
        "total_revenue": {
          "sum": {
            "script": {
              "source": "doc['quantity'].value * doc['unitPrice'].value"
            }
          }
        }
      }
    }
  }
}

Uses a script to multiply quantity by unitPrice for each document, then sums that up per product code to get total revenue.

Group by product code, get average unit price

GET /sales/_search
{
  "size": 0,
  "aggs": {
    "products_by_code": {
      "terms": { "field": "productCode" },
      "aggs": {
        "avg_unit_price": { "avg": { "field": "unitPrice" } }
      }
    }
  }
}

Calculates the average unitPrice for each product group.

4. Combined Query + Aggregation

Get filtered results and run aggregations on them:

Filter for quantity > 2, then group by product code to get total revenue

GET /sales/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        { "range": { "quantity": { "gt": 2 } } }
      ]
    }
  },
  "aggs": {
    "products_by_code": {
      "terms": { "field": "productCode" },
      "aggs": {
        "total_revenue": {
          "sum": {
            "script": "doc['quantity'].value * doc['unitPrice'].value"
          }
        }
      }
    }
  }
}

First filters down to sales where quantity is over 2, then runs the revenue aggregation on that subset.

5. Sorting Results

Sort your search results by a specific field:

Sort sales by quantity (descending)

GET /sales/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "quantity": { "order": "desc" } }
  ]
}

Returns all sales documents sorted from highest to lowest quantity.


内容的提问来源于stack exchange,提问作者Davout

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 07:05:15