前言

因为最近项目上线,正好碰到了elasticsearch的许多问题,又恰好前几天写了es相关使用。就想梳理一些关于寻找elastic search bug的一些小技巧。


先描述一下遇到的几个bug

HTTP/1.1 400 Bad Request

    @Test
    public void testExist() throws IOException {
        GetIndexRequest request = new GetIndexRequest();
        boolean exists = client.indices().exists(request.indices("index-test"), RequestOptions.DEFAULT);// 利用检查索引是否存在看参数是否正确
        System.out.println(exists);
    }

报错内容

ElasticsearchStatusException[method [HEAD], host [http://localhost:9200], URI [/index-test?include_type_name=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true], status line [HTTP/1.1 400 Bad Request]
]; nested: ResponseException[method [HEAD], host [http://localhost:9200], URI [/index-test?include_type_name=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true], status line [HTTP/1.1 400 Bad Request]];
	at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1680)
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1446)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1403)
	at org.elasticsearch.client.IndicesClient.exists(IndicesClient.java:823)
	at com.ai.cass.dc.risk.elasticSearch.HelloElasticsearch.testExist(HelloElasticsearch.java:73)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.elasticsearch.client.ResponseException: method [HEAD], host [http://localhost:9200], URI [/index-test?include_type_name=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true], status line [HTTP/1.1 400 Bad Request]
	at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:260)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:238)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1433)
	... 28 more

报错原因

es 版本不匹配,测试环境使用没有问题,但到了生产环境

type is missing

错误内容如下,因为我是bulk,所以报了很多type is missing。

