elasticsearch - 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 ...
解决方案
您可以在查询部分进行过滤并使用聚合进行分组
{
"_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
}
}
}
}
推荐阅读
- php - 使用 npm 在 css 中错误的目录位置
- javascript - 使用 PDFJS 在 IE 11 中未打印大型 pdf
- amazon-web-services - AWS,无服务器 SAM 模板 - 发布嵌套应用程序
- scala - 如何找到图中每个顶点的度数 - RDD Transformation?
- javascript - Node.js:等待 zip 文件生成并发送响应
- python - 葡萄酒质量 Kaggle 练习的问题 Python ML 错误
- amazon-web-services - 在 AWS 安全组中创建多个规则
- javascript - Google line chart: How to compare values from different time periods?
- javascript - 如果在外面单击,如何使模态消失
- excel - 向下拖动时,Excel公式索引和匹配不更新