是否可以在MySQL中使用elasticsearch spring数据查询IN?

时间:2016-03-07 07:19:37

标签: java elasticsearch spring-boot spring-data-elasticsearch

我需要在MySQL中使用查询IN,所以我的问题是“有可能”吗?我尝试在googl`e中找到一些东西,但结果却没有。如果可能我该怎么做?

也许使用clousure应该?但是我应该如何使用弹性搜索弹簧数据?

我的代码:

TransactionIndexRepository:

public interface TransactionIndexRepository extends ElasticsearchRepository<TransIndex, String> {

    List<TransIndex> findBySellerIn(String sellers);

}

TransactionQueryController:

@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

怎么了?

1 个答案:

答案 0 :(得分:1)

在您的知识库类中,您可以简单地定义一个findByXyzIn() query method来获取字符串集合,如下所示:

findByNameIn(Collection<String> names);

将要生成的等效DSL查询如下所示:

{
  "bool": {
    "must": {
      "bool": {
        "should": [
          {
            "field": {
              "name": "name1"
            }
          },
          {
            "field": {
              "name": "name2"
            }
          }
        ]
      }
    }
  }
}