解决 Elasticsearch 组合查询 ElasticsearchStatusException 异常

异常信息:

ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]
	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1897)
	at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1867)
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1624)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1581)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1551)
	at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1067)
	at test.ElasticsearchTest.sort1Query(ElasticsearchTest.java:181)
	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:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
	Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/user/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: Cannot search on field [address] since it is not indexed.","index_uuid":"_B3-AzylS6GEOFrh41r8cA","index":"user"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"user","node":"HsU-3cXZSCeEhgjaJcRQJA","reason":{"type":"query_shard_exception","reason":"failed to create query: Cannot search on field [address] since it is not indexed.","index_uuid":"_B3-AzylS6GEOFrh41r8cA","index":"user","caused_by":{"type":"illegal_argument_exception","reason":"Cannot search on field [address] since it is not indexed."}}}]},"status":400}
		at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:283)
		at org.elasticsearch.client.RestClient.performRequest(RestClient.java:261)
		at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235)
		at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1611)
		... 27 more

分析原因:

{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: Cannot search on field [address] since it is not indexed.","index_uuid":"_B3-AzylS6GEOFrh41r8cA","index":"user"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"user","node":"HsU-3cXZSCeEhgjaJcRQJA","reason":{"type":"query_shard_exception","reason":"failed to create query: Cannot search on field [address] since it is not indexed.","index_uuid":"_B3-AzylS6GEOFrh41r8cA","index":"user","caused_by":{"type":"illegal_argument_exception","reason":"Cannot search on field [address] since it is not indexed."}}}]},"status":400}
  • 查询时,不能解析address,组合查询,字段的类型必须是text类型,keyword会报异常

  • 查询映射信息
    使用Postman,向ES服务器发送GET请求:http://127.0.0.1:9200/user/_mapping

修改字段映射类型为text

  • 修改映射信息,Elasticsearch 不支持修改,需要删除后重新添加
    使用Postman,向ES服务器发送PUT请求:http://127.0.0.1:9200/user/_mapping
    参数:[Body] → [raw] → [JSON]
{
    "properties" : {
        "id" : {
            "type" : "text",
            "index" : true
        },
        "name" : {
            "type" : "text",
            "index" : true
        },
        "address" : {
            "type" : "text",
            "index" : false
        }
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