2022-09-03 23:00:15,002 ERROR Function [] - 3. 【afterBulk-failure失败】es执行bluk失败:
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;2: type is missing;3: type is missing;4: type is missing;5: type is missing;6: type is missing;7: type is missing;8: type is missing;9: type is missing;10: type is missing;11: type is missing;12: type is missing;13: type is missing;14: type is missing;15: type is missing;16: type is missing;17: type is missing;18: type is missing;19: type is missing;20: type is missing;21: type is missing;22: type is missing;23: type is missing;24: type is missing;25: type is missing;26: type is missing;27: type is missing;28: type is missing;29: type is missing;30: type is missing;31: type is missing;32: type is missing;33: type is missing;34: type is missing;35: type is missing;36: type is missing;37: type is missing;38: type is missing;39: type is missing;40: type is missing;41: type is missing;42: type is missing;43: type is missing;44: type is missing;45: type is missing;46: type is missing;47: type is missing;48: type is missing;49: type is missing;50: type is missing;51: type is missing;52: type is missing;53: type is missing;54: type is missing;55: type is missing;56: type is missing;57: type is missing;58: type is missing;59: type is missing;60: type is missing;61: type is missing;62: type is missing;63: type is missing;64: type is missing;65: type is missing;66: type is missing;67: type is missing;68: type is missing;69: type is missing;70: type is missing;71: type is missing;72: type is missing;73: type is missing;74: type is missing;75: type is missing;76: type is missing;77: type is missing;78: type is missing;79: type is missing;80: type is missing;81: type is missing;82: type is missing;83: type is missing;84: type is missing;85: type is missing;86: type is missing;87: type is missing;88: type is missing;89: type is missing;90: type is missing;91: type is missing;92: type is missing;93: type is missing;94: type is missing;95: type is missing;96: type is missing;97: type is missing;98: type is missing;99: type is missing;100: type is missing;101: type is missing;102: type is missing;103: type is missing;104: type is missing;105: type is missing;106: type is missing;107: type is missing;108: type is missing;109: type is missing;110: type is missing;111: type is missing;112: type is missing;113: type is missing;114: type is missing;115: type is missing;116: type is missing;117: type is missing;118: type is missing;119: type is missing;120: type is missing;121: type is missing;122: type is missing;123: type is missing;124: type is missing;125: type is missing;126: type is missing;127: type is missing;128: type is missing;129: type is missing;130: type is missing;131: type is missing;132: type is missing;133: type is missing;134: type is missing;135: type is missing;136: type is missing;137: type is missing;138: type is missing;139: type is missing;140: type is missing;141: type is missing;142: type is missing;143: type is missing;144: type is missing;145: type is missing;146: type is missing;147: type is missing;148: type is missing;149: type is missing;150: type is missing;151: type is missing;152: type is missing;153: type is missing;154: type is missing;155: type is missing;156: type is missing;157: type is missing;158: type is missing;159: type is missing;160: type is missing;161: type is missing;162: type is missing;163: type is missing;164: type is missing;165: type is missing;166: type is missing;167: type is missing;168: type is missing;169: type is missing;170: type is missing;171: type is missing;172: type is missing;173: type is missing;174: type is missing;175: type is missing;176: type is missing;177: type is missing;178: type is missing;179: type is missing;180: type is missing;181: type is missing;182: type is missing;183: type is missing;184: type is missing;185: type is missing;186: type is missing;187: type is missing;188: type is missing;189: type is missing;190: type is missing;191: type is missing;192: type is missing;193: type is missing;194: type is missing;195: type is missing;196: type is missing;197: type is missing;198: type is missing;199: type is missing;200: type is missing;201: type is missing;202: type is missing;203: type is missing;204: type is missing;205: type is missing;206: type is missing;207: type is missing;208: type is missing;209: type is missing;210: type is missing;211: type is missing;212: type is missing;213: type is missing;214: type is missing;215: type is missing;216: type is missing;217: type is missing;218: type is missing;219: type is missing;220: type is missing;221: type is missing;222: type is missing;223: type is missing;224: type is missing;225: type is missing;226: type is missing;227: type is missing;228: type is missing;229: type is missing;230: type is missing;231: type is missing;232: type is missing;233: type is missing;234: type is missing;235: type is missing;236: type is missing;237: type is missing;238: type is missing;239: type is missing;240: type is missing;241: type is missing;242: type is missing;243: type is missing;244: type is missing;245: type is missing;246: type is missing;247: type is missing;248: type is missing;249: type is missing;250: type is missing;251: type is missing;252: type is missing;253: type is missing;254: type is missing;255: type is missing;256: type is missing;257: type is missing;258: type is missing;259: type is missing;260: type is missing;261: type is missing;262: type is missing;263: type is missing;264: type is missing;265: type is missing;266: type is missing;267: type is missing;268: type is missing;269: type is missing;270: type is missing;271: type is missing;272: type is missing;273: type is missing;274: type is missing;275: type is missing;276: type is missing;277: type is missing;278: type is missing;279: type is missing;280: type is missing;281: type is missing;282: type is missing;283: type is missing;284: type is missing;285: type is missing;286: type is missing;287: type is missing;288: type is missing;289: type is missing;290: type is missing;291: type is missing;292: type is missing;293: type is missing;294: type is missing;295: type is missing;296: type is missing;297: type is missing;298: type is missing;299: type is missing;300: type is missing;301: type is missing;302: type is missing;303: type is missing;304: type is missing;305: type is missing;306: type is missing;307: type is missing;308: type is missing;309: type is missing;310: type is missing;311: type is missing;312: type is missing;313: type is missing;314: type is missing;315: type is missing;316: type is missing;317: type is missing;318: type is missing;319: type is missing;320: type is missing;321: type is missing;322: type is missing;323: type is missing;324: type is missing;325: type is missing;326: type is missing;327: type is missing;328: type is missing;329: type is missing;330: type is missing;331: type is missing;332: type is missing;333: type is missing;334: type is missing;335: type is missing;336: type is missing;337: type is missing;338: type is missing;339: type is missing;340: type is missing;341: type is missing;342: type is missing;343: type is missing;344: type is missing;345: type is missing;346: type is missing;347: type is missing;348: type is missing;349: type is missing;350: type is missing;351: type is missing;352: type is missing;353: type is missing;354: type is missing;355: type is missing;356: type is missing;357: type is missing;358: type is missing;359: type is missing;360: type is missing;361: type is missing;362: type is missing;363: type is missing;364: type is missing;365: type is missing;366: type is missing;367: type is missing;368: type is missing;369: type is missing;370: type is missing;371: type is missing;372: type is missing;373: type is missing;374: type is missing;375: type is missing;376: type is missing;377: type is missing;378: type is missing;379: type is missing;380: type is missing;381: type is missing;382: type is missing;383: type is missing;384: type is missing;
	at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:562) ~[elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.client.RestHighLevelClient.performRequestAsync(RestHighLevelClient.java:1669) ~[elasticsearch-rest-high-level-client-6.5.4.jar:6.5.4]
	at org.elasticsearch.client.RestHighLevelClient.performRequestAsyncAndParseEntity(RestHighLevelClient.java:1635) ~[elasticsearch-rest-high-level-client-6.5.4.jar:6.5.4]
	at org.elasticsearch.client.RestHighLevelClient.bulkAsync(RestHighLevelClient.java:438) ~[elasticsearch-rest-high-level-client-6.5.4.jar:6.5.4]
	at com.ai.cass.dc.risk.flink.function.tansfer.RichEsSinkFunction.lambda$createBulkProcessor$1(RichEsSinkFunction.java:245) ~[blob_p-3412ff664e6e6f482fad02b1cb7951bb2994b09d-31b714462e7c0fe7d4622dcd62ea03ba:1.0.0]
	at org.elasticsearch.action.bulk.Retry$RetryHandler.execute(Retry.java:230) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.action.bulk.Retry.withBackoff(Retry.java:61) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.action.bulk.BulkRequestHandler.execute(BulkRequestHandler.java:62) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.action.bulk.BulkProcessor.execute(BulkProcessor.java:339) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.action.bulk.BulkProcessor.access$300(BulkProcessor.java:51) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.action.bulk.BulkProcessor$Flush.run(BulkProcessor.java:373) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.threadpool.Scheduler$ReschedulingRunnable.doRun(Scheduler.java:182) [elasticsearch-6.5.4.jar:6.5.4]
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.5.4.jar:6.5.4]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_252]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_252]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_252]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.8.0_252]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_252]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_252]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_252]

