SpringBoot集成

可以通过Spring Data Elasticsearch来进行操作,boot对其进行了集成:

  1. 导包

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    4. </dependency>
  2. 配置连接

    spring:
    elasticsearch:
     rest:
       uris:
       - http://localhost:9200
    
  3. 注入模板对象

    @Autowired
    ElasticsearchOperations operations;
    

    对象映射

    ES的文档对应于Java的对象,可以通过Spring-Data-Es提供的注解来进行关联:

    @Data
    @Document(indexName = "woniu48")
    public class Student {
     @Id
     @Field(type = FieldType.Integer,name = "id")
     private Integer id;
     @Field(type = FieldType.Keyword,name="name")
     private String name;
     @Field(type = FieldType.Integer,name="age")
     private Integer age;
     @Field(type = FieldType.Text,analyzer = "ik_max_word",name = "desc")
     private String desc;
    }
    
  • @Document: 在类级别应用,以指示该类是映射到ES的哪一个Index。最重要的属性是:
    • indexName: 用于存储此实体的索引的名称
  • @Id: 用来标识ID
  • @Field: 在字段级别应用并定义字段的属性:
    • name: 字段名称
    • type: 字段类型
    • analyzer: 分词器

      操作

      保存(新增与更新)对象:
      Student stu = new Student();
      stu.setId(3333);
      stu.setName("张三丰");
      stu.setDesc("此人涨得不好看");
      stu.setAge(100);
      operations.save(stu);
      
      删除对象:
      //第一个参数是id
      operations.delete("3333", Student.class);
      
      查询对象:
      查询有三种主要方式:
  1. 通过CriteriaQuery条件来查询,可以简单组合条件,不能分词,适用于非常简单的查询

    //查询年龄在20-25之间或则名称是"admin"的学员
    Criteria c = new Criteria("age").between(20, 25)
     .or("name").is("admin");            
    CriteriaQuery cq = new CriteriaQuery(c);
    SearchHits<Student> shs = operations.search(cq, Student.class);
    System.out.println(shs.getTotalHits());
    shs.getSearchHits().forEach(student -> {
     System.out.println(student.getContent().getName()+":"+student.getContent().getAge());
    });
    
  2. 通过StringQuery来查询,直接在java这边构建查询json字符串,不推荐使用

  3. 通过NativeSearchQuery来构建复杂查询,包括bool查询等等,推荐使用

    /*查询匹配非常漂亮的人或则年龄在90以上的*/
    //构建NativeSearchQuery建造器
    NativeSearchQueryBuilder nsqb = new NativeSearchQueryBuilder();
    //构造Bool查询条件        
    BoolQueryBuilder bqb = new BoolQueryBuilder();
    //通过该Bool查询器构建should查询器
    List<QueryBuilder> should = bqb.should();
    //向should里面添加两个查询条件
    should.add(new MatchQueryBuilder("desc", "非常漂亮的人"));
    should.add(new RangeQueryBuilder("age").gt(90));
    //注册bool查询条件
    nsqb.withQuery(bqb);
    //注册分页(第一页,显示3条)
    nsqb.withPageable(PageRequest.of(0, 3));
    //注册排序(age降序)
    nsqb.withSort(SortBuilders.fieldSort("age").order(SortOrder.DESC));
    //构建NativeSearchQuery
    NativeSearchQuery nsq = nsqb.build();
    //执行查询方法        
    SearchHits<Student> shs = operations.search(nsq, Student.class);
    System.out.println(shs.getTotalHits());
    shs.getSearchHits().forEach(x -> {
     Student stu = x.getContent();
     System.out.println(stu.getName()+":"+stu.getDesc()+":"+stu.getAge());
    });
    
  4. 查询结果高亮显示

    //创建高亮显示器
    HighlightBuilder hb = new HighlightBuilder();
    //设置高亮字段
    hb.field("desc");
    //设置如何高亮显示
    hb.preTags("<span style=\"color:red\">");
    hb.postTags("</span>");
    //设置高亮显示范围以字符为单位
    hb.fragmentSize(50);
    //设置高亮显示的片段数量
    hb.numOfFragments(1);
    NativeSearchQueryBuilder nsqb = new NativeSearchQueryBuilder();
    //注册高亮显示器
    nsqb.withHighlightBuilder(hb);
    nsqb.withQuery(new MatchQueryBuilder("desc", "非常漂亮"));
    NativeSearchQuery nsq = nsqb.build();
    SearchHits<Student> shs = operations.search(nsq, Student.class);
    shs.getSearchHits().forEach(x -> {
     Student stu = x.getContent();
     String desc = stu.getDesc();
     //获取高亮显示字段
     List<String> hs = x.getHighlightField("desc"); 
     if(hs != null && hs.size() > 0) desc = hs.get(0);
     System.out.println(stu.getName()+":"+desc);
    });