赞
踩
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
indexes->Document->type(类似表)->Field
之前写过这篇博客
TestRepository
public interface ItemRepository extends ElasticsearchRepository<Item,Long> {
List<Item> findByWeightsBetween(double price1, double price2);
List<Item> findByCategoryOrderByWeights(String category);
}
类似jpa的Repository
中文分词
analyzer = "ik_max_word"
FieldType.Text支持全文搜索,所以可以进行分词
FieldType.Keyword支持精确查询,不支持分词,支持聚合等等
参考这一篇 ,比较简单实现
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
版本还要跟客户端es版本进行匹配
Elasticsearch-head https://github.com/mobz/elasticsearch-head
安装看下这一篇
看下es配置elasticsearch.yml
在最下面加了
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
Elasticsearch-head安装文章进行安装即可。
由于想要通过个人化进行搜索文章的需求,后面发现es解决不了,还是得上算法解决。
可以用到标签的智能化应用https://www.elastic.co/guide/en/elasticsearch/guide/current/stopwords-relavance.html
@Test public void a(){ QueryBuilder queryBuilder= QueryBuilders.boolQuery() //.must(QueryBuilders.matchQuery("title","鸡胸").minimumShouldMatch("100%")); .must(QueryBuilders.matchQuery("category","吃货")); //请求精度QueryBuilders.matchQuery("category","吃").minimumShouldMatch() SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(queryBuilder) .build(); // 搜索,获取结果 Page<Item> items = this.itemRepository.search(searchQuery); // 总条数 long total = items.getTotalElements(); System.out.println("总条数 = " + total); // 总页数 System.out.println("总页数 = " + items.getTotalPages()); // 当前页 System.out.println("当前页:" + items.getNumber()); // 每页大小 System.out.println("每页大小:" + items.getSize()); for (Item item : items) { System.out.println(JSON.toJSON(item)); } }
es支持的各类匹配查询 ,上面是match查询,会进行分词查询。
minimumShouldMatch通过这个提高匹配程度,一般提到100%就是完全匹配,其他数值都跟不设置一毛一样。
QueryBuilders.boolQuery().must ->and功能,必须匹配到这一项
QueryBuilders.boolQuery().should ->or功能,可能会匹配到
boost的方法进行修改字段权重
为了实现用户搜索对标签的匹配度,搜索到更接近的文章,这里使用es的相似度算法
@Data @Document(indexName = "article", type = "docs", shards = 1, replicas = 0) public class Article { @Id private Long id; /** * 标题 */ @Field(type = FieldType.Text, analyzer = "ik_max_word") private String title; /** * 标签 */ @Field(type = FieldType.Text, analyzer = "ik_max_word") private String label; /** * 文章id */ @Field(type = FieldType.Long) private Long articleId; }
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
}
@Resource
ElasticsearchTemplate esTemplate;
@Test
public void insert() {
esTemplate.createIndex(Article.class);
}
@Resource ArticleRepository articleRepository; @Test public void insertxx() { List<Article> list = new ArrayList<>(); String[] titles = {"美妆", "护肤", "口红", "眼影", "粉底液", "遮瑕液", "眼霜", "亮白", "清洁", "肿眼泡", "发型", "大小眼","底妆","懂彩妆","爱种草","单眼皮", "礼物","好物分享","上新","内双"}; String[] labels = {"脸基尼、DIY面膜…当代护肤迷惑行为大赏,笑skr人", "完子护肤Q&A:根据肤质选择适合自己的卸妆产品", "超全眼型大解析 | 眼妆一直画不好,原来是因为这个!", "分情况选择适合自己的面膜", "如何选择适合自己的卸妆产品", "学会堪比微整,化妆师最怕被偷师的7个技巧", "变美不用挨针!如何用最高性价比的单品,get水光肌?", "涨姿势 | 斩男必备的眼唇配色小心机,让你妆容美到犯规!", "全民沉迷哪吒仿妆?我却被这些超实用的清透眼妆圈粉了!", "敏感星人最爱的“全能”成分,补水修复舒缓一步到位!" ,"十分钟出门妆 | 直男无法分辨的超自然伪素颜!","如何用最高性价比的单品,get水光肌","气味贩卖 | 哪一种味道,能让ta立刻注意到你" ,"人无完人","拯救黄皮 | 什么妆容适合黄皮?看这篇教科书级示范~","6月减肉难题 | 带妆运动是作死还是精致,终于有了答案","当代忙碌女青年的夏天,请一切从简" ,"美到报警的12色动物盘,全4盘眼妆笔记"}; for (int i = 0; i < 100; i++) { Article article = new Article(); article.setId(IdUtils.getId()); article.setTitle(titles[getRandom(titles.length)]+","+titles[getRandom(titles.length)]); article.setArticleId(IdUtils.getId()); article.setLabel(labels[getRandom(labels.length)]); list.add(article);   **网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。** **[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)** **一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!** 转存中...(img-tQD390H5-1715383083836)] [外链图片转存中...(img-t7gTPpBt-1715383083836)] **网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。** **[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)** **一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。