Huawei Cloud Yaoyun Server L インスタンスの評価 | 完全なクエリとファジー クエリのための Elasticsearch と Kibana の Springboot 統合

ここに画像の説明を挿入します

序文

最近、Huawei Cloud Yaoyun Server L インスタンスがリリースされ、私も構築して遊んでみましたが、その過程でさまざまな問題に遭遇し、問題を解決する過程での運用保守について多くのことを学びました。

前回のブログでは、Elasticsearch の Docker バージョンのインストール、Elasticsearch のビジュアル Kibana ツールのインストール、および IK ワード セグメンターのインストールを紹介しました。

このブログでは、Elasticsearch の springboot 統合と Kibana の完全なクエリとファジー クエリを紹介します。

その他の Elasticsearch 関連記事のリストは次のとおりです。

ここに画像の説明を挿入します

その他の関連する Huawei Cloud Yaoyun Server L インスタンスの評価記事のリストは次のとおりです。

導き出す


1. Elasticsearch の Springboot 統合、
2. 完全なクエリとファジー クエリのための Kibana。

springBoot は elasticsearch を統合します

ここに画像の説明を挿入します

1.依存関係を導入する

        <!--        elasticsearch的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

2.ymlファイルの設定

ここに画像の説明を挿入します

server:
  port: 9090

spring:
  application:
    # 给这个项目起个名称
    name: book-mall
  datasource:
    druid:
      url: jdbc:mysql://127.0.0.1:3306/book_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      username: root
      password: 123
      driver-class-name: com.mysql.cj.jdbc.Driver

  # es的相关配置
  data:
    elasticsearch:
      repositories:
        enabled: true
  # es的ip+端口
  elasticsearch:
    uris: 124.70.138.34:9200


mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# 单元测试配置
knife4j:
  enable: true

# 日志级别配置
logging:
  level:
    com.tinaju.bm: debug

構成クラスの書き込み

ここに画像の説明を挿入します

package com.tinaju.bm.config;


import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * Elasticsearch的配置类
 */
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    
    
    @Value("${spring.elasticsearch.uris}")
    private String uris;
    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {
    
    
        ClientConfiguration configuration =
                ClientConfiguration.builder()
                        .connectedTo(uris)
                        .build();
        return RestClients.create(configuration).rest();
    }
}

3. docのエンティティクラスを作成する

ここに画像の説明を挿入します

package com.tinaju.bm.entity.es;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 和es相关的映射类doc文档
 */
@Data
@NoArgsConstructor
@AllArgsConstructor

@Document(indexName = "book_index",createIndex = true)
public class BookDoc {
    
    

    @Id
    @Field(type = FieldType.Text)
    private String id;
    
    @Field(type = FieldType.Text)
    private String title;
    
    @Field(type = FieldType.Text)
    private String author;
    
    @Field(type = FieldType.Text)
    private String isbn;
    
    @Field(type = FieldType.Double) // 是double类型
    private BigDecimal price;
    
    @Field(type = FieldType.Integer)
    private Integer version;
    
    @Field(type = FieldType.Date,format = DateFormat.basic_date_time)
    private Date pubDate; // 出版日期
    
    @Field(type = FieldType.Integer)
    private Integer store; // 库存
    
    @Field(type = FieldType.Text)
    private String imgUrl; // 封面图片地址
    
    @Field(type = FieldType.Double) // 是double类型
    private BigDecimal weight; // 重量
    
    @Field(type = FieldType.Integer)
    private Integer sold; // 卖出数量;
    
    @Field(type = FieldType.Text)
    private String introduction; // 简介
    
    @Field(type = FieldType.Integer)
    private Integer pages; // 图书页数
    
    @Field(type = FieldType.Date,format = DateFormat.basic_date_time)
    private Date createTime;
    
    @Field(type = FieldType.Date,format = DateFormat.basic_date_time)
    private Date updateTime;
    
    @Field(type = FieldType.Text)
    private String createBy; // 数据记录人
    
    @Field(type = FieldType.Text)
    private Long publisherName; // 出版社
    
    @Field(type = FieldType.Text)
    private Long typeName; // 类型

}

4. エンティティクラスに対応するマッパーを記述する

ここに画像の説明を挿入します

package com.tinaju.bm.dao.es;

import com.tinaju.bm.entity.es.BookDoc;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * es的dao,实体类类型,和主键的类型
 */
@Mapper
public interface IBookDocDao extends ElasticsearchRepository<BookDoc,String> {
    
    
}

5. データへのアクセスのテスト

ここに画像の説明を挿入します

    @Autowired
    private IBookDocDao bookDocDao;

    @Override
    public void addToES() {
    
    
        List<Book> books = bookMapper.selectList(null);
        books.forEach(book -> {
    
    
            BookDoc bookDoc = new BookDoc();
            bookDoc.setId(book.getId()+"");
            bookDoc.setAuthor(book.getAuthor());
            bookDoc.setIntroduction(book.getIntroduction());
            bookDoc.setTitle(book.getTitle());
            bookDoc.setPrice(book.getPrice());
            bookDocDao.save(bookDoc);
        });
    }

ここに画像の説明を挿入します

エスクエリを実行する

ここに画像の説明を挿入します

    @Autowired
    private IBookDocDao bookDocDao;

    @Test
    public void findES() {
    
    
        Iterable<BookDoc> all = bookDocDao.findAll();
        System.out.println(all);
        all.forEach(bookDoc -> {
    
    
            System.out.println(bookDoc);
        });

        System.out.println("###############");
        Optional<BookDoc> byId = bookDocDao.findById("4");
        System.out.println(byId.get());
    }

キバナクエリ

1.完全なクエリ

GET /book_index/_search

フルクエリは Navicat よりも時間がかかりますが、データ量が少なく、es がサーバー上にあるため、ネットワークも遅延している可能性があります。

私の mysql はローカル データベースです。

ここに画像の説明を挿入します

Navicat がクエリを実行する

ここに画像の説明を挿入します

2.あいまいクエリを実行する

MySQLはファジークエリを実行します

ここに画像の説明を挿入します

ファジークエリの場合は es

ここに画像の説明を挿入します

GET /book_index/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "should": [
        {
    
    
          "fuzzy": {
    
    
            "title": {
    
    
              "value": "中"
            }
          }
        },
        {
    
    
          "wildcard": {
    
    
            "author": {
    
    
              "value": "中"
            }
          }
        },
        {
    
    
          "wildcard": {
    
    
            "introduction": {
    
    
              "value": "中"
            }
          }
        }
      ]
    }
  }
}

付録: Kibana エラー解決

kibana Web ページを開いた後にエラーが報告されました

ここに画像の説明を挿入します

問題を解決するには、構成ファイルにserver.nameを設定します。

ここに画像の説明を挿入します


要約する

1. Elasticsearch の Springboot 統合、
2. 完全なクエリとファジー クエリのための Kibana。

おすすめ

転載: blog.csdn.net/Pireley/article/details/133513298