当前位置:   article > 正文

最全ES 总结(二)以及实践搜索优化_eslabelrepository(1),2024年最新46道面试题带你了解中高级软件测试面试_elasticsearchrepository注解式开发聚合

elasticsearchrepository注解式开发聚合

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

ES组成

indexes->Document->type(类似表)->Field

springboot整合ES

之前写过这篇博客

detail

TestRepository

public interface ItemRepository extends ElasticsearchRepository<Item,Long> {

    List<Item> findByWeightsBetween(double price1, double price2);

    List<Item> findByCategoryOrderByWeights(String category);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

类似jpa的Repository

@Field

中文分词

analyzer = "ik_max_word"
  • 1
FieldType.Text支持全文搜索,所以可以进行分词
  • 1
FieldType.Keyword支持精确查询,不支持分词,支持聚合等等
  • 1

调用方法

参考这一篇 ,比较简单实现

版本

<?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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

版本还要跟客户端es版本进行匹配

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
  • 1
  • 2
  • 3
  • 4

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));
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

es支持的各类匹配查询 ,上面是match查询,会进行分词查询。

minimumShouldMatch通过这个提高匹配程度,一般提到100%就是完全匹配,其他数值都跟不设置一毛一样。

QueryBuilders.boolQuery().must ->and功能,必须匹配到这一项
  • 1
QueryBuilders.boolQuery().should ->or功能,可能会匹配到
  • 1

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;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
文章dao类
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
}
  • 1
  • 2
新建索引
    @Resource
    ElasticsearchTemplate esTemplate;

    @Test
    public void insert() {
        esTemplate.createIndex(Article.class);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
插入假数据
@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);


![img](https://img-blog.csdnimg.cn/img_convert/11a4811c92939bc3092e0e16269b28a0.png)
![img](https://img-blog.csdnimg.cn/img_convert/961c8400731757f55e861df74b40b1ec.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

转存中...(img-tQD390H5-1715383083836)]
[外链图片转存中...(img-t7gTPpBt-1715383083836)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号