如何将两个不同布局的弹性搜索索引的结果组合在一起



我们有一个带有两个不同索引的弹性搜索。在一个索引中,有一个名为physical_location的字段,它包含一个子字段。在另一个索引中,有一个名为program_run.geolocation的字段,具有相同的嵌套lat和lon。

现在,我们希望在客户端web应用程序的地图上绘制来自两个索引的文档。我们尝试使用布尔查询

{
"query": {
"bool": {
"should": [
{
"geo_distance": {
"distance": "10km",
"physical_location": {
"lat": 53.193078596551715,
"lon": 6.783202751724139
}
}
},

{
"geo_distance": {
"distance": "10km",
"program_run.geolocation": {
"lat": 53.193078596551715,
"lon": 6.783202751724139
}
}
} 
]
}
}
}

但这确实给出了一个误差,因为elastic在第二索引中找不到第一字段,而在第一索引中又找不到第二字段。

解决这个问题的最佳方法是什么?

  • 是否有一种服务器端方法可以将不同索引和不同布局的查询组合在一起而不会出错
  • 还是在客户端进行组合更方便?(以及在这种情况下如何处理累计计数(
  • 还是唯一的方法是重新索引并使两个索引中的名称相同?(需要大量的重构工作(

弹性搜索论坛上的某个人给出了正确的答案

可以为索引映射中的字段名创建别名。所以你可以定义一个";location_for_search"通过别名映射到右侧字段的字段。

工作示例如下:https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659/2

编辑:以上问题的答案无效,请查找未投票的答案

我认为第一个选项可以实现,在Java RestHighLevelClient中,它有一个可用的构造函数,可以接受多个索引名称和一个搜索查询。

Constructs a new search request against the provided indices with the given search source
public SearchRequest(String[] indices, SearchSourceBuilder source) {
this();
if (source == null) {
throw new IllegalArgumentException("source must not be null");
}
indices(indices);
this.source = source;
}

最新更新