一尘不染

elasticsearch-聚合返回key中的项,但不返回完整字段,如何获得完整字段?

elasticsearch

在elasticsearch的实现中,基于几个字段,我只有几个简单的聚合,如下所示:

 "aggs" : {
    "author" : {
        "terms" : { "field" : "author" 
          , "size": 20,
          "order" : { "_term" : "asc" }
        }
    },
    "title" : {
        "terms" : { "field" : "title" 
          , "size": 20
        }
    },
    "contentType" : {
        "terms" : { "field" : "docType" 
          , "size": 20
        }
    }
}

聚合工作正常,我得到了相应的结果。但是返回的标题键字段(或任何其他字段-多字)具有单个字的汇总和结果。我需要返回结果中的完整标题,而不是一个单词-
没什么意义。我该怎么办。

当前结果(仅是摘录)-

"title": {
     "buckets": [
        {
           "key": "test",
           "doc_count": 1716
        },
        {
           "key": "pptx",
           "doc_count": 1247
        },
        {
           "key": "and",
           "doc_count": 661
        },
        {
           "key": "for",
           "doc_count": 489
        },
        {
           "key": "mobile",
           "doc_count": 487
        },
        {
           "key": "docx",
           "doc_count": 486
        },
        {
           "key": "pdf",
           "doc_count": 450
        },
        {
           "key": "2012",
           "doc_count": 397
        } ] }

预期成绩 -

"title": {
         "buckets": [
            {
               "key": "test document for stack overflow ",
               "doc_count": 1716
            },
            {
               "key": "this is a pptx",
               "doc_count": 1247
            },
            {
               "key": "its another document and so on",
               "doc_count": 661
            },
            {
               "key": "for",
               "doc_count": 489
            },
            {
               "key": "mobile",
               "doc_count": 487
            },
            {
               "key": "docx",
               "doc_count": 486
            },
            {
               "key": "pdf",
               "doc_count": 450
            },
            {
               "key": "2012",
               "doc_count": 397
            } }

我浏览了很多文档,它解释了汇总结果的不同方法,但是如果结果中的字段中有字段,我找不到如何获取全文,请告知我该如何实现?


阅读 1046

收藏
2020-06-22

共1个答案

一尘不染

您需要在索引中具有术语的未标记化副本,在映射中使用多字段

{
    "test": {
        "mappings": {
            "book": {
                "properties": {                
                    "author": {
                        "type": "string",
                        "fields": {
                            "untouched": {
                                "type": "string",
                                "index": "not_analyzed"
                            }
                        }
                    },
                    "title": {
                        "type": "string",
                        "fields": {
                            "untouched": {
                                "type": "string",
                                "index": "not_analyzed"
                            }
                        }
                    },
                    "docType": {
                        "type": "string",
                        "fields": {
                            "untouched": {
                                "type": "string",
                                "index": "not_analyzed"
                            }
                        }
                    }
                }
            }
        }
    }
}

在聚合查询中,引用未标记的字段:

"aggs" : {
    "author" : {
         "terms" : { 
            "field" : "author.untouched", 
            "size": 20,
            "order" : { "_term" : "asc" }
        }
     },
    "title" : {
        "terms" : { 
          "field" : "title.untouched", 
          "size": 20
        }
    },
    "contentType" : {
        "terms" : { 
           "field" : "docType.untouched", 
           "size": 20
        }
    }
}
2020-06-22