【搜索引擎】浅谈ElasticSearch在项目中的应用

  ElasticSearch(简称ES)是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。它也是目前最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。
  安装ES、分词插件IK、图形插件Head,这些博主这里就不介绍了。
  在讲代码之前,博主先说一下我对索引的理解,比如有个题目:背诵《悯农》,我们立刻就能背诵出来“锄禾日当午……”,这是因为我们的脑海里面有“悯农=>锄禾日当午”这个索引关系。但如果把题目改成:背诵含有“午”的诗句。那么恐怕我们都要想一会才能背诵出来,这是因为我们的脑海里面没有有“午=>锄禾日当午”这个索引关系。如果我们能够在脑海里面建立“锄=>悯农=>锄禾日当午”、“禾=>悯农=>锄禾日当午”、“日=>悯农=>锄禾日当午”、“当=>悯农=>锄禾日当午”、“午=>悯农=>锄禾日当午”这些索引关系,那么是不是无论题目怎么问,我们都可以很快就能背诵出来了?
  ES中建立索引的过程肯定没这么简单,我也没具体研究过。还有就是,我们可以把ES中的“索引 => 类型 => 文档 => 字段”想象成关系型数据库中的“数据库 => 表 => 行 => 列”,这样看代码的时候就比较容易理解了。
  先说明一下,SpringBoot继承ES有两种方式,第一种写一个Repository实现类,继承 ElasticsearchRepository ;第二种是用ElasticsearchTemplate,这个类是SpringBoot给封装的,可以直接在 Service或者Controller层 中引入该 bean。这里我们讲第二种。
  博主是做java后台开发的,平时不怎么写Web端代码,所以这篇博客的代码也是博主照着“java知识分享网”上面的一个百度云资源分享论坛来敲的,也算是“现炒现卖”,和大家一起学习。
  第一步,要在pom.xml文件里面添加ES依赖,如下。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

  第二步,在application.yml文件中添加ES配置信息,如下。

spring:
  data:
    elasticsearch:
      #集群名称,默认为elasticsearh
      cluster-name: elastic 
      #ES节点信息,逗号分隔
      cluster-nodes: 10.4.1.88:9300,10.4.1.89:9300

  第三步,写一个ArticleInfo实体类,注意不要忘了写@Document注解。

package com.zznode.entity.es;

import org.springframework.data.elasticsearch.annotations.Document;

import java.io.Serializable;

/**
 * 用于ES的帖子实体类
 * 
 * @author Administrator
 *
 */
@Document(indexName = "test2", type = "my")
public class ArticleInfo implements Serializable {
   

	private Long id;
	private String name;
	private String content;

	public Long getId() {
   
		return id;
	}

	public void setId(Long id) {
   
		this.id = id;
	}

	public String getName() {
   
		return name;
	}

	public void setName(String name) {
   
		this.name = name;
	}

	public String getContent() {
   
		return content;
	}

	public void setContent(String content) {
   
		this.content = content;
	}
}

  第四步,编写ES的Service层代码,如下,这是一个百度云资源分享论坛的Service层代码,其中,search方法是根据关键字分页查询ES中的信息,并高亮显示;searchNoHighLight方法是根据关键字分页查询ES中的信息,不高亮显示;searchCount方法是根据关键字,查询ES中符合条件信息的条数;deleteIndex方法是根据文档id来删除ES中对应的信息。

package com.zznode.service.impl;

import com.zznode.entity.Article;
import com.zznode.entity.es.ArticleInfo;
import com.zznode.repository.ArticleRepository;
import com.zznode.service.ArticleService;
import com.zznode.util.StringUtil;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
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.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值