【发布时间】:2016-01-05 06:25:25
【问题描述】:
我正在尝试对我拥有的一组数据进行一些弹性搜索查询。 我有一个用户文档,它是许多子页面视图文档的父级。我希望返回查看特定页面任意次数(由用户输入框定义)的所有用户。到目前为止,我有一个 has_child 查询,它将返回所有具有特定 id 的页面视图的用户。但是,这将返回那些父母和他们所有的孩子。接下来,我尝试在这些查询结果上编写一个聚合,它本质上将以聚合形式执行相同的 has_child 查询。现在,我对过滤后的子文档有了正确的文档计数。我需要使用此文档计数返回并过滤父母。用文字来解释查询,“将所有浏览特定页面超过 4 次的用户返回给我”。我可能需要重组我的数据。有什么想法吗?
这是我目前的查询:
curl -XGET 'http://localhost:9200/development_users/_search?pretty=true' -d '
{
"query" : {
"has_child" : {
"type" : "page_view",
"query" : {
"terms" : {
"viewed_id" : [175,180]
}
}
}
},
"aggs" : {
"to_page_view": {
"children": {
"type" : "page_view"
},
"aggs" : {
"page_views_that_match" : {
"filter" : { "terms": { "viewed_id" : [175,180] } }
}
}
}
}
}'
这会返回如下响应:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "development_users",
"_type" : "user",
"_id" : "22548",
"_score" : 1.0,
"_source":{"id":22548,"account_id":1009}
} ]
},
"aggregations" : {
"to_page_view" : {
"doc_count" : 53,
"page_views_that_match" : {
"doc_count" : 2
}
}
}
}
关联映射:
{
"development_users" : {
"mappings" : {
"page_view" : {
"dynamic" : "false",
"_parent" : {
"type" : "user"
},
"_routing" : {
"required" : true
},
"properties" : {
"created_at" : {
"type" : "date",
"format" : "date_time"
},
"id" : {
"type" : "integer"
},
"viewed_id" : {
"type" : "integer"
},
"time_on_page" : {
"type" : "integer"
},
"title" : {
"type" : "string"
},
"type" : {
"type" : "string"
},
"updated_at" : {
"type" : "date",
"format" : "date_time"
},
"url" : {
"type" : "string"
}
}
},
"user" : {
"dynamic" : "false",
"properties" : {
"account_id" : {
"type" : "integer"
},
"id" : {
"type" : "integer"
}
}
}
}
}
}
【问题讨论】:
-
"id"和"viewable_id"一样吗?一般来说,发布您的地图可以让人们更容易弄清楚如何回答您的问题。 -
是的,是我的错字,是 id。我也刚刚添加了映射。
-
酷,谢谢。我想我知道该怎么做,现在开始测试。
-
嗯,我想我明白了。
"page_view.id"是页面 id 吗?所以可以有很多"page_view"s 和相同的"id",对吧? -
其实我又犯了一个错误。它应该是一个单独的字段“viewed_id”。是的,可以有多个具有相同“viewed_id”的页面浏览量,并且应该计算在内。
标签: elasticsearch