java - 是否可以像在MySQL中一样在elasticsearch中使用spring数据查询IN?

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

我需要像在 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/

相关文章:

python - ElasticSearch切片滚动限制(Python)

ruby-on-rails - 与Elasticsearch相关的数据库

Elasticsearch:使用关系查询多个索引

java - Spring Cache 中的@Cacheable 在缓存外的redis 中存储值。我如何将它放入redis的缓存中?

将根据用户输入以梯形图形式打印的 Java 程序

java - SetFontSize 在 JRDesignStyle 中已弃用

java - 创建名称为 ‘client’ 的 bean 时出错

java - Spring Boot 2.0 中的 EmbeddedServletContainerCustomizer

java - 在 Ubuntu 12.10 上使用 Tomcat 7 --- 如何?

java - 如何在注销时使“记住我”失效?