java报错
ElasticsearchStatusException[Elasticsearch exception [type=x_content_parse_exception, reason=[1:55] [bool] failed to parse field [must]]
]; nested: ElasticsearchException[Elasticsearch exception [type=parsing_exception, reason=[match] unknown token [START_ARRAY] after [query]]];
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://xxxxx:9200], URI [/vertical/_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":"parsing_exception","reason":"[match] unknown token [START_ARRAY] after [query]","line":1,"col":55}],"type":"x_content_parse_exception","reason":"[1:55] [bool] failed to parse field [must]","caused_by":{"type":"parsing_exception","reason":"[match] unknown token [START_ARRAY] after [query]","line":1,"col":55}},"status":400}
Caused by: ElasticsearchException[Elasticsearch exception [type=parsing_exception, reason=[match] unknown token [START_ARRAY] after [query]]]
分析:
注意报错信息,
type=parsing_exception
[HTTP/1.1 400 Bad Request]
所以我猜是请求参数有问题,经过控制变量法,发现是创建索引库时properties里面的字段type问题,type为text类型的字段不能传数组进去查询
下面为会报错的代码
创建索引库时,我们有一个字段cities,类型为text 即表示该字段需要分词
在java中,前端会传来要查询的城市集合,我们要根据城市筛选出合适的数据
下面我们构造查询条件,将前端传入的数组传入es进行查询
运行后就报上面的错误了
解决方法
当要采用传入数组进行查询时,字段名采用keyword类型,即该字段创建倒排索引时不用分词
至于为什么是这样设计的,我想应该是当你传数组查询时,es会遍历数组,相当于对数组进行了分词,当这个字段是text时,遍历数组后会再进行分词,就是二次分词了
细细语
至于我为什么犯这个错误,一开始我是不知道还能传数组给es查询的,所以我一开始想的是,设置city的类型为text(要分词),前端传来城市数组后,我将数组toString()后传给es,es对这个字符串进行分词,应该就可以对城市进行筛选了
标签:exception,parse,reason,报错,数组,type,分词,es From: https://blog.csdn.net/m0_73665762/article/details/137053437