• es项目实战


    1springboot简单实用

    --依赖

      <!--es的依赖-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>

    --yml的配置

    spring:
      data:
        elasticsearch:
          cluster-name: elasticsearch
          cluster-nodes: 127.0.0.1:9300 #java的客户端要用9300 服务器地址 配置es的地址

    --需要创建一个文档映射

     

    /**
     * 文档映射:
     *
     * index  type
     */
    @Document(indexName = "xxx",type = "emp")
    public class Employee {

        @Id
        private Long id;

        @Field(type = FieldType.Keyword)
        private String name;

        // intro使用text类型进行映射;analyzer:索引创建时使用的分词器
        //searchAnalyzer:搜索分词器:搜索该字段的值时,传入的查询内容的分词器
        @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
        private String intro;

        private Integer  age;

    --启动类

    @SpringBootApplication
    public class EsApplication {
         public static void main(String[] args) {
             SpringApplication.run(EsApplication.class);
         }
    }

    --测试

      --首先进行环境的初始化

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = EsApplication.class)
    public class EsTest {
        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;
        @Test
        public void testInit()throws Exception{
            //创建索引  做映射
            elasticsearchTemplate.createIndex(Employee.class);
            //做映射
            elasticsearchTemplate.putMapping(Employee.class);
        }
    }

      --crud的操作

     @Autowired

        private ElasticsearchTemplate elasticsearchTemplate;

     

        @Autowired

        private EsRepository esRepository;

     

        @Test

        public void testInit() throws Exception {

            //创建索引  做映射

            elasticsearchTemplate.createIndex(Employee.class);

            //做映射

            elasticsearchTemplate.putMapping(Employee.class);

     

        }

     

        @Test

        public void testAdd() throws Exception {

            esRepository.save(new Employee(1L, "111", "I 1", 919));

        }等等

    2.es的项目实战

     --Es的服务单独封装为一个服务--给内部调用的:feign

    --依赖

     

    <!--es的依赖-->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
    </dependency>

    --定义接口

    @FeignClient(value = "xxx-PRIVODER",fallbackFactory = ProductEsClientFactory.class) //表示对哪一个服务进行处理
    @RequestMapping("/xxx/es")
    public interface ProductEsClient {
        //添加一个
        @RequestMapping(value = "/productdoc", method = RequestMethod.POST)
        AjaxResult addOne(@RequestBody  ProductDoc productDoc);

        //批量添加
        @RequestMapping(value = "/productdocs", method = RequestMethod.POST)
        AjaxResult addBatch(@RequestBody List<ProductDoc> productDocList);

        //删除一个
        @RequestMapping(value = "/productdoc/{id}", method = RequestMethod.DELETE)
        AjaxResult deleteOne(@PathVariable("id") Long id );

        //批量删除
        @RequestMapping(value = "/productdocs", method = RequestMethod.DELETE)
        AjaxResult deleteBatch(@RequestBody List<Long> ids );

        //查询一个
        @RequestMapping(value = "/productdoc/{id}", method = RequestMethod.GET)
        AjaxResult findOne(@PathVariable("id") Long id  );

    }

     

     --配置yml文件

    xxx-service:增加配置文件:

    spring:
      application:
        name: xxx-PRIVODER
      data:
        elasticsearch:
          cluster-name: elasticsearch
          cluster-nodes: 127.0.0.1:9300

     

    --定义Repository接口

     

     

    public interface ProductRepository extends ElasticsearchRepository<ProductDoc,Long>{
    }

    --定义service/controller-->按照ProductEsClient来写

    --最后在消费者层写自己的业务逻辑。。。。。。

     

     

     

  • 相关阅读:
    地震逃生【网络流】
    地震逃生【网络流】
    【杭电】[2002]计算球体积
    【杭电】[2002]计算球体积
    【杭电】[2003]求绝对值
    【杭电】[2003]求绝对值
    写在正式写博客之前——博客的意义
    写在正式写博客之前——博客的意义
    初识ACM——活泼的精灵
    初识ACM——活泼的精灵
  • 原文地址:https://www.cnblogs.com/wgyi140724-/p/10747851.html
Copyright © 2020-2023  润新知