spring-data-elasticsearch + java 查询方法的封装

  • 最近开始重构elasicsearch+springboot+spring data elasticsearch的东西

  • 由于近期更新了elasticsearch 5.x /6.x,大家都忙着尝鲜,不过其中整合框架的过程中遇到了很多的问题

  • 在复写项目的过程中,有重新温习了一下spring data elasticsearch 的操作,我当前尝试的是spring-data-elasticsearch 3.0.1.RELEASE版本,比对以前的一些操作修改的不多,就是一些save=>saveAll,findOne=>findById,findAll=>findAllByIds等等,底层的操作原理没有影响,如果会的同志应当不会太困扰
  • 后期还会对transportClient的使用和框架整合ES的searchquery的介绍

以下是旧版本的spring-data-elasticsearch +java的操作:

1、封装数据库基本CRUD(创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete))

public interface CrudRepository<T, ID extends Serializable>
 extends Repository<T, ID> {

  S save(S entity);

 T findOne(ID primaryKey);    

 Iterable findAll();     

 Long count();          

 void delete(T entity);     

 boolean exists(ID primaryKey); 

 // … more functionality omitted.
}

2、分页排序查询

public interface PagingAndSortingRepository<T, ID extends Serializable>
 extends CrudRepository<T, ID> {

 Iterable findAll(Sort sort);

 Page findAll(Pageable pageable);
}

//Accessing the second page by a page size of 20
PagingAndSortingRepository repository = // … get access to a bean
Page users = repository.findAll(new PageRequest(1, 20));

3、计数

public interface UserRepository extends CrudRepository<User, Long> {

 Long countByLastname(String lastname);
}

4、删除

public interface UserRepository extends CrudRepository<User, Long> {

 Long deleteByLastname(String lastname);

 List removeByLastname(String lastname);

}

5、自定义查询方法自动注入
声明一个接口继承Repository

interface PersonRepository extends Repository {
List findByLastname(String lastname);
}

保证注入了elasticsearch配置
在bootstrap.yml中写入了spring-data-elasticsearch的配置文件将自动注入
注入调用

public class SomeClient {

 @Autowired
 private PersonRepository repository;

 public void doSomething() {
  List persons = repository.findByLastname("Matthews");
 }
}

6、支持Java8 Stream查询和sql语句查询

@Query("select u from User u")
Stream findAllByCustomQueryAndStream();

Stream readAllByFirstnameNotNull();

@Query("select u from User u")
Stream streamAllPaged(Pageable pageable);

try (Stream stream = repository.findAllByCustomQueryAndStream()) {
 stream.forEach(…);
}

7、支持异步查询

@Async
Future findByFirstname(String firstname);        

@Async
CompletableFuture findOneByFirstname(String firstname);

@Async
ListenableFuture findOneByLastname(String lastname);

支持原生es JavaAPI

1、NativeSearchQueryBuilder构建查询

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

SearchQuery searchQuery = new NativeSearchQueryBuilder()
  .withQuery(matchAllQuery())
  .withFilter(boolFilter().must(termFilter("id", documentId)))
  .build();

Page sampleEntities =
  elasticsearchTemplate.queryForPage(searchQuery,SampleEntity.class);

2、利用Scan和Scroll进行大结果集查询

SearchQuery searchQuery = new NativeSearchQueryBuilder()
  .withQuery(matchAllQuery())
  .withIndices("test-index")
  .withTypes("test-type")
  .withPageable(new PageRequest(0,1))
  .build();
String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false);
List sampleEntities = new ArrayList();
boolean hasRecords = true;
while (hasRecords){
  Page page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper()
  {
    @Override
    public Page mapResults(SearchResponse response) {
      List chunk = new ArrayList();
      for(SearchHit searchHit : response.getHits()){
        if(response.getHits().getHits().length <= 0) {
          return null;
        }
        SampleEntity user = new SampleEntity();
        user.setId(searchHit.getId());
        user.setMessage((String)searchHit.getSource().get("message"));
        chunk.add(user);
      }
      return new PageImpl(chunk);
    }
  });
  if(page != null) {
    sampleEntities.addAll(page.getContent());
    hasRecords = page.hasNextPage();
  }
  else{
    hasRecords = false;
  }
  }
}

3、获取client实例进行节点操作,可以自行封装Util方法

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

public void searchHelper() throws IOException {

    //节点客户端
    // on startup
//    Node node = nodeBuilder().clusterName("syncwt-es").client(true).node();
//    Client nodeClient = node.client();

    //传输客户端
//    Settings settings = Settings.settingsBuilder().build();
//    Client transportClient = TransportClient.builder().settings(settings).build();

    Client transportClient = elasticsearchTemplate.getClient();

    Customer customer = new Customer("Alice", "Smith");

    // instance a json mapper
    ObjectMapper mapper = new ObjectMapper(); // create once, reuse

    // generate json
    String json = mapper.writeValueAsString(customer);
    System.out.println("--------------------------------jackson mapper");
    System.out.println(json);

    XContentBuilder builder = jsonBuilder()
        .startObject()
        .field("firstName", "Alice")
        .field("latName", "Smith")
        .endObject();
    System.out.println("--------------------------------jsonBuilder");
    System.out.println(builder.string());

    IndexResponse response = transportClient.prepareIndex("es-customer", "customer")
        .setSource(jsonBuilder()
            .startObject()
            .field("firstName", "Alice")
            .field("latName", "Smith")
            .endObject()
        )
        .execute()
        .actionGet();

    System.out.println("--------------------------------response");
    System.out.println(response.toString());

    // on shutdown
//    node.close();
//    nodeClient.close();
    transportClient.close();

  }

你可能感兴趣的:(elasticsearch,java)