一丶ElasticSearch是什么?
1.ES是一个分布式的全文搜索引擎,为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案,ES的索引库管理支持依然是基于Apache Lucene(TM)的开源搜索引擎。
2.ES也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 RESTful API来隐Lucene的复杂性,从而让全文搜索变得简单。
3.总的来说ElasticSearch简化了全文检索lucene的使用,同时增加了分布式的特性,使得构建大规模分布式全文检索变得非常容易。
4.Lucene是apache下的一个开源的全文检索引擎工具包(一堆jar包)。它为软件开发人员提供一个简单易用的工具包(类库),以方便的在小型目标系统中实现全文检索的功能。Lucene适用于中小型项目 ,ES适用于中大型项目(它底层是基于lucene实现的)
二丶SpringCloud项目整合ElasticSearch
1.整一个ElasticSearch模块(名字任取)
2.导入依赖
<-- elasticsearch包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--测试类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
3.编写一个Doc,这里以课程类(course)为例
@Document(indexName = "course" , type = "_doc")
@Data
public class CourseDoc {
// ID
@Id
private Long id;
// 课程名
@Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart") // 指定为 分词
private String name;
// 价格
@Field(type = FieldType.Double)
private BigDecimal price;
// 原价
@Field(type = FieldType.Double)
private BigDecimal priceOld;
// 课程类型
@Field(type = FieldType.Long)
private Long courseTypeId;
// 课程等级
@Field(type = FieldType.Keyword)
private String gradeName;
// 课程封面
@Field(type = FieldType.Keyword)
private String pic;
// 课程上线时间
@Field(type = FieldType.Date)
private Date onlineTime;
// 课程老师名字
@Field(type = FieldType.Keyword)
private String teacherName;
// 课程是否收费 - 收费/免费
@Field(type = FieldType.Keyword)
private String chargeName;
// 课程销量
@Field(type = FieldType.Integer)
private Integer saleCount;
// 课程浏览量
@Field(type = FieldType.Integer)
private Integer viewCount;
}
4.编写repository,并继承ElasticsearchRepository类
@Repository
public interface CourseElasticsearchRepository extends ElasticsearchRepository<CourseDoc,Long> {
}
这里的泛型第一个参数类型是编写的Doc类,第二个参数类型是Doc中id的类型
5.初始化索引库和文档映射
ElasticsearchTemplate 是一个ES的工具类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchStart.class)
public class ESTest {
@Autowired
private ElasticsearchRestTemplate template;
@Test
public void testCreateIndex() {
//创建索引
template.createIndex(CourseDoc.class);
//做文档映射
template.putMapping(CourseDoc.class);
}
6.编写controller接口类
@RestController
@RequestMapping("/es/course")
public class SearchController {
@Autowired
private ISearchService searchService;
@PostMapping("/search")
public JSONResult search(@RequestBody CourseDoc doc){
searchService.search(doc);
return JSONResult.success();
}
}
7.编写service实现类
@Service
public class SearchServiceImpl implements ISearchService {
@Autowired
private CourseElasticsearchRepository courseElasticsearchRepository;
@Override
public void search(CourseDoc doc) {
courseElasticsearchRepository.save(doc);
}
}
8.抽取feiqn接口
@FeignClient(value = "service-search",fallbackFactory = SearchFallbackFactory.class)
public interface SearchClient {
@PostMapping("/es/course/search")
JSONResult search(@RequestBody CourseDoc doc);
}
9.编写降级类
@Component
public class SearchFallbackFactory implements FallbackFactory<SearchClient> {
@Override
public SearchClient create(Throwable throwable) {
return new SearchClient() {
@Override
public JSONResult search(CourseDoc doc) {
throwable.printStackTrace();
return JSONResult.error("调用服务失败");
}
};
}
}
10.调用feiqn接口
调用之前,调用的模块需要开启feiqn支持(@EnableFeignClients),然后配置feiqn的降级
feign:
sentinel:
enabled: true
然后注入feiqn接口即可使用方法