elasticsearch-聚合返回key中的项,但不返回完整字段,如何获得完整字段? [英] elasticsearch - Aggregation returns terms in key , but not the complete field, how can I get full field returned?

查看:91
本文介绍了elasticsearch-聚合返回key中的项,但不返回完整字段,如何获得完整字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the elasticsearch implementation , I have few simple aggregations on the basis of few fields as shown below -

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

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

The aggregations work fine and I get the results accordingly. but the title key field returned (or any other field - multi word) , has single word aggregation and results. I need the full title in the returned result, rather then just a word- which doesn't make much sense. how can I get that.

当前结果(只是摘录)-

Current results (just a snippet) -

"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
            } }

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

I went through a lot of documentation, it explains different ways to aggregate results, but I couldn't find how to get the full text if a field in key in result , please advise how can I achieve this?

推荐答案

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

You need to have untokenized copies of the terms in the index, in your mapping use multi-fields:

{
    "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"
                            }
                        }
                    }
                }
            }
        }
    }
}

在汇总查询中,引用未标记的字段:

In your aggregation query reference the untokenized fields:

"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
        }
    }
}

这篇关于elasticsearch-聚合返回key中的项,但不返回完整字段,如何获得完整字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