如何在 Elasticsearch 中获取匹配的文本位置?

How do I get the matched text positions in Elasticsearch?

Elasticsearch 或 Elastic Cloud return 可以响应匹配的位置吗?

示例请求

{ "query": { "match": { "body": "apple" } } }

理想反应

{
    "_source": {
        "body": "The red apple is a good apple."
    },
    "matched_position": [
        {
            "start": 8,
            "end": 12,
        },
        {
            "start": 24,
            "end": 28,            
        }
    ]
}

如果您需要偏移量而不仅仅是突出显示的文本,则需要实现自定义荧光笔(插件)。

荧光笔插件示例:https://github.com/wikimedia/search-highlighter - 请注意,您必须将其调整为 7.X 版本。