通过文本字段进行聚合和排序,并在Elasticsearch中连接其他文本字段

问题描述 投票:0回答:1

在Elasticsearch中,如何通过文本字段进行聚合和排序,并连接其他文本字段的字段值,例如, ;

在连接中,我的意思是连接来自所有聚合文档的相同字段的值,而不是来自同一文档的不同字段的值。

细节

我有小文件,字段基因,标签,注释描述为

{
  "mappings": {
    "annotations": {
      "properties": {
        "species": {
          "type": "text"
        },
        "gene": {
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            }  
          }
        },
        "tag": {
          "type": "text"
        },
        "annotation": {
          "type": "text"
        }
      }
    }
  }
}

每个基因有很多条目。也就是说,我有:

Gene  Tag   Annotation
----- ----- ---------------
A1BG  tag1  first gene
A2M   tag1  a-macroglobulin
A2M   tag2  second gene
BRCA1 tag1  breast cancer 1
BRCA1 tag3  important gene

我想查询这些数据,按基因聚合和排序,得到这样的结果:

Gene   Tags        Annotations
------ ----------- -------------------------------
A1BG   tag1        first gene
A2M    tag1; tag2  a-macroglobulin; second gene
BRCA1  tag1; tag3  breast cancer 1; important gene

谷歌搜索超过一天后,我找不到任何有意义的东西。 Elasticsearch示例主要显示统计信息,例如count,关于连接同一文档中的字段的一些示例,但我找不到连接相同字段的值的方法。我尝试使用map以及类似的东西:

{
    "aggs" : {
        "genes_agg" : {
            "terms" : {
                "script" : {
                    "source": "doc['tag'].join('; ')",
                    "lang": "painless"
                }
            }
        }
    }
}

但没有任何作用。

elasticsearch concatenation aggregation string-concatenation
1个回答
0
投票

我认为你找不到任何东西,因为你从关系数据库的角度来看这个问题。 Elasticsearch的构建方式类似于文档存储,因此您基本上可以将BRCA1的所有标记,注释等放在一个文档中。我认为你需要重新考虑你的索引策略,而不是你的查询策略。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.