我需要像在 MySQL 中一样使用查询 IN 所以我的问题是它“可能”吗?我尝试在谷歌中查找一些内容,但没有结果。如果可能的话我应该怎么做?
也许应该使用闭包?但是在elasticsearch spring data中我应该如何使用?
我的代码:
交易索引存储库:
public interface TransactionIndexRepository extends ElasticsearchRepository<TransIndex, String> {
List<TransIndex> findBySellerIn(String sellers);
}
事务查询 Controller :
@RestController
public class TransactionQueryController {
private TransactionIndexRepository transactionIndexRepository;
private TransactionService transactionService;
@Autowired
public TransactionQueryController(TransactionService transactionService) {
this.transactionService = transactionService;
}
@RequestMapping(value = "/transaction/search", produces = MediaType.APPLICATION_JSON_VALUE)
private Map search(
@RequestParam(value = "commentText", required = false) String commentText,
@RequestParam(value = "commentType", required = false) Long commentType,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "priceFrom", required = false) Long priceFrom,
@RequestParam(value = "priceTo", required = false) Long priceTo,
@RequestParam(value = "tsFrom", required = false) Long tsFrom,
@RequestParam(value = "tsTo", required = false) Long tsTo,
@RequestParam(value = "seller", required = false) Long seller,
@RequestParam(value = "item", required = false) Long item,
@RequestParam(value = "tree_cat", required = false) Long tree_cat,
@RequestParam(value = "buyer", required = false) Long buyer,
@RequestParam(value = "cat", required = false) Long cat,
@RequestParam(value = "sellers", required = false) String sellers,
Pageable pageable) {
System.out.println(transactionIndexRepository.findBySellerIn(sellers));
final TransIndexSearchParams searchParams = TransIndexSearchParams.builder()
.commentText(commentText)
.commentType(commentType)
.title(title)
.priceFrom(priceFrom)
.priceTo(priceTo)
.tsFrom(tsFrom)
.tsTo(tsTo)
.seller(seller)
.item(item)
.tree_cat(tree_cat)
.buyer(buyer)
.cat(cat)
.build();
return transactionService.searchByIndexParams(searchParams, pageable);
}
}
我遇到了错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
怎么了?
最佳答案
在您的存储库类中,您可以简单地定义一个 findByXyzIn()
query method它需要一个字符串集合,如下所示:
findByNameIn(Collection<String> names);
将生成的等效 DSL 查询如下所示:
{
"bool": {
"must": {
"bool": {
"should": [
{
"field": {
"name": "name1"
}
},
{
"field": {
"name": "name2"
}
}
]
}
}
}
}
关于java - 是否可以像在MySQL中一样在elasticsearch中使用spring数据查询IN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35838203/