Spring Boot + Elasticsearch 实现索引的日常维护

全文检索的应用越来越广泛,几乎成了互联网应用的标配,商品搜索、日志分析、历史数据归档等等,各种场景都会涉及到大批量的数据,在全文检索方面,方案无外乎Lucene、Solr、Elasticsearch三种应用的较为广泛。es、solr的底层都依托于Lucene,但es比solr学习成本更低,由于其提供的RESTful API简单快捷,对互联网应用开发而言更是如虎添翼。

下面结合以实际案例,通过Java API的形式操作es数据集。

框架选型基础是Spring Boot + Spring-data-elasticsearch + elasticsearch。

使用ElasticsearchRepository的形式来连接、维护ES数据集,ElasticsearchRepository中提供了简单的操作索引数据的方法集合,继承自ElasticsearchCrudRepository,涵盖了CRUD、排序、分页等常见的基本操作功能。

 
  1. @NoRepositoryBean  

  2. public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {  

  3.    <S extends T> S index(S var1);  

  4.  

  5.    Iterable<T> search(QueryBuilder var1);  

  6.  

  7.    Page<T> search(QueryBuilder var1, Pageable var2);  

  8.  

  9.    Page<T> search(SearchQuery var1);  

  10.  

  11.    Page<T> searchSimilar(T var1, String[] var2, Pageable var3);  

  12.  

  13.    void refresh();  

  14.  

  15.    Class<T> getEntityClass();  

  16. }  

从基本的pom配置开始

 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2.    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3.    <modelVersion>4.0.0</modelVersion>

  4.    <groupId>com.esp.index.data</groupId>

  5.    <artifactId>esp-cube</artifactId>

  6.    <version>0.0.1-SNAPSHOT</version>

  7.  

  8.    <parent>

  9.        <groupId>org.springframework.boot</groupId>

  10.        <artifactId>spring-boot-starter-parent</artifactId>

  11.        <version>1.5.2.RELEASE</version>

  12.        <relativePath /> <!-- lookup parent from repository -->

  13.    </parent>

  14.  

  15.    <properties>

  16.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  17.        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  18.        <java.version>1.7</java.version>

  19.    </properties>

  20.  

  21.    <dependencies>

  22.        <dependency>

  23.            <groupId>org.springframework.boot</groupId>

  24.            <artifactId>spring-boot-starter-jdbc</artifactId>

  25.            <exclusions>

  26.                <exclusion>

  27.                    <groupId>org.apache.tomcat</groupId>

  28.                    <artifactId>tomcat-jdbc</artifactId>

  29.                </exclusion>

  30.            </exclusions>

  31.        </dependency>

  32.        <dependency>

  33.            <groupId>org.springframework.boot</groupId>

  34.            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>

  35.        </dependency>

  36.        <dependency>

  37.            <groupId>org.springframework.boot</groupId>

  38.            <artifactId>spring-boot-starter-web</artifactId>

  39.            <exclusions>

  40.                <exclusion>

  41.                    <artifactId>log4j-over-slf4j</artifactId>

  42.                    <groupId>org.slf4j</groupId>

  43.                </exclusion>

  44.            </exclusions>

  45.        </dependency>

  46.        <dependency>

  47.            <groupId>org.springframework.boot</groupId>

  48.            <artifactId>spring-boot-starter</artifactId>

  49.            <exclusions>

  50.                <exclusion>

  51.                    <groupId>org.springframework.boot</groupId>

  52.                    <artifactId>spring-boot-starter-logging</artifactId>

  53.                </exclusion>

  54.            </exclusions>

  55.        </dependency>

  56.        <dependency>

  57.            <groupId>org.springframework.boot</groupId>

  58.            <artifactId>spring-boot-starter-test</artifactId>

  59.            <scope>test</scope>

  60.        </dependency>

  61.        <dependency>

  62.            <groupId>org.springframework.boot</groupId>

  63.            <artifactId>spring-boot-starter-log4j</artifactId>

  64.            <version>1.3.1.RELEASE</version>

  65.        </dependency>

  66.    </dependencies>

  67.  

  68.    <build>

  69.        <finalName>esp-cube</finalName>

  70.        <plugins>

  71.            <plugin>

  72.                <groupId>org.springframework.boot</groupId>

  73.                <artifactId>spring-boot-maven-plugin</artifactId>

  74.            </plugin>

  75.        </plugins>

  76.    </build>

  77. </project>

编写自己的Resository操作类

 
  1. public interface ArticleSearchRepository extends ElasticsearchRepository<Article, Long>{

  2.    List<Article> findByAbstractsAndContent(String abstracts, String content);

  3. }

