首页 > 解决方案 > 如何查看在 Elasticsearch 中被索引的分析令牌是什么

问题描述

给定一个源值“New-Value”,假设我有一个keyword字段,我知道它被索引为“New-Value”不变,然后是一个text字段,我知道它被索引为“new”,“value”两个小写标记. 现在对于一些带有一些自定义分析器的自定义字段,而不是自己仔细跟踪索引定义,是否有一种简单的方法可以查询从现有索引中索引的内容?

标签: elasticsearch

解决方案


您需要启用fielddata才能实现它,它未在text字段上启用,之后您可以获得为 ES 倒排索引中的字段创建的令牌:

完整示例

索引映射

{
    "mappings" :{
        "properties" :{
            "foo" :{
                "type" : "text",
                "fielddata": true
            }
        }
    }
}

索引示例文档

{
    "foo" : "Bar"
}

{
    "foo" : "Bar and Baz"
}

并搜索查询以获取两个示例文档的倒排索引中的标记

{
    "query": {
        "match_all": {}
    },
    "docvalue_fields": [
        {
            "field": "foo"
        }
    ]
}

结果

 "hits": [
            {
                "_index": "myindex",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "foo": "Bar"
                },
                "fields": {
                    "foo": [
                        "bar"
                    ]
                }
            },
            {
                "_index": "myindex",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "foo": "Bar and Baz"
                },
                "fields": {
                    "foo": [
                        "and",
                        "bar",
                        "baz"
                    ]
                }
            }
        ]


推荐阅读