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来写

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

 

 

 

posted on 2019-04-21 23:59  阿姨哥  阅读(2596)  评论(0编辑  收藏  举报

导航