我正在学习 Elasticsearch ,我正在尝试根据表中的字段值检索数据。
我有一个表(MySQL)“code”,它有一个字段“code_group_id”和表中的现有数据。
我想使用 Typescript 和 Java 检索具有特定 code_group_id 的代码对象列表。我在Java中准备了以下方法:
@GetMapping("/_search/codes")
@Timed
public ResponseEntity<List<CodeDTO>> searchCodes(@RequestParam String query, Pageable pageable) {
log.debug("REST request to search for a page of Codes for query {}", query);
Page<CodeDTO> page = codeService.search(query, pageable);
HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/codes");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
@GetMapping("/codes/currencies")
@Timed
public ResponseEntity<List<CodeDTO>> getAllByCodeGroupId(Pageable pageable) {
QueryBuilder qb = QueryBuilders.termQuery("codeGroupId", 3);
return searchCodes(qb.toString(), pageable);
}
根据 ES 文档,术语查询在这里应该是正确的选择,因为我正在寻找一个特定的查询术语,所以这应该返回一个响应主体,其中包含所有具有 code_group_id = 3 的“代码”记录。
但是,当我在 REST API 上测试 GET 命令时,出现以下异常:
2018-04-21 21:32:47.024 ERROR 14961 --- [ XNIO-59 task-5]
c.i.s.aop.logging.LoggingAspect : Exception in ch.ice.swingkasso.service.impl.CodeServiceImpl.search() with cause = '[code] QueryParsingException[Failed to parse query [{
"term" : {
"codeGroupId" : 3
}
}]]; nested: ParseException[Cannot parse '{
"term" : {
"codeGroupId" : 3
}
}': Encountered " <RANGE_GOOP> "{\n "" at line 1, column 13.
Was expecting one of:
"]" ...
"}" ...
]; nested: ParseException[Encountered " <RANGE_GOOP> "{\n "" at line 1, column 13.
Was expecting one of:
"]" ...
"}" ...
];' and exception = 'all shards failed'
Caused by: org.elasticsearch.index.query.QueryParsingException: Failed to parse query [{
"term" : {
"codeGroupId" : 3
}
}]
我是否忽略了一些简单的事情?感谢您对此事的任何指示。
最佳答案
找到解决方案。问题是搜索方法将查询重新转换为 QueryStringQuery,因此出现解析错误。
关于java - ElasticSearch 解析异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49959542/