这是一个简单但困难的问题。我想对查询结果进行聚合,该结果应该像任何RDBMS的SQL一样使用"NOT IN"功能。
例如,我想做如下工作。
curl -XGET http://localhost:9200/my_index/my_type/_search?pretty -d '{
"query": {
"filtered": {
"filter": {
!!! Documents whose 'user_id' field value is 'NOT IN' distinct user_ids where the 'action' field value is 'signup' !!!
}
}
},
"aggregations": {
"distinct_users":{
"cardinality": {
"field": "user_id",
"precision_threshold": 1000000
}
}
}
}'
编辑
下面是一个示例数据。
curl -s -XPOST 'localhost:9200/my_index/my_type/1' -d'{ "user_id": 1234, "action": "signup" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/2' -d'{ "user_id": 1234, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/3' -d'{ "user_id": 1234, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/4' -d'{ "user_id": 5678, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/5' -d'{ "user_id": 5678, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/6' -d'{ "user_id": 9012, "action": "signup" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/7' -d'{ "user_id": 9012, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/8' -d'{ "user_id": 9012, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/9' -d'{ "user_id": 3456, "action": "visit" }'
curl -s -XPOST 'localhost:9200/my_index/my_type/10' -d'{ "user_id": 3456, "action": "visit" }'
我真正想得到的是"根据这些日志数据user_id未注册的文档"。所以,文档 [4, 5, 9, 10] 是我想要得到的最终结果。
是否可以在 Elasticsearch 中获得我想要的结果?
提前谢谢。
不,elasticsearch 不做连接,你要求的是连接的变体。
如上所述,它确实具有父子关系和嵌套对象,但根据您的域,这可能会对您有所帮助。
Elasticsearch也没有你需要的独特功能。但是你可以用术语聚合来伪造它。
但是,这在这里对您没有帮助,因为您确实需要为此加入。因此,这里唯一的解决方案是在 elasticsearch 之外进行连接。根据您的数据大小,这可能很昂贵。另请参阅应用程序端联接。
如果使用 not 过滤器,Elasticsearch 将依次检查每个文档 - 以下内容将返回具有action
不是signup
的任何操作的所有文档。
curl -XGET http://localhost:9200/myindex/my_type/_search?pretty -d '{
"query": {
"filtered": {
"filter": {
"not" : {
"term" : { "action" : "signup" }
}
}
}
}
}'
为了满足在没有任何action=signup
实例的情况下返回用户 ID 的要求,您需要设置父子关系。
在这种情况下,UserID 在所有user
类型文档中都是唯一的。每个用户文档都有一个或多个action
类型的子级。
以下查询检查action
子项并返回user
文档。
curl -XGET 'http://localhost:9200/myindex/my_type/_search?pretty' -d '{
"query": {
"filtered": {
"filter": {
"not" : {
"has_child": { "type": "my_action", "term" : { "action" : "signup" }}
}
}
}
}
}'