spring-data-elasticsearch 字段如何驼峰转下划线,并实现ES查询
spring-data-elasticsearch 字段如何驼峰转下划线
使用spring-data-elasticsearch,但是想在es中存储的时候使用下划线。
解决:实体中采用@Field(name = "test_id") 注解;
例如:
ES查询:
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; @Component public class EsTaskJob { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate;
// 周期线程池 private final ScheduledExecutorService scheduledExecutorService = newScheduledThreadPool(16);
// 执行一次 public void doStart() { scheduledExecutorService.schedule(new Runnable() { @Override public void run() { try { doTask(req); } catch (RuntimeException e) { e.printStackTrace(); } } }, 0, TimeUnit.SECONDS); } private void doTask(AssetRetrievalTaskReq req) {
// 查询条件 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.matchQuery("be_app_name", "open")); // 查询
SearchHits<EsHttpReq> searchHitReqs = elasticsearchRestTemplate.search(query, EsHttpReq.class, IndexCoordinates.of("app_req")); if (searchHitReqs != null && searchHitReqs.getSearchHits().size() > 0) {
// 读取返回内容
for (org.springframework.data.elasticsearch.core.SearchHit<EsHttpReq> searchHit : searchHitReqs) { EsHttpReq esHttpReq = searchHit.getContent(); } } } }