es对nested类型字段查询并高亮显示时出现的bug

博客讨论了DSL语句在Elasticsearch中执行时遇到的offsetoutofbounds异常,原因是高亮配置使用了通配符*。解决方法是将高亮字段指定为具体的字段名,如member.name。修复后的DSL语句成功返回了预期结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、bug描述

DSL语句:

{
    "from": 0,
    "size": 5,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "is_deleted": {
                            "value": "false",
                            "boost": 1.0
                        }
                    }
                },
                {
                    "function_score": {
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "nested": {
                                            "query": {
                                                "bool": {
                                                    "should": [
                                                        {
                                                            "term": {
                                                                "member.name.pinyin": {
                                                                    "value": "鲁霞",
                                                                    "boost": 0.5
                                                                }
                                                            }
                                                        }
                                                    ],
                                                    "adjust_pure_negative": true,
                                                    "boost": 10.0
                                                }
                                            },
                                            "path": "member",
                                            "ignore_unmapped": false,
                                            "score_mode": "avg",
                                            "boost": 5.0,
                                            "inner_hits": {
                                                "ignore_unmapped": false,
                                                "from": 0,
                                                "size": 3,
                                                "version": false,
                                                "explain": false,
                                                "track_scores": false,
                                                "highlight": {
                                                    "fields": {
                                                        "*": {}
                                                    }
                                                }
                                            }
                                        }
                                    }
                                ],
                                "adjust_pure_negative": true,
                                "boost": 1.0
                            }
                        },
                        "boost": 3.0
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "track_scores": true,
    "highlight": {
        "fields": {
            "*": {}
        }
    }
}
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 6,
        "successful": 5,
        "skipped": 0,
        "failed": 1,
        "failures": [
            {
                "shard": 1,
                "index": "jingdata_project",
                "node": "sM3Z-9snQuexvj5V5EfswQ",
                "reason": {
                    "type": "illegal_argument_exception",
                    "reason": "offset out of bounds"
                }
            }
        ]
    },
    "hits": {
        "total": 2,
        "max_score": 1279.1053,
        "hits": [
            {省略}
        ]
    }
}

查询结果中报错(offset out of bounds),显示一个分片查询失败,并且结果中total为2,实际只返回1条数据。

二、bug原因

在这里插入图片描述

"highlight": {
    "fields": {
        "*": {}
    }
}

查询DSL高亮配置中,不能直接写通配符*,需要指定具体的字段名称

三、解决办法

需要把对应DSL改成如下语句

"highlight": {
     "fields": {
        "member.name": {}
    }
}

四、参考资料

1、https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值