史上最全的ElasticSearch系列之实战SpringBoot+spring-data-elasticsearch(下)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 前言文本已收录至我的GitHub仓库,欢迎Star:github.com/bin39232820…种一棵树最好的时间是十年前,其次是现在


ElasticsearchRepository这个接口,只要继承了这个类就可以实现基本的增删改查

打开这个类我们发现:

@NoRepositoryBean
public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {
    <S extends T> S index(S var1);
    Iterable<T> search(QueryBuilder var1);
    Page<T> search(QueryBuilder var1, Pageable var2);
    Page<T> search(SearchQuery var1);
    Page<T> searchSimilar(T var1, String[] var2, Pageable var3);
    void refresh();
    Class<T> getEntityClass();
}
复制代码


关键字

关键字 使用示例 等同于的ES查询
And findByNameAndPrice {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Or findByNameOrPrice {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}}
Is findByName {“bool” : {“must” : {“field” : {“name” : “?”}}}}
Not findByNameNot {“bool” : {“must_not” : {“field” : {“name” : “?”}}}}
Between findByPriceBetween {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
LessThanEqual findByPriceLessThan {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
GreaterThanEqual findByPriceGreaterThan {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Before findByPriceBefore {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}}
After findByPriceAfter {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}}
Like findByNameLike {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
StartingWith findByNameStartingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}}
EndingWith findByNameEndingWith {“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}}
Contains/Containing findByNameContaining {“bool” : {“must” : {“field” : {“name” : {“query” : “?”,”analyze_wildcard” : true}}}}}
In findByNameIn(Collectionnames) {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}}
NotIn findByNameNotIn(Collectionnames) {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}}
True findByAvailableTrue {“bool” : {“must” : {“field” : {“available” : true}}}}
False findByAvailableFalse {“bool” : {“must” : {“field” : {“available” : false}}}}
OrderBy findByAvailableTrueOrderByNameDesc {“sort” : [{ “name” : {“order” : “desc”} }],”bool” : {“must” : {“field” : {“available” : true}}}}

再来一个分页的例子


QuestionElasticsearchRepository

package com.gf.repository;
import com.gf.entity.FQuestionElasticssearch;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface QuestionElasticsearchRepository extends ElasticsearchRepository<FQuestionElasticssearch, Long> {
    Page<FQuestionElasticssearch> findByCatory(String catory, Pageable pageable);
    @Query("{ \"bool\":{ \"must\":[ { \"multi_match\": { \"query\": \"?0\", \"type\": \"most_fields\", \"fields\": [ \"title\", \"content\" ] } }, { \"match\": { \"catory\": \"?1\" } } ] } } ")
    Page<FQuestionElasticssearch> searchByKeyWordsAndCatory(String keyword, String catory, Pageable pageable);
}
复制代码


实体 FQuestionElasticssearch

package com.gf.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "pomit", type = "issue", createIndex = false)
public class FQuestionElasticssearch {
    @Id
    private Long id;
    private String title;
    private String catory;
    private String content;
    public FQuestionElasticssearch() {
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setCatory(String catory) {
        this.catory = catory;
    }
    public String getCatory() {
        return catory;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
复制代码


Service

package com.gf.service;
import com.gf.entity.FQuestionElasticssearch;
import com.gf.repository.QuestionElasticsearchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class QuestionElasticsearchService {
    @Autowired
    QuestionElasticsearchRepository questionElasticsearchRepository;
    public Page<FQuestionElasticssearch> pageByOpenAndCatory(Integer page, Integer size, String catory,
                                                             String keyWord) {
        Pageable pageable = PageRequest.of(page, size);
        if (StringUtils.isEmpty(keyWord)) {
            return questionElasticsearchRepository.findByCatory(catory, pageable);
        } else {
            return questionElasticsearchRepository.searchByKeyWordsAndCatory(keyWord, catory, pageable);
        }
    }
}
复制代码


Controller

package com.gf.controller;
import com.gf.entity.FQuestionElasticssearch;
import com.gf.service.QuestionElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/elsearch")
public class ElasticsearchRest {
    @Autowired
    QuestionElasticsearchService questionElasticsearchService;
    @RequestMapping(value = "/test", method = { RequestMethod.GET })
    public List<FQuestionElasticssearch> test(@RequestParam(value = "value", required = false) String value) {
        return questionElasticsearchService.pageByOpenAndCatory(0, 10, "Spring专题", value).getContent();
    }
}
复制代码


结果


这个里面我具体来说说

@Query("{ \"bool\":{ \"must\":[ { \"multi_match\": { \"query\": \"?0\", \"type\": \"most_fields\", \"fields\": [ \"title\", \"content\" ] } }, { \"match\": { \"catory\": \"?1\" } } ] } } ")
    Page<FQuestionElasticssearch> searchByKeyWordsAndCatory(String keyword, String catory, Pageable pageable);
复制代码


这个的意思吧,其实这个和所有的springData JPA是一样的,它是为了拼成这样的条件。里面的分页有是有了的


结尾


今天就稍微讲了下这个springDataElasticsearch的增删改查,反正如果是初学的话,就先做到这,要用的时候再去深入吧

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
2天前
|
Java 开发者 Spring
深入解析 @Transactional:Spring 事务管理的艺术及实战应对策略
深入解析 @Transactional:Spring 事务管理的艺术及实战应对策略
7 2
|
2天前
|
Dubbo Java 应用服务中间件
Spring Boot 调用 Dubbo 接口与编写 Dubbo 接口实战
Spring Boot 调用 Dubbo 接口与编写 Dubbo 接口实战
10 1
|
2天前
|
监控 Java 数据库
Spring Boot与Spring Batch的深度集成
Spring Boot与Spring Batch的深度集成
|
2天前
|
安全 Java 数据安全/隐私保护
使用Spring Boot和Spring Security保护你的应用
使用Spring Boot和Spring Security保护你的应用
|
2天前
|
SQL Java 数据库
使用Spring Boot和Spring Data JPA进行数据库操作
使用Spring Boot和Spring Data JPA进行数据库操作
|
2天前
|
存储 搜索推荐 Java
Spring Boot中如何集成ElasticSearch进行全文搜索
Spring Boot中如何集成ElasticSearch进行全文搜索
|
2天前
|
JSON 安全 Java
Spring Boot与WebFlux的实战案例
Spring Boot与WebFlux的实战案例
|
2天前
|
存储 搜索推荐 Java
Spring Boot与Elasticsearch的集成应用
Spring Boot与Elasticsearch的集成应用
|
2天前
|
缓存 数据处理 数据安全/隐私保护
Elasticsearch索引状态管理实战指南
Elasticsearch索引状态管理实战指南
6 0
|
2天前
|
JSON 安全 Java
Spring Boot与WebFlux的实战案例
Spring Boot与WebFlux的实战案例