我正在学习弹性搜索,并尝试根据表中的字段值检索数据。
我有一个表(MySQL)“code”,其中有一个字段“code\u group\u id”,表中有现有数据。
我想使用Typescript和Java检索具有特定Code\u group\u 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\u group\u id=3的“code”记录的响应正文。
但是,当我在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
}
}]
我是否忽略了一些简单的事情?谢谢你在这件事上的指点。