按地理距离的订单条款聚合
Order Terms Aggregation by Geo Distance
所以我这里有一个问题...
我正在使用 chewy
ruby gem 与 Elasticsearch 通信
=> #<Chewy::SnippetPagesIndex::Query:0x007f911c6b1610
@_collection=nil,
@_fully_qualified_named_aggs={"chewy::snippetpagesindex"=>{"chewy::snippetpagesindex::snippetpage"=>{}}},
@_indexes=[Chewy::SnippetPagesIndex],
@_named_aggs={},
@_request=nil,
@_response=nil,
@_results=nil,
@_types=[],
@criteria=
#<Chewy::Query::Criteria:0x007f911c6b1458
@aggregations=
{:group_by=>{:terms=>{:field=>"seo_area.suburb.id", :order=>{:_count=>"asc"}}, :aggs=>{:by_top_hit=>{:top_hits=>{:size=>10}}}}},
@facets={},
@fields=[],
@filters=
[{:geo_distance=>{:distance=>"100km", "seo_area.suburb.coordinates"=>"-27.9836052, 153.3977354"}},
{:bool=>
{:must_not=>[{:terms=>{:id=>[1]}}, {:terms=>{"seo_area.suburb.id"=>[5559]}}],
:must=>[{:term=>{:path_category=>"garden-services"}}, {:term=>{:status=>"active"}}, {:exists=>{:field=>"path_area"}}],
:should=>[]}}],
@options=
{:query_mode=>:must,
:filter_mode=>:and,
:post_filter_mode=>:and,
:preload=>
{:scope=>
#<Proc:0x007f911c6b1700@/Users/serviceseeking/Work/serviceseeking/engines/seo/app/concepts/seo/snippet_page/twins/search.rb:45 (lambda)>},
:loaded_objects=>true},
@post_filters=[],
@queries=[],
@request_options={},
@scores=[],
@script_fields={},
@search_options={},
@sort=[{:_geo_distance=>{"seo_area.suburb.coordinates"=>"-27.9836052, 153.3977354", :order=>"asc", :unit=>"km"}}],
@suggest={},
@types=[]>,
@options={}>
我正在使用 Elasticsearch 聚合,因此 query/search 阶段的任何排序都将在访问聚合时消失。
我传递的是这个...
aggs: {
by_seo_area_suburb_id: {
terms: {
field: "seo_area.suburb.id",
size: 10,
order: { by_distance: "desc" }
},
aggs: {
by_top_hit: {
top_hits: { size: 10 }
},
by_distance: {
geo_distance: {
field: "seo_area.suburb.coordinates",
origin: "52.3760, 4.894",
ranges: [
{ from: 0, to: 1 },
{ from: 1, to: 2 }
]
}
}
}
}
}
不过我收到了这个错误...
[500] {"error":{"root_cause":[{"type":"aggregation_execution_exception","reason":"Invalid terms aggregation order path [by_distance]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"snippet_pages","node":"srrlBssmSEGsqpZnPnOJmA","reason":{"type":"aggregation_execution_exception","reason":"Invalid terms aggregation order path [by_distance]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."}}]},"status":500}
简单地说...
术语桶只能在子聚合器路径上排序,该路径由路径内的零个或多个单桶聚合以及路径上的最终单桶或指标聚合构建而成结束。
有什么想法吗?
您有这样的存储桶:
1-2
2-3
4-5
等等。这些不是具有自然顺序的单一值桶。这就是异常告诉你的。所以你需要一些东西把它融化成单一的价值。
即使你可以通过那个订购。你为什么会?所有距离在 1 和 2 之间的都将具有相同的比较值,并且它们的排序将是不确定的。如果足以让您知道哪些是 0-1 和 1-2 等等,只需翻转聚合顺序即可。先取距离,对term进行子聚合
总而言之,我认为您有一个用例,其中聚合不是您想要的,因为请考虑以下两个文档:
{ name: "peter", location: [0,0] }
{ name: "peter", location: [100,0] }
很明显,在术语聚合中,两个 peter 会融为一体。但是它们有两个不同的位置,因此距离(几乎)总是不同的。那么如何按距离订购彼得呢?一旦聚合了一个字段,所有其他字段或多或少都会与它分离,并且您不能为此使用其他字段。
所以。如果您想要这样的东西,您很可能必须通过正常搜索进行。看一下如何按距离对搜索进行排序:
https://www.elastic.co/guide/en/elasticsearch/guide/current/sorting-by-distance.html
所以我这里有一个问题...
我正在使用 chewy
ruby gem 与 Elasticsearch 通信
=> #<Chewy::SnippetPagesIndex::Query:0x007f911c6b1610
@_collection=nil,
@_fully_qualified_named_aggs={"chewy::snippetpagesindex"=>{"chewy::snippetpagesindex::snippetpage"=>{}}},
@_indexes=[Chewy::SnippetPagesIndex],
@_named_aggs={},
@_request=nil,
@_response=nil,
@_results=nil,
@_types=[],
@criteria=
#<Chewy::Query::Criteria:0x007f911c6b1458
@aggregations=
{:group_by=>{:terms=>{:field=>"seo_area.suburb.id", :order=>{:_count=>"asc"}}, :aggs=>{:by_top_hit=>{:top_hits=>{:size=>10}}}}},
@facets={},
@fields=[],
@filters=
[{:geo_distance=>{:distance=>"100km", "seo_area.suburb.coordinates"=>"-27.9836052, 153.3977354"}},
{:bool=>
{:must_not=>[{:terms=>{:id=>[1]}}, {:terms=>{"seo_area.suburb.id"=>[5559]}}],
:must=>[{:term=>{:path_category=>"garden-services"}}, {:term=>{:status=>"active"}}, {:exists=>{:field=>"path_area"}}],
:should=>[]}}],
@options=
{:query_mode=>:must,
:filter_mode=>:and,
:post_filter_mode=>:and,
:preload=>
{:scope=>
#<Proc:0x007f911c6b1700@/Users/serviceseeking/Work/serviceseeking/engines/seo/app/concepts/seo/snippet_page/twins/search.rb:45 (lambda)>},
:loaded_objects=>true},
@post_filters=[],
@queries=[],
@request_options={},
@scores=[],
@script_fields={},
@search_options={},
@sort=[{:_geo_distance=>{"seo_area.suburb.coordinates"=>"-27.9836052, 153.3977354", :order=>"asc", :unit=>"km"}}],
@suggest={},
@types=[]>,
@options={}>
我正在使用 Elasticsearch 聚合,因此 query/search 阶段的任何排序都将在访问聚合时消失。
我传递的是这个...
aggs: {
by_seo_area_suburb_id: {
terms: {
field: "seo_area.suburb.id",
size: 10,
order: { by_distance: "desc" }
},
aggs: {
by_top_hit: {
top_hits: { size: 10 }
},
by_distance: {
geo_distance: {
field: "seo_area.suburb.coordinates",
origin: "52.3760, 4.894",
ranges: [
{ from: 0, to: 1 },
{ from: 1, to: 2 }
]
}
}
}
}
}
不过我收到了这个错误...
[500] {"error":{"root_cause":[{"type":"aggregation_execution_exception","reason":"Invalid terms aggregation order path [by_distance]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"snippet_pages","node":"srrlBssmSEGsqpZnPnOJmA","reason":{"type":"aggregation_execution_exception","reason":"Invalid terms aggregation order path [by_distance]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."}}]},"status":500}
简单地说...
术语桶只能在子聚合器路径上排序,该路径由路径内的零个或多个单桶聚合以及路径上的最终单桶或指标聚合构建而成结束。
有什么想法吗?
您有这样的存储桶:
1-2
2-3
4-5
等等。这些不是具有自然顺序的单一值桶。这就是异常告诉你的。所以你需要一些东西把它融化成单一的价值。
即使你可以通过那个订购。你为什么会?所有距离在 1 和 2 之间的都将具有相同的比较值,并且它们的排序将是不确定的。如果足以让您知道哪些是 0-1 和 1-2 等等,只需翻转聚合顺序即可。先取距离,对term进行子聚合
总而言之,我认为您有一个用例,其中聚合不是您想要的,因为请考虑以下两个文档:
{ name: "peter", location: [0,0] }
{ name: "peter", location: [100,0] }
很明显,在术语聚合中,两个 peter 会融为一体。但是它们有两个不同的位置,因此距离(几乎)总是不同的。那么如何按距离订购彼得呢?一旦聚合了一个字段,所有其他字段或多或少都会与它分离,并且您不能为此使用其他字段。
所以。如果您想要这样的东西,您很可能必须通过正常搜索进行。看一下如何按距离对搜索进行排序:
https://www.elastic.co/guide/en/elasticsearch/guide/current/sorting-by-distance.html