Lucene Aggregation Query

Lucene is a text search library, not a database. It is mainly used for full-text search and relevance ranking, rather than supporting aggregate queries. However, Lucene's related projects Solr and Elasticsearch can use Lucene as the underlying library for their search engine, and they provide rich aggregation query capabilities. 1. Solr aggregation query: Solr supports multiple aggregation queries, including counting, summing, average, minimum, maximum, grouping, and nested aggregations. Example Table Structure: Id | name | category | price ---|------------|----------|------ 1 | Product 1 | Category1 | 10.5 2 | Product 2 | Category2 | 15.5 3 | Product 3 | Category1 | 20.0 4 | Product 4 | Category2 | 12.0 5 | Product 5 | Category3 | 18.5 Example aggregation query: -Count the number of matching documents: /select?q=*:*&rows=0&wt=json -Sum: /select?q=*:*&rows=0&json.facet={"total_price": "sum(price)"} -Average value: /select?q=*:*&rows=0&json.facet={"avg_price": "avg(price)"} -Minimum value: /select?q=*:*&rows=0&json.facet={"min_price": "min(price)"} -Maximum value: /select?q=*:*&rows=0&json.facet={"max_price": "max(price)"} -Grouping: /select?q=*:*&rows=0&json.facet={"category_group": {"type": "terms", "field": "category"}} 2. Elasticsearch aggregation query: Elasticsearch provides more powerful and flexible aggregation query functions, including bucket aggregation, metric aggregation, nested aggregation, filter aggregation, and so on. The sample table structure and data are the same as the Solr example. Example aggregation query: -Bucket aggregation (grouped by category): json GET /products/_search { "size": 0, "aggs": { "categories": { "terms": { "field": "category.keyword" } } } } -Aggregation of indicators (summation): json GET /products/_search { "size": 0, "aggs": { "total_price": { "sum": { "field": "price" } } } } -Nested aggregation (grouping by category and calculating the average price for each group): json GET /products/_search { "size": 0, "aggs": { "categories": { "terms": { "field": "category.keyword" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } -Filter aggregation (counting the number of documents with category 1): json GET /products/_search { "size": 0, "aggs": { "category1_count": { "filter": { "term": { "category.keyword": "Category1" } } } } } Summary: Lucene itself is just a search engine library and does not have the ability to directly support aggregated queries. However, Solr and Elasticsearch, two projects that use Lucene as the underlying search engine, provide powerful aggregation query capabilities that can meet various aggregation query requirements. The above are some examples of aggregation query usage and results. In practical use, the table structure and query requirements may vary.