<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Spring Data ElasticSearch的使用

      1、什么是Spring Data

        Spring Data是一個用于簡化數(shù)據(jù)庫訪問,并支持云服務的開源框架。其主要目標是使得對數(shù)據(jù)的訪問變得方便快捷,并支持map-reduce框架和云計算數(shù)據(jù)服務。 Spring Data可以極大的簡化JPA的寫法,可以在幾乎不用寫實現(xiàn)的情況下,實現(xiàn)對數(shù)據(jù)的訪問和操作。除了CRUD外,還包括如分頁、排序等一些常用的功能。

      2、什么是Spring Data ElasticSearch

        Spring Data ElasticSearch 基于 spring data API 簡化 elasticSearch操作,將原始操作elasticSearch的客戶端API進行封裝 。Spring Data為Elasticsearch項目提供集成搜索引擎。Spring Data Elasticsearch POJO的關(guān)鍵功能區(qū)域為中心的模型與Elastichsearch交互文檔和輕松地編寫一個存儲庫數(shù)據(jù)訪問層。

      3.Spring Data ElasticSearch環(huán)境  

           1.1 環(huán)境搭建
                  步驟一:Spring-Data-ElasticSearch,Spring-test幫助你加載配置文件并且測試
                      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>
       


                  步驟二:創(chuàng)建Spring的核心配置文

       
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
             xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd">
          <!--開啟包掃描-->
          <context:component-scan base-package="com.wdksoft"/>
          <!--配置集群信息-->
          <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>
          <!--掃描Mapper-->
          <elasticsearch:repositories base-package="com.wdksoft.mapper"/>
      </beans>

       

          2.SpringDataES案例
              
              2.1 添加索引庫 

       
             @RunWith(SpringJUnit4ClassRunner.class)
                  @ContextConfiguration(locations = "classpath:applicationContext.xml")
                  public class SpringDataESTest {
                      //植入模板對象
                      @Resource
                      private ElasticsearchTemplate elasticsearchTemplate;
      
                      @Test
                      public void createIndex(){
                          //創(chuàng)建空的索引庫
                          elasticsearchTemplate.createIndex(Hello.class);
                          
                      }
                  }

       

              2.2 添加索引庫并且指定Mapping信息
                  利用POJO映射Mapping信

       
               @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(){
                          //創(chuàng)建空的索引庫
                          elasticsearchTemplate.+(Hello.class);
                          elasticsearchTemplate.putMapping(Hello.class);
                          
                      }

       

              2.3 添加文檔數(shù)據(jù)
                  創(chuàng)建Mapper層接口:

       
                      /**
                       * 數(shù)據(jù)訪問層接口
                       */
                      @Repository
                      public interface HelloMapper extends ElasticsearchRepository<Hello,Long> {
                      }

                  創(chuàng)建Service層接口

                      public interface HelloService {
                          public void save(Hello hello);
                      }

                  創(chuàng)建Service層接口實現(xiàn)類

       
                      @Service("helloService")
                      public class HelloServiceImpl implements HelloService {
                          //植入Mapper層對象
                          @Resource
                          private HelloMapper helloMapper;
      
                          @Override
                          public void save(Hello hello) {
                              helloMapper.save(hello);
                          }
                     }

                  測試:

       
      /**
           * 添加文檔數(shù)據(jù)
           */
          @Test
          public void createDocument(){
              for(long i=1l;i<=10l;i++){
                  Hello hello=new Hello();
                  hello.setId(i);
                  hello.setTitle("新增的Title數(shù)據(jù)"+i);
                  hello.setContent("新增的Content數(shù)據(jù)"+i);
                  helloService.save(hello);
              }
          

       

              2.4 刪除文檔數(shù)據(jù)
                      Service層接口

                          //根據(jù)文檔ID刪除
                          public void deleteById(long id);
                          //刪除全部
                          public void deleteAll();

                      Service層接口實現(xiàn)類

       
                          @Override
                          public void deleteById(long id) {
                              helloMapper.deleteById(id);
                          }
      
                          @Override
                          public void deleteAll() {
                              helloMapper.deleteAll();
                          }

                      測試:

       
                  /**
                           * 刪除文檔數(shù)據(jù)
                           */
                          @Test
                          public void deleteDocument(){
                              //根據(jù)文檔ID刪除
                              helloService.deleteById(1l);
                              //全部刪除
                              helloService.deleteAll();
                          }

       

              2.5 修改文檔數(shù)據(jù)

        修改也是調(diào)用save方法,如果id不存在則添加,id存在則先刪除再添加

       
                  /**
                   * 修改文檔數(shù)據(jù):先刪除再添加,調(diào)用的還是save方法
                   */
                  @Test
                  public void updateDocument(){
                      Hello hello=new Hello();
                      hello.setId(1l);
                      hello.setTitle("[修改]新增的Title數(shù)據(jù)");
                      hello.setContent("[修改]新增的Content數(shù)據(jù)");
                      helloService.save(hello);
                  }

       


              2.6 根據(jù)ID查詢
                  Service層接口

                          //根據(jù)文檔ID查詢數(shù)據(jù)
                          public Optional<Hello> findById(Long id);

                  Service層接口實現(xiàn)類

                          @Override
                          public Optional<Hello> findById(Long id) {
                              return helloMapper.findById(id);
                          }

                  測試:

       
                      /**
                       * 根據(jù)文檔ID查詢
                       */
                      @Test
                      public void getDocumentById(){
                          Optional<Hello> optional = helloService.findById(2l);
                          Hello hello = optional.get();
                          System.out.println(hello);
                      

       

              
              2.7 查詢?nèi)课臋n數(shù)據(jù)
                  Service層接口

                      //查詢所有數(shù)據(jù)
                      public Iterable<Hello> findAll();
                      //查詢所有數(shù)據(jù)包含分頁
                      public Page<Hello> findAll(Pageable pageable);

                  Service層接口實現(xiàn)類

       
                      @Override
                      public Iterable<Hello> findAll() {
                          return helloMapper.findAll();
                      }
      
                      @Override
                      public Page<Hello> findAll(Pageable pageable) {
                          return helloMapper.findAll(pageable);
                      }

                  測試:    

       
                      /**
                       * 查詢所有文檔數(shù)據(jù)
                       */
                      @Test
                      public void getAllDocument(){
                          Iterable<Hello> iterable = helloService.findAll();
                          iterable.forEach(item-> System.out.println(item));
                      }
                      /**
                       * 查詢所有文檔數(shù)據(jù)加分頁
                       */
                      @Test
                      public void getDocumentPage(){
                          //指定分頁規(guī)則
                          Page<Hello> page = helloService.findAll(PageRequest.of(0, 5));
                          for (Hello hello:page.getContent()) {
                              System.out.println(hello);
                          }
                      }

        

              2.8 根據(jù)查詢方法的自定義規(guī)則進行數(shù)據(jù)查詢
                  根據(jù)Title域進行查詢,并且加分頁
                      Mapper層接口:

       
                          /**
                           * 數(shù)據(jù)訪問層接口
                           */
                          @Repository
                          public interface HelloMapper extends ElasticsearchRepository<Hello,Long> {
                              //根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
                              public List<Hello> findByTitle(String str);
                              //根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
                              public List<Hello> findByTitle(String str, Pageable pageable);
                          }

                      Service層接口

                              //根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
                              public List<Hello> findByTitle(String str);
                              //根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
                              public List<Hello> findByTitle(String str, Pageable pageable);

                      Service層接口實現(xiàn)類

       
                          @Override
                          public List<Hello> findByTitle(String str) {
                              return helloMapper.findByTitle(str);
                          }
      
                          @Override
                          public List<Hello> findByTitle(String str, Pageable pageable) {
                              return helloMapper.findByTitle(str,pageable);
                          }

                      測試:    

       
                          /**
                           * 根據(jù)條件查詢文檔數(shù)據(jù)加分頁
                           */
                          @Test
                          public void getDocumentByTitle(){
                              /*List<Hello> helloLists = helloService.findByTitle("修改");
                              helloLists.stream().forEach(item-> System.out.println(item));*/
      
                              List<Hello> helloLists = helloService.findByTitle("新增", PageRequest.of(0, 5));
                              helloLists.stream().forEach(item-> System.out.println(item));
      
                          }

       


              2.9 NativeSearchQuery

        將輸入的查詢條件先分詞,再進行查詢

       
                  /**
                   * 根據(jù)一段內(nèi)容查詢數(shù)據(jù):NativeSearchQuery
                   * 先分詞再查詢
                   */
                  @Test
                  public void getDocumentQuery(){
                      //創(chuàng)建NativeSearchQueryBuilder對象
                      NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
                      //指定查詢規(guī)則
                      NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(QueryBuilders.queryStringQuery("搜索新增").defaultField("title"))
                              .withPageable(PageRequest.of(0,5)).build();
                      //使用ESTemplates執(zhí)行查詢
                      List<Hello> hellos = elasticsearchTemplate.queryForList(build, Hello.class);
                      hellos.stream().forEach(item-> System.out.println(item));
                  }

       

      posted @ 2020-03-02 19:06  流氓大隊長  閱讀(2158)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 91精品久久久久久无码人妻| 国产初高中生视频在线观看| 色欲色香天天天综合网站免费| 欧美人与动zozo| 中文熟妇人妻av在线| 久久亚洲精品11p| 国产精品三级中文字幕| 嘉义市| 午夜大尺度福利视频一区| 国产高清自产拍av在线| 丝袜a∨在线一区二区三区不卡| 国产成人无码A区在线观| 日韩精品一区二区三区日韩| 国产精品污双胞胎在线观看| 色综合视频一区二区三区| 久久久精品波多野结衣av| 国产精品久久国产丁香花| 久久亚洲国产精品久久| 4399理论片午午伦夜理片| 亚洲av日韩av永久无码电影| 天堂V亚洲国产V第一次| 精品国产一区二区三区国产区| 国产亚洲视频免费播放| 日韩深夜福利视频在线观看| 国产精品久久久久久av| 亚洲第三十四九中文字幕| 人妻蜜臀久久av不卡| 国产午夜亚洲精品不卡下载| 亚洲av第一区二区三区| 男女xx00xx的视频免费观看| 久久99精品久久久久久| 即墨市| 亚洲日本精品一区二区| 日韩秘 无码一区二区三区 | 无码国产69精品久久久久网站| 亚欧洲乱码视频一二三区| 色又黄又爽18禁免费视频| 久热久精久品这里在线观看| 无码精品人妻一区二区三区中| 国产另类ts人妖一区二区| av在线播放无码线|