【问题标题】:Aggregation in elasticsearch with specific parameter具有特定参数的弹性搜索中的聚合
【发布时间】:2016-05-03 11:32:36
【问题描述】:

我在 elasticsearch 中有大量文档,作为示例,我将 elasticsearch 文档示例作为银行

   {
"_index": "bank",
"_type": "account",
"_id": "25",
"_score": 1,
"_source": {
"account_number": 25,
"balance": 40540,
"firstname": "Virginia",
"lastname": "Ayala",
"age": 39,
"gender": "F",
"address": "171 Putnam Avenue",
"employer": "Filodyne",
"email": "virginiaayala@filodyne.com",
"city": "Nicholson",
"state": "PA"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "44",
"_score": 1,
"_source": {
"account_number": 44,
"balance": 34487,
"firstname": "Aurelia",
"lastname": "Harding",
"age": 37,
"gender": "M",
"address": "502 Baycliff Terrace",
"employer": "Orbalix",
"email": "aureliaharding@orbalix.com",
"city": "Yardville",
"state": "DE"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "99",
"_score": 1,
"_source": {
"account_number": 99,
"balance": 47159,
"firstname": "Ratliff",
"lastname": "Heath",
"age": 39,
"gender": "F",
"address": "806 Rockwell Place",
"employer": "Zappix",
"email": "ratliffheath@zappix.com",
"city": "Shaft",
"state": "ND"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "119",
"_score": 1,
"_source": {
"account_number": 119,
"balance": 49222,
"firstname": "Laverne",
"lastname": "Johnson",
"age": 28,
"gender": "F",
"address": "302 Howard Place",
"employer": "Senmei",
"email": "lavernejohnson@senmei.com",
"city": "Herlong",
"state": "DC"
}
}
,
{
"_index": "bank",
"_type": "account",
"_id": "126",
"_score": 1,
"_source": {
"account_number": 126,
"balance": 3607,
"firstname": "Effie",
"lastname": "Gates",
"age": 39,
"gender": "F",
"address": "620 National Drive",
"employer": "Digitalus",
"email": "effiegates@digitalus.com",
"city": "Blodgett",
"state": "MD"
}
}


现在每个文档中都有一个名为 state 和 price 的字段。
我如何编写一个查询,它只返回包含不同状态的结果,排序顺序为 asc order 中的余额。

我正在尝试使用术语聚合,但没有用。
更新

POST _search
{
   "size": 0,
   "aggs": {
      "states": {
          "terms": {
              "field": "state"
          },
          "aggs": {
              "balances": {
                  "top_hits": {
                      "from" : 0,
                      "size": 1,
                      "sort": {"balance": "asc"}
                  }
              }
          }
      }
   }
}


现在,对于此查询,我将返回所有热门歌曲,其中价格按该键“状态”排序。但我想要的是一个排序结果 w.r.t 平衡和独特的状态字段。
对于上述查询,我​​得到如下响应

"buckets": [
            {
               "key": "tx",
               "doc_count": 30,
               "balances": {
                  "hits": {
                     "total": 30,
                     "max_score": null,
                     "hits": [
                        {
                           "_index": "bank",
                           "_type": "account",
                           "_id": "161",
                           "_score": null,
                           "_source": {
                              "account_number": 161,
                              "balance": 4659,
                              "firstname": "Doreen",
                              "lastname": "Randall",
                              "age": 37,
                              "gender": "F",
                              "address": "178 Court Street",
                              "employer": "Calcula",
                              "email": "doreenrandall@calcula.com",
                              "city": "Belmont",
                              "state": "TX"
                           },
                           "sort": [
                              4659
                           ]
                        }
                     ]
                  }
               }
            },
            {
               "key": "md",
               "doc_count": 28,
               "balances": {
                  "hits": {
                     "total": 28,
                     "max_score": null,
                     "hits": [
                        {
                           "_index": "bank",
                           "_type": "account",
                           "_id": "527",
                           "_score": null,
                           "_source": {
                              "account_number": 527,
                              "balance": 2028,
                              "firstname": "Carver",
                              "lastname": "Peters",
                              "age": 35,
                              "gender": "M",
                              "address": "816 Victor Road",
                              "employer": "Housedown",
                              "email": "carverpeters@housedown.com",
                              "city": "Nadine",
                              "state": "MD"
                           },
                           "sort": [
                              2028
                           ]
                        }
                     ]
                  }
               }
            },
            {
               "key": "id",
               "doc_count": 27,
               "balances": {
                  "hits": {
                     "total": 27,
                     "max_score": null,
                     "hits": [
                        {
                           "_index": "bank",
                           "_type": "account",
                           "_id": "402",
                           "_score": null,
                           "_source": {
                              "account_number": 402,
                              "balance": 1282,
                              "firstname": "Pacheco",
                              "lastname": "Rosales",
                              "age": 32,
                              "gender": "M",
                              "address": "538 Pershing Loop",
                              "employer": "Circum",
                              "email": "pachecorosales@circum.com",
                              "city": "Elbert",
                              "state": "ID"
                           },
                           "sort": [
                              1282
                           ]
                        }
                     ]
                  }
               }
            },

未按价格排序。

【问题讨论】:

    标签: elasticsearch aggregation elasticsearch-net elasticsearch-query


    【解决方案1】:

    试试这样:

    POST bank/_search
    {
       "size": 0,
       "aggs": {
          "states": {
              "terms": {
                  "field": "state",
                  "order": {
                       "balances": "asc"
                  }
              },
              "aggs": {
                  "balances": {
                      "sum": {
                          "field": "balance"
                      }
                  }
              }
          }
       }
    }
    

    注意:我没有看到 price 字段,而是 balance 字段,也许这就是您的意思。

    如果您有兴趣按州按价格排序所有文件,那么您也可以试试这个:

    POST bank/_search
    {
       "size": 0,
       "aggs": {
          "states": {
              "terms": {
                  "field": "state"
              },
              "aggs": {
                  "balances": {
                      "top_hits": {
                          "size": 5,
                          "sort": {"balance": "asc"}
                      }
                  }
              }
          }
       }
    }
    

    【讨论】:

    • 你能详细说明你得到了哪些“重复”吗?你能在你的问题中说明你期望得到什么回应吗?
    • 我没有得到不同的值,你可以试试这个例子。它是 elelasticsearch 文档中最常用的示例,索引为银行。我需要我的结果包含不同的状态值。
    • 状态字段上的terms 聚合只能为您提供不同的状态值。您如何发送查询?你到底得到了什么?
    • 我正在使用 sense(chrome 扩展)我通过 post _search 调用发送它最后我得到所有状态的聚合计数。但是将搜索更改为 20,我需要得到我现在没有得到的结果中的不同值......!!!
    • 你得到了什么?
    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    相关资源
    最近更新 更多