Springboot整合Elasticsearch7.X实现高级查询和基本的CRUD

1.搭建一个Springboot项目,导入如下依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.配置yml

  elasticsearch:
    uris: http://localhost:9200
    #连接超时时间
    connection-timeout: 20000ms
  data:
    elasticsearch:
      repositories:
        #是否持久化
        enabled: true

3.准备一个domain实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
/** indexName索引名称 ,createIndex是否创建索引,默认为true,shards分片机制,replicas代表副本默认为1*/
@Document(indexName = "t_user",createIndex = true,shards = 5,replicas = 1)
public class User {

    @Id()
    private Long Id;

    /** FieldType.Keyword代表不分词*/
    @Field(type =FieldType.Keyword)
    private String nickName;

    /** FieldType.Text代表分词,这里可以指定分词器 analyzer*/
    @Field(type = FieldType.Text)
    private String passWord;

    /** FieldType.Date, pattern="dd.MM.uuuu 时间格式uuuu代替yyyy*/
    @Field(type=FieldType.Date, format= DateFormat.basic_date)
    private String registerDate;

}

关于文档映射就不多做简绍了,有兴趣的朋友可以去看下官网Spring Data Elasticsearch - Reference Documentation

4.准备一个测试类


@SpringBootTest
class DemoApplicationTests {
    @Test
   public void contextLoads() {

    }

5.开始操作ES

        5.1添加和更新

        

 @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
   public void contextLoads() {
        LinkedList<User> list = new LinkedList<>();
        list.add(new User(1L,"张三","12345","2022-03-26"));
        list.add(new User(2L,"李四","321","2022-03-26"));
         elasticsearchRestTemplate.save(list);

    }

然后运行访问我们的可视化界面;http://localhost:9100/ ,没有的小伙伴需要去安装哦,添加成功

 更新的话和添加的话区别不大,我们只需要去更新我们的参数就好了

        5.2搜索单个用matchQuery

   @Test
   public void contextLoads() throws ParseException {
   NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("nickName", "四")).build();
        SearchHits<User> search = elasticsearchRestTemplate.search(query, User.class);
        search.getSearchHits().forEach(System.out::println);
    }

查询成功

 查询多个就用matchAllQuery()就好了

        5.3删除

  @Test
   public void contextLoads() throws ParseException {

       elasticsearchRestTemplate.delete("1", IndexCoordinates.of("t_user"));


    }

IndexCoordinates.of()里面填写我们的索引库,删除成功

6.分页,高亮,排序

  @Test
   public void contextLoads() throws ParseException {
       NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("nickName", "李"))
               // 分页加排序
               .withPageable(PageRequest.of(0,5, Sort.Direction.DESC,"_id"))
               // 高亮
               .withHighlightFields(new HighlightBuilder.Field("nickName")).build();
        SearchHits<User> search = elasticsearchRestTemplate.search(query, User.class);
        search.getSearchHits().forEach(System.out::println);

    }

效果;

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值