SpringData-ElasticSearch入门

文章目录

1、创建demo工程


2、application.properties

json 复制代码
spring.application.name=es-demo
spring.elasticsearch.uris=http://192.168.74.148:9200
spring.elasticsearch.username=elastic
spring.elasticsearch.password=123456

3、Goods 实体类

java 复制代码
package com.atguigu.es.demo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "goods")
public class Goods {

    @Id
    private Long id;

    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title;

    @Field(type = FieldType.Keyword,index = false)
    private String image;

    @Field(type = FieldType.Integer)
    private Integer price;

    @Field(type = FieldType.Keyword)
    private String brand;

    @Field(type = FieldType.Keyword)
    private String category;
}

4、EsDemoApplicationTests 测试类

java 复制代码
package com.atguigu.es.demo;

import com.atguigu.es.demo.pojo.Goods;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;

@SpringBootTest
class EsDemoApplicationTests {

	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

	@Test
	void contextLoads() {
	    // 获取Goods类对应的索引操作对象
	    IndexOperations indexOps = this.elasticsearchTemplate.indexOps(Goods.class);
	    // 创建索引
	    indexOps.create();
	    // 创建映射
	    indexOps.putMapping();
	}

}

5、pom.xml

xml 复制代码
<?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 https://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>3.0.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

  <!-- Generated by https://start.springboot.io -->
  <!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
	<groupId>com.atguigu</groupId>
	<artifactId>es-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>es-demo</name>
	<description>es-demo</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<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>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

6、查看索引库

7、查看单个索引(数据库)

json 复制代码
GET /goods
json 复制代码
{
  "goods": {
    "aliases": {},
    "mappings": {
      "properties": {
        "_class": {
          "type": "keyword",
          "index": false,
          "doc_values": false
        },
        "brand": {
          "type": "keyword"
        },
        "category": {
          "type": "keyword"
        },
        "image": {
          "type": "keyword",
          "index": false
        },
        "price": {
          "type": "integer"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "refresh_interval": "1s",
        "number_of_shards": "1",
        "provided_name": "goods",
        "creation_date": "1724723254472",
        "number_of_replicas": "1",
        "uuid": "vBsgfYlfRjKs6uSISXbxtw",
        "version": {
          "created": "8080299"
        }
      }
    }
  }
}

8、从goods索引中检索出符合特定搜索条件的文档(或记录)

json 复制代码
GET /goods/_search
json 复制代码
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}
相关推荐
无代码专家5 分钟前
设备巡检数字化闭环解决方案:从预防到优化的全流程赋能
大数据·人工智能
神算大模型APi--天枢6461 小时前
合规与高效兼得:国产全栈架构赋能行业大模型定制,从教育到工业的轻量化落地
大数据·前端·人工智能·架构·硬件架构
云和数据.ChenGuang2 小时前
Logstash配置文件的**语法解析错误**
运维·数据库·分布式·rabbitmq·jenkins
飞飞传输3 小时前
守护医疗隐私,数据安全摆渡系统撑起内外网安全伞!
大数据·运维·安全
Guheyunyi3 小时前
视频安全监测系统的三大核心突破
大数据·运维·服务器·人工智能·安全·音视频
Elasticsearch3 小时前
使用 OpenLit、 OpenTelemetry 和 Elastic 的 AI Agent 可观测性
elasticsearch
阿里云大数据AI技术4 小时前
1TB数据,ES却收到了2TB?揪出那个客户端中的“隐形复读机”
大数据·elasticsearch
初恋叫萱萱4 小时前
【TextIn大模型加速器 + 火山引擎】文件智能体构建全路径指南
大数据·数据库·火山引擎
安达发公司4 小时前
安达发|效率革命:APS自动排程,为“金属丛林”安装精准导航
大数据·运维·人工智能·aps高级排程·aps排程软件·安达发aps·aps自动排程
科士威传动5 小时前
精密仪器中的微型导轨如何选对润滑脂?
大数据·运维·人工智能·科技·机器人·自动化