首页 > 解决方案 > ElasticSearch如何通过where条件进行分组

问题描述

我想用弹性搜索做一个简单的 SQL 查询,因为我不明白如何制作 where 条件。

这是我想要的 sql 命令:

SELECT * FROM my_index WHERE idUsers = 1018 Group by idPropositions

我尝试了很多东西,并通过 idPropositions 使用以下查询获得了我的请求组:

{
   "size": 0,
   "aggs": {
      "countries": {
         "terms": {
            "field": "idProposition"
         },
        
               "aggs": {
                  "docs": {
                     "top_hits": {
                        "size": 10
                     }
                  }
               }
            }
         }
      
   
} 

但我有这个结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "countries" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "2",
          "doc_count" : 2,
          "docs" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "conversations",
                  "_type" : "_doc",
                  "_id" : "Ji9593kBos4IiA1H-tUy",
                  "_score" : 1.0,
                  "_source" : {
                    "conversation" : "Salut",
                    "idConv" : "1",
                    "idUsers" : [
                      1018,
                      3
                    ],
                    "idProposition" : "2",
                    "idUserReceive" : "1018",
                    "idSender" : "3"
                  }
                },
                {
                  "_index" : "conversations",
                  "_type" : "_doc",
                  "_id" : "ucDmAHoBQ6Bqas-1bZhR",
                  "_score" : 1.0,
                  "_source" : {
                    "conversation" : "ca va?",
                    "idConv" : "1",
                    "idUsers" : [
                      1018,
                      3
                    ],
                    "idProposition" : "2",
                    "idUserReceive" : "3",
                    "idSender" : "1018"
                  }
                }
              ]
            }
          }
        },
        {
          "key" : "5",
          "doc_count" : 1,
          "docs" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "conversations",
                  "_type" : "_doc",
                  "_id" : "BiBlJXoB2tmdPjqJm_wd",
                  "_score" : 1.0,
                  "_source" : {
                    "conversation" : "test?",
                    "idConv" : "3",
                    "idUsers" : [
                      1020,
                      6
                    ],
                    "idProposition" : "5",
                    "idUserReceive" : "6",
                    "idSender" : "1020"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

如何使过滤器只有 idUsers = 1018 ?

我尝试使用过滤器,但它只返回 doc_count ...

标签: elasticsearch

解决方案


您可以在查询部分进行过滤并使用聚合进行分组

{
  "_source": false, 
  "query": {
    "term": {
      "idUsers": {
        "value": 1018
      }
    }
  },
  "aggs": {
    "countries": {
      "terms": {
        "field": "idProposition.keyword"
      },
      "aggs": {
        "docs": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

另一种方法是使用折叠

{
  "collapse": {
    "field": "idProposition.keyword",
    "inner_hits": {
      "name": "most_recent",
      "from":0,
      "size":10
    }
  },
  "_source": false, 
  "query": {
    "term": {
      "idUsers": {
        "value": 1018
      }
    }
  }
}

推荐阅读