SpringDataES

Spring Data:

  ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API

进行封装 。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域
为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层

步骤一:导入依赖

          ESTemplate模板,模板当中包含了一系列的方法
                <dependency>
                  <groupId>org.springframework.data</groupId>
                  <artifactId>spring-data-elasticsearch</artifactId>
                  <version>3.1.9.RELEASE</version>
                  <exclusions>
                    <exclusion>
                      <groupId>org.elasticsearch.plugin</groupId>
                      <artifactId>transport‐netty4‐client</artifactId>
                    </exclusion>
                  </exclusions>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
                <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-test</artifactId>
                  <version>5.1.5.RELEASE</version>
                  <scope>test</scope>
                </dependency>

 

步骤二:大配置文件

          <!--开启包扫描-->
                <context:component-scan base-package="com.mckz"/>
                <!--配置集群信息-->
                <elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch" cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302"/>
                <!--注入ESTemplate模板-->
                <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
                    <constructor-arg name="client" ref="esClient"/>
                </bean>

步骤三:添加索引库

        @RunWith(SpringJUnit4ClassRunner.class)
            @ContextConfiguration(locations = "classpath:applicationContext.xml")
            public class SpringDataESTest {
                //植入模板对象
                @Resource
                private ElasticsearchTemplate elasticsearchTemplate;

                @Test
                public void createIndex(){
                    //创建空的索引库
                    elasticsearchTemplate.createIndex(Hello.class);
                    
                }
            }

步骤四

         @Document(indexName = "my-index",type = "hello")
                public class Hello {
                    @Id
                    @Field(type = FieldType.Long,index = false,store = true)
                    private Long id;
                    @Field(type = FieldType.Text,index = true,store = true,analyzer = "ik_max_word")
                    private String title;
                    @Field(type = FieldType.Text,index = true,store = true,analyzer = "ik_max_word")
                    private String content;
                }
                @Test
                public void createIndex(){
                    //创建空的索引库
                    elasticsearchTemplate.+(Hello.class);
                    elasticsearchTemplate.putMapping(Hello.class);
                    
                }

mapper:

          /**
                 * 数据访问层接口
                 */
                @Repository
                public interface HelloMapper extends ElasticsearchRepository<Hello,Long> {
                }

Service:

          public interface HelloService {
                    public void save(Hello hello);
                    //根据文档ID删除
                    public void deleteById(long id);
                    //删除全部
                    public void deleteAll();
                    //查询所有数据
                    public Iterable<Hello> findAll();
                    //查询所有数据包含分页
                    public Page<Hello> findAll(Pageable pageable);
                    //根据Title域中的关键词查找数据
                    public List<Hello> findByTitle(String str);
                    //根据Title域中的关键词查找数据
                    public List<Hello> findByTitle(String str, Pageable pageable);
                }

 

 

 

Serviceimpl:

         @Service("helloService")
                public class HelloServiceImpl implements HelloService {
                    //植入Mapper层对象
                    @Resource
                    private HelloMapper helloMapper;

                    @Override
                    public void save(Hello hello) {
                        helloMapper.save(hello);
                    }
                    @Override
                    public void deleteById(long id) {
                        helloMapper.deleteById(id);
                    }
                    @Override
                    public void deleteAll() {
                        helloMapper.deleteAll();
                    }
                    @Override
                    public Optional<Hello> findById(Long id) {
                        return helloMapper.findById(id);
                    }
                    @Override
                    public Iterable<Hello> findAll() {
                    return helloMapper.findAll();
                    }
                    @Override
                    public Page<Hello> findAll(Pageable pageable) {
                        return helloMapper.findAll(pageable);
                    }
                }

 

创建测试:

         @Test
                public void createDocument(){
                    Hello hello=new Hello();
                    hello.setId(1l);
                    hello.setTitle("新增的Title数据");
                    hello.setContent("新增的Content数据");
                    helloService.save(hello);

删除测试:

           @Test
                    public void deleteDocument(){
                        //根据文档ID删除
                        helloService.deleteById(1l);
                        //全部删除
                        helloService.deleteAll();
                    }

修改测试:

            @Test
            public void updateDocument(){
                Hello hello=new Hello();
                hello.setId(1l);
                hello.setTitle("[修改]新增的Title数据");
                hello.setContent("[修改]新增的Content数据");
                helloService.save(hello);
            }                            

ID查询测试:

         @Test
                public void getDocumentById(){
                    Optional<Hello> optional = helloService.findById(2l);
                    Hello hello = optional.get();
                    System.out.println(hello);
                }

查询测试

          /**
                 * 查询所有文档数据
                 */
                @Test
                public void getAllDocument(){
                    Iterable<Hello> iterable = helloService.findAll();
                    iterable.forEach(item-> System.out.println(item));
                }
                /**
                 * 查询所有文档数据加分页
                 */
                @Test
                public void getDocumentPage(){
                    //指定分页规则
                    Page<Hello> page = helloService.findAll(PageRequest.of(0, 5));
                    for (Hello hello:page.getContent()) {
                        System.out.println(hello);
                    }
                }

 

posted @ 2020-03-02 19:37  梅川酷子灬  阅读(552)  评论(0编辑  收藏  举报