其中Article为是与elasticsearch连接的实体类,类似于PO的概念,其中指定的索引名称、类型名称、及分片、副本数量等要素。

 
  1. @Data

  2. @Document(indexName = "article_index", type = "article", shards = 5, replicas = 1, indexStoreType = "fs", refreshInterval = "-1")

  3. public class Article implements Serializable {

  4.  

  5.    /**

  6.     * serialVersionUID:

  7.     *

  8.     * @since JDK 1.6

  9.     */

  10.    private static final long serialVersionUID = 1L;

  11.  

  12.    @Id

  13.    private Long id;

  14.    /** 标题 */

  15.    private String title;

  16.    /** 摘要 */

  17.    private String abstracts;

  18.    /** 内容 */

  19.    private String content;

  20.    /** 发表时间 */

  21.    @Field(format = DateFormat.date_time, index = FieldIndex.no, store = true, type = FieldType.Object)

  22.    private Date postTime;

  23.    /** 点击率 */

  24.    private Long clickCount;

  25. }

我们需要定义域的实体和一个Spring data的基本的CRUD支持库类。用id注释定义标识符字段,如果你没有指定ID字段,Elasticsearch不能索引你的文件。同时需要指定索引名称类型,@Document注解也有助于我们设置分片和副本数量。

接口类

 
  1. public interface ArticleService {

  2.  

  3.    /**

  4.     * saveArticle: 写入<br/>

  5.     *

  6.     * @author guooo Date:2017年9月27日下午3:20:06

  7.     * @param article

  8.     * @return

  9.     * @since JDK 1.6

  10.     */

  11.    long saveArticle(Article article);

  12.  

  13.    /**

  14.     * deleteArticle: 删除,并未真正删除,只是查询不到<br/>

  15.     *

  16.     * @author guooo Date:2017年9月27日下午3:20:08

  17.     * @param id

  18.     * @since JDK 1.6

  19.     */

  20.    void deleteArticle(long id);

  21.  

  22.    /**

  23.     * findArticle: <br/>

  24.     *

  25.     * @author guooo Date:2017年9月27日下午3:20:10

  26.     * @param id

  27.     * @return

  28.     * @since JDK 1.6

  29.     */

  30.    Article findArticle(long id);

  31.  

  32.    /**

  33.     * findArticlePageable: <br/>

  34.     *

  35.     * @author guooo Date:2017年9月27日下午3:20:13

  36.     * @return

  37.     * @since JDK 1.6

  38.     */

  39.    List<Article> findArticlePageable();

  40.  

  41.    /**

  42.     * findArticleAll: <br/>

  43.     *

  44.     * @author guooo Date:2017年9月27日下午3:20:15

  45.     * @return

  46.     * @since JDK 1.6

  47.     */

  48.    List<Article> findArticleAll();

  49.  

  50.    /**

  51.     * findArticleSort: <br/>

  52.     *

  53.     * @author guooo Date:2017年9月27日下午3:20:18

  54.     * @return

  55.     * @since JDK 1.6

  56.     */

  57.    List<Article> findArticleSort();

  58.  

  59.    /**

  60.     * search: <br/>

  61.     *

  62.     * @author guooo Date:2017年9月27日下午3:20:22

  63.     * @param content

  64.     * @return

  65.     * @since JDK 1.6

  66.     */

  67.    List<Article> search(String content);

  68.  

  69.    /**

  70.     * update: es没有修改操作,结合save操作完成<br/>

  71.     *

  72.     * @author guooo Date:2017年9月27日下午3:20:25

  73.     * @param id

  74.     * @return

  75.     * @since JDK 1.6

  76.     */

  77.    long update(long id);

  78. }