原因

据我了解版本8没有type一说,在7的版本就开始在弱化type,因此7的版本可能写或者不写type都不影响。
但在es6的版本上,没有指定type是不可以的。而我的生产环境恰好是6的版本,所以这个问题,我只需增加指定类型即可。

解决,只需加上type即可

indexRequest.index(index).type("_doc");

我想吐槽一下现在的网络环境,type is missing 为什么全网那么多都在说版本问题,然后就没然后了。版本问题没错,但问题的本质是type is missing,这翻译很简单吧,类型丢失,加上类型不就完了。

VALUE_NUMBER_INT

报错

Elasticsearch exception [type=json_parse_exception, reason=Current token (VALUE_NUMBER_INT) not of boolean type

这个问题,我在是网上搜到很多答案,不符合我的情况。
我猜测这个报错可能是比较范的,解决这类型问题,要拿出终极武器 DEBUG
在这里插入图片描述
把查询语句提取出来

{"from":0,"size":10,"timeout":"60s","query":{"bool":{"adjust_pure_negative":true,"boost":1.0}},"sort":[{"sortTime":{"order":"desc"}}],"track_total_hits":2147483647}

觉得看得不清楚 使用json转换工具转换一下

{
	"from": 0,
	"size": 10,
	"timeout": "60s",
	"query": {
		"bool": {
			"adjust_pure_negative": true,
			"boost": 1.0
		}
	},
	"sort": [{
		"sortTime": {
			"order": "desc"
		}
	}],
	"track_total_hits": 2147483647
}

然后将查询json拿到kibana执行
在这里插入图片描述

我这边执行之后是报了VALUE_NUMBER_INT的问题,于是我一个一个删除json中的内容,最终定位到是加了一下这个参数

"track_total_hits": 2147483647

很快找到对应代码中

sourceBuilder.trackTotalHits(true);

将这一行注释后,便不会生成 “track_total_hits”: 2147483647

这个问题的出现其实也是es6和es7的特性原因,一些es7支持的配置,在es6版本上不支持,所以导致报的问题很迷茫,es在兼容上比较差。

ES BUG 快速定位

我觉得问题的快速定位还是需要一些小技巧,才不会迷茫,虽然尝试有可能找到,但是时间就是成本

  • 第一步肯定是看报错的字面意思,再不济,copy翻译也是可以
  • 第二步确定代码使用的版本和安装的es版本
  • 第三步,debug 提取json 直接在kibana上运行,定位错误位置。
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