接口实现

 
  1. @Service

  2. public class ArticleServiceImpl implements ArticleService {

  3.  

  4.    final int page = 0;

  5.    final int size = 10;

  6.  

  7.    /* 搜索模式 */

  8.    String SCORE_MODE_SUM = "sum"; // 权重分求和模式

  9.    Float MIN_SCORE = 10.0F; // 由于无相关性的分值默认为 1 ,设置权重分最小值为 10

  10.  

  11.    Pageable pageable = new PageRequest(page, size);

  12.  

  13.    @Autowired

  14.    ArticleSearchRepository repository;

  15.  

  16.    @Override

  17.    public long saveArticle(Article article) {

  18.        Article result = repository.save(article);

  19.        return result.getId();

  20.    }

  21.  

  22.    @Override

  23.    public void deleteArticle(long id) {

  24.        repository.delete(id);

  25.    }

  26.  

  27.    @Override

  28.    public Article findArticle(long id) {

  29.        return repository.findOne(id);

  30.    }

  31.  

  32.    @Override

  33.    public List<Article> findArticlePageable() {

  34.  

  35.        return repository.findAll(pageable).getContent();

  36.    }

  37.  

  38.    @Override

  39.    public List<Article> findArticleAll() {

  40.        Iterable<Article> iterables = repository.findAll();

  41.        List<Article> articles = new ArrayList<>();

  42.        for (Article article : iterables) {

  43.            articles.add(article);

  44.        }

  45.        return articles;

  46.    }

  47.  

  48.    @Override

  49.    public List<Article> findArticleSort() {

  50.        List<Order> orders = new ArrayList<>();

  51.        Order order = new Order(Direction.ASC, "clickCount");

  52.        orders.add(order);

  53.        Sort sort = new Sort(orders);

  54.        Iterable<Article> iterables = repository.findAll(sort);

  55.        List<Article> articles = new ArrayList<>();

  56.        for (Article article : iterables) {

  57.            articles.add(article);

  58.        }

  59.        return articles;

  60.    }

  61.  

  62.    @Override

  63.    public List<Article> search(String content) {

  64.        return repository.findByAbstractsAndContent(content, content);

  65.    }

  66.  

  67.    @Override

  68.    public long update(long id) {

  69.        Article article = repository.findOne(id);

  70.        article.setTitle("test");

  71.        Article retun = repository.save(article);

  72.        System.out.println(retun.getId()+"更新的数据");

  73.        return retun.getId();

  74.    }

  75. }

是不是与JPA、hibernate操作数据集的手法很类似?

controller方法类:

 
  1. @RestController

  2. @RequestMapping(value = "/article")

  3. public class APIArticleController {

  4.  

  5.    @Autowired

  6.    ArticleService articleService;

  7.  

  8.  

  9.    @RequestMapping(value = "save", method = RequestMethod.POST)

  10.    public long save() {

  11.        for (int i = 10000; i < 12000; i++) {

  12.            Article article = new Article();

  13.            article.setClickCount(Long.valueOf(i + RandomUtils.nextInt(23, i)));

  14.            article.setAbstracts("我的一个测试" + i);

  15.            article.setContent(i + "这是第一个测试的内容@spring-data-elasticsearch");

  16.            article.setPostTime(new Date());

  17.            article.setId(Long.valueOf(RandomUtils.nextLong(i, i)));

  18.            long _id = articleService.saveArticle(article);

  19.            System.out.println(_id);

  20.        }

  21.        return 23;

  22.    }

  23.  

  24.    @RequestMapping(value = "delete", method = RequestMethod.POST)

  25.    public void deleteArticle(long id) {

  26.        articleService.deleteArticle(id);

  27.    }

  28.  

  29.    @RequestMapping(value = "findOne", method = RequestMethod.POST)

  30.    public Article findArticle(long id) {

  31.        return articleService.findArticle(id);

  32.    }

  33.  

  34.    @RequestMapping(value = "findArticlePageable", method = RequestMethod.POST)

  35.    public List<Article> findArticlePageable() {

  36.        return articleService.findArticlePageable();

  37.    }

  38.  

  39.    @RequestMapping(value = "findArticleAll", method = RequestMethod.POST)

  40.    public List<Article> findArticleAll() {

  41.        return articleService.findArticleAll();

  42.    }

  43.  

  44.    @RequestMapping(value = "findArticleSort", method = RequestMethod.POST)

  45.    public List<Article> findArticleSort() {

  46.        return articleService.findArticleSort();

  47.    }

  48.  

  49.    @RequestMapping(value = "search", method = RequestMethod.POST)

  50.    public List<Article> search(String content) {

  51.        return articleService.search(content);

  52.    }

  53.  

  54.    @RequestMapping(value = "update", method = RequestMethod.POST)

  55.    public long update(long id) {

  56.        return articleService.update(id);

  57.    }

  58. }

Spring Boot的启动类及配置项,这里略过,项目启动后,可能过controller暴露出来的方法进行Article数据索引的CRUD操作。

扩展阅读:

0?wx_fmt=gif&wxfrom=5&wx_lazy=1

歪脖贰点零 ∣迭代当下 · 架构未来

640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1

程序员,除了编码,生活还应该有沉淀!

长按,识别二维码,加关注

 

转载于:https://my.oschina.net/u/3669358/blog/1632097

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值