ElasticSearch入门
认识ElasticSearch
基于数据库查询问题
需求: 查询title中包含‘手机’ 的信息?
实现:SELECT * FROM tb_item WHERE title LIKE '%手机%';
问题:
(1)性能低:如果是模糊查询,坐标有通配符,不会走索引,会进行全表扫描,性能低。
(2)功能弱:如果以”华为手机“作为条件,若没有”华为手机“关键字,查询不出来数据。
解决:使用ES就可以倒排索引就可以解决上述存在的问题
倒排索引
倒排索引概念
将文档进行分词,形成词条和id对应关系即为反向索引
倒排序索引
以唐诗为例,所处包含“前”的诗句?
正向索引:由《静夜思》-->床前明月光--->“前”字
| key | value |
|---|---|
| <<静夜思>> | 床前明月光,疑是地上霜... |
| <<水调歌头>> | 明月几时有,把酒问青天... |
| <<春晓>> | 春眠不觉晓,处处闻啼鸟... |
正向索引 :床------>床前明月光----<静夜思>
反向索引:
床前明月光:
床 ---》 静夜思
前 ---》 静夜思
明月 ---》 静夜思
光 ---》 静夜思
月光 ---》 静夜思
ElasticSearch存储和查询的原理
index(索引):相当于mysql的库
映射:相当于mysql 的表结构
document(文档):相当于mysql的表中的数据
Es使用倒排索引,对title 进行分词
ElasticSearch概念详解(数据大)
ElasticSearch是一个基于Lucene的搜索服务器
是一个分布式、高扩展、高实时的搜索与数据分析引擎
基于RESTful web接口
Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎
应用场景
搜索:海量数据的查询
日志数据分析
实时数据分析
介绍Kibana
Kibana
Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索、查看交互存储在Elasticsearch索引中的数据。使用Kibana,可以通过各种图表进行高级数据分析及展示。
Kibana让海量数据更容易理解。它操作简单,基于浏览器的用户界面可以快速创建仪表板(dashboard)实时显示Elasticsearch查询动态。
ElasticSearch核心概念
索引(index)
ElasticSearch存储数据的地方,可以理解成关系型数据库中的数据库概念。
映射(mapping)
mapping定义了每个字段的类型、字段所使用的分词器等。相当于关系型数据库中的表结构。
文档(document)
Elasticsearch中的最小数据单元,常以json格式显示。一个document相当于关系型数据库中的一行数据。
倒排索引
一个倒排索引由文档中所有不重复词的列表构成,对于其中每个词,对应一个包含它的文档id列表。
类型(type)
一种type就像一类表。如用户表、角色表等。在Elasticsearch7.X默认type为_doc
ES 5.x中一个index可以有多种type。
ES 6.x中一个index只能有一种type。
ES 7.x以后,将逐步移除type这个概念,现在的操作已经不再使用,默认_doc
脚本操作ElasticSearch
RESTful风格介绍
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,可以降低开发的复杂性,提高系统的可伸缩性。
特点:
1.基于http协议
2.使用XML格式定义或JSON格式定义
3.每一个URI代表1种资源。
4.客户端使用GET、POST、PUT、DELETE 4个表示操作方式的动词对服务端资源进行操作:
GET:用来获取资源
POST:用来新建资源(也可以用于更新资源)
PUT:用来更新资源
DELETE:用来删除资源
操作索引
创建索引(PUT)
http://ip:端口/索引名称
案例:
http://192.168.126.20:9200/index_01
查询索引(GET)
GET http://ip:端口/索引名称 # 查询单个索引信息
GET http://ip:端口/索引名称1,索引名称2... # 查询多个索引信息
GET http://ip:端口/_all # 查询所有索引信息
删除索引(DELETE)
DELETE http://ip:端口/索引名称
ElasticSearch 数据类型
简单数据类型
| 数据类型 | 备注 |
|---|---|
| 字符串 | text :会分词,不支持聚合 相当于mysql 中的sum(求和) |
| keyword:不会分词,将全部内容作为一个词条,支持聚合 | |
| 数值 | byte, short, integer, long, float, double |
| 布尔 | boolean |
| 范围类型 | integer_range, float_range, long_range, double_range, date_range |
| 日期 | date |
复杂数据类型
| 数据类型 | 备注 |
|---|---|
| 数组: [ {id:333,name:user},{id:444,name:vip}] | nested (for arrays of JSON objects 数组类型的JSON对象) |
| 对象: | object(for single JSON objects 单个JSON对象) |
操作映射
查询映射
GET /索引的名称/_mapping
创建映射
- 创建索引后添加映射
#创建一个person 索引
PUT person
#查询person 索引的mapping 是空
GET /person/_mapping
#给person 添加索引
PUT /person/_mapping
{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"integer"
}
}
}
- 创建索引并添加映射
#创建索引并且添加映射
PUT person2
{
"mappings": {
"properties": {
"name":{
"type":"text"
},
"age":{
"type": "integer"
}
}
}
}
#查询映射
GET person2/_mapping
- 添加字段
#给创建好的索引添加字段
PUT /person2/_mapping
{
"properties":{
"address":{
"type":"text"
}
}
}
总结:在实际当中 ,我们很少操作映射,一般都是使用API方式操作文档,所以以上的操作只需要能看懂就可以。
操作文档
查询文档
#根据id查询具体的文档
语法: GET /索引的名称/_doc/索引的id
案例: GET /person1/_doc/1
select * from person where id =1
#查询所有的文档
语法:GET /索引名称/_search
案例:GET /person1/_search
添加文档,指定id
#语法:POST /索引的名称/文档的类型/文档的id
POST /person2/_doc/1
{
"name":"张三",
"age":18,
"address":"北京"
}
#查询文档
GET /person2/_doc/1
添加文档,不指定id
#添加文档,不指定id
POST /person/_doc/
{
"name":"张三",
"age":18,
"address":"北京"
}
#查询所有文档
GET /person2/_search
删除文档
#删除指定id文档
DELETE /person2/_doc/1
IK分词器介绍
IK分词器的介绍
IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包
是一个基于Maven构建的项目
具有60万字/秒的高速处理能力
支持用户词典扩展定义
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/archive/v7.4.0.zip
安装包在资料文件夹中提供
分词器的介绍
es:index(库) mapping(表结构) docment(存放的数据)
put offcn_02
{
"mappings":{
"properties":{
"bir":{
"type": "date"
},
"titile":{
"type": "text",
"analyzer":'standard'
},
"age":{
"type":"integer"
}
}
}
}
post /offcn_02/_doc/1
{
"age":234,
"bir": "2021-10-21",
"titile": "超级酷炫黑珍珠小米小手机"
}
post /offcn_02/_doc/2
{
"age":234,
"bir": "2021-10-21",
"titile": "苹果 移动4g 银色大手机"
}
post /offcn_02/_doc/3
{
"age":234,
"bir": "2021-10-21",
"titile": "华为 移动4g 双卡超大屏移动电话"
}
ES内置分词器的说明
分词器(Analyzer):将一段文本,按照一定逻辑,分析成多个词语的一种工具
比如: 华为手机 ----->华为,手,手机
ElasticSearch内置的分词器:
Standard Analyzer - 默认分词器,按词切分,小写处理
Simple Analyzer - 按照非字母切分(符号被过滤), 小写处理
Stop Analyzer - 小写处理,停用词过滤(the,a,is)
Whitespace Analyzer - 按照空格切分,不转小写
Keyword Analyzer - 不分词,直接将输入当作输出
Patter Analyzer - 正则表达式,默认\W+(非字符分割)
Language - 提供了30多种常见语言的分词器
Customer Analyzer 自定义分词器
注意: ElasticSearch对中文分词并不友好,处理方式:一个字一个词
案例:
get _analyze
{
"analyzer":"standard",
"text":"我爱北京天安门"
}
使用IK分词器
IK分词器使用
IK分词器有两种分词模式:ik_max_word和ik_smart模式。
ik_max_word :会将文本做最细粒度的拆分,比如会将“乒乓球明年总冠军”拆分为“乒乓球、乒乓、球、明年、总冠军、冠军。
ik_smart: 会做最粗粒度的拆分,比如会将“乒乓球明年总冠军”拆分为乒乓球、明年、总冠军。
- ik_max_word
会将文本做最细粒度的拆分,比如会将“乒乓球明年总冠军”拆分为“乒乓球、乒乓、球、明年、总冠军、冠军。
#方式一 ik_max_word
GET /_analyze
{
"analyzer": "ik_max_word",
"text": "乒乓球明年总冠军"
}
ik_max_word分词器执行如下:
{
"tokens" : [
{
"token" : "乒乓球",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "乒乓",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "球",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "明年",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "总冠军",
"start_offset" : 5,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "冠军",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 5
}
]
}
- ik_smart
会做最粗粒度的拆分,比如会将“乒乓球明年总冠军”拆分为乒乓球、明年、总冠军。
#方式二ik_smart
GET /_analyze
{
"analyzer": "ik_smart",
"text": "乒乓球明年总冠军"
}
ik_smart分词器执行如下:
{
"tokens" : [
{
"token" : "乒乓球",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "明年",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "总冠军",
"start_offset" : 5,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 2
}
]
}
由此可见 使用ik_smart可以将文本"text": "乒乓球明年总冠军"分成了【乒乓球】【明年】【总冠军】
- 使用IK分词器查询文档
词条查询:term
词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时才匹配搜索
北京天安门---->
全文查询:match
全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集
北京天安门---->【北京 天安门】
1.创建索引,添加映射,并指定分词器为ik分词器
PUT person2
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"address": {
"type": "text",
"analyzer": "ik_max_word"
},
"age":{
"type":"integer"
}
}
}
}
2.添加文档
POST /person2/_doc/1
{
"name":"张三",
"age":18,
"address":"北京海淀区"
}
POST /person2/_doc/2
{
"name":"李四",
"age":18,
"address":"北京朝阳区"
}
POST /person2/_doc/3
{
"name":"王五",
"age":18,
"address":"北京昌平区"
}
POST /person2/_doc/4
{
"name":"赵六",
"age":18,
"address":"昌平区慧聪园"
}
3.查询映射
GET person2
4.查看分词效果
GET _analyze
{
"analyzer": "ik_max_word",
"text": "北京海淀"
}
5.词条查询:term
查询person2中匹配到"北京"两字的词条
GET /person2/_search
{
"query": {
"term": {
"address": {
"value": "北京"
}
}
}
}
6.全文查询:match
全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集
GET /person2/_search
{
"query": {
"match": {
"address":"北京昌平"
}
}
}
ElasticSearch JavaAPI
SpringBoot整合ElasticSearch环境搭建
搭建SpringBoot工程
引入ElasticSearch相关坐标
<!--引入es的坐标-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
编写核心配置类
编写核心配置文件:
elasticsearch:
hostname: 192.168.126.20
port: 9200
编写核心配置类
@Configuration
@ConfigurationProperties(prefix="elasticsearch")
public class ElasticSearchConfig {
private String host;
private int port;
@Bean
public RestHighLevelClient client(){
return new RestHighLevelClient(RestClient.builder(
new HttpHost(host,port,"http")
));
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
测试客户端对象
注意:使用@Autowired注入RestHighLevelClient 如果报红线,则是因为配置类所在的包和测试类所在的包,包名不一致造成的
@SpringBootTest
class SpringElasticsearchApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
void contextLoads() {
System.out.println(restHighLevelClient);
}
}
操作ElasticSearch
添加索引
/**
* 添加索引:
* @throws Exception
*/
@Test
public void addIndex() throws Exception{
//1:使用restHighLevelClient获取操作索引的对象
IndicesClient indices = restHighLevelClient.indices();
//2: 具体操作索引,并且获得返回值
//2.1 创建索引对象,设置索引的名称
CreateIndexRequest createIndexRequest = new CreateIndexRequest("offcn");
//2.2 创建索引,获得返回值
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
//3:根据返回值判断返回结果
System.out.println(createIndexResponse.isAcknowledged());
}
添加索引,并添加映射
/**
* 创建索引并且添加映射
* @throws IOException
*/
@Test
public void addIndexAndMapping() throws IOException {
//1.使用client获取操作索引的对象
IndicesClient indicesClient = restHighLevelClient.indices();
//2.具体操作,获取返回值
CreateIndexRequest createRequest = new CreateIndexRequest("offcn");
//2.1 设置mappings
String mapping = "{\n" +
" \"properties\" : {\n" +
" \"address\" : {\n" +
" \"type\" : \"text\",\n" +
" \"analyzer\" : \"ik_max_word\"\n" +
" },\n" +
" \"age\" : {\n" +
" \"type\" : \"long\"\n" +
" },\n" +
" \"name\" : {\n" +
" \"type\" : \"keyword\"\n" +
" }\n" +
" }\n" +
" }";
createRequest.mapping(mapping, XContentType.JSON);
//2.2执行创建,返回对象
CreateIndexResponse response = indicesClient.create(createRequest, RequestOptions.DEFAULT);
//3.根据返回值判断结果
System.out.println(response.isAcknowledged());
}
查询、删除、判断索引
- 查询索引
/**
* 查询索引
*/
@Test
public void queryIndex() throws IOException {
//1:使用client获取操作索引的对象
IndicesClient indices = restHighLevelClient.indices();
//2:获得对象,执行具体的操作
//2.1 创建获取索引的请求对象,设置索引名称
GetIndexRequest getReqeust = new GetIndexRequest("offcn");
//2.2 执行查询,获得返回值
GetIndexResponse response = indices.get(getReqeust, RequestOptions.DEFAULT);
//3:获取结果,遍历
Map<String, MappingMetaData> mappings = response.getMappings();
for (String key : mappings.keySet()) {
System.out.println(key + ":" + mappings.get(key).getSourceAsMap());
}
}
- 删除索引
/**
* 删除索引
*/
@Test
public void deleteIndex() throws IOException {
IndicesClient indices = restHighLevelClient.indices();
DeleteIndexRequest deleteRequest = new DeleteIndexRequest("offcn");
AcknowledgedResponse response = indices.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
- 索引是否存在
/**
* 判断索引是否存在
*/
@Test
public void existIndex() throws IOException {
IndicesClient indices = restHighLevelClient.indices();
GetIndexRequest getRequest = new GetIndexRequest("person");
boolean exists = indices.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
操作文档
- 添加文档
添加文档,使用map作为数据
/**
* 添加文档,使用map作为数据
*/
@Test
public void addDoc() throws IOException {
//数据对象,map
Map data = new HashMap();
data.put("address", "北京昌平");
data.put("name", "马同志");
data.put("age", 20);
//1:获取操作文档的对象
IndexRequest request = new IndexRequest("offcn").id("1").source(data);
//2:添加数据,获取结果
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
//3:打印响应结果
System.out.println(response.getId());
}
添加文档,使用对象作为数据
/**
* 添加文档,使用对象作为数据
*/
@Test
public void addDoc2() throws IOException {
//数据对象,javaObject
Person p = new Person();
p.setId("2");
p.setName("小胖2222");
p.setAge(30);
p.setAddress("陕西西安");
//将对象转为json
String data = JSON.toJSONString(p);
//1:获取操作文档的对象
IndexRequest request = new IndexRequest("offcn").id(p.getId()).source(data, XContentType.JSON);
//2:添加数据,获取结果
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
//3:打印响应结果
System.out.println(response.getId());
}
- 修改文档:添加文档时,如果id存在则修改,id不存在则添加
/**
* 修改文档:添加文档时,如果id存在则修改,id不存在则添加
*/
@Test
public void updateDoc() throws IOException {
//数据对象,map
Map data = new HashMap();
data.put("address", "北京昌平");
data.put("name", "朱同志");
data.put("age", 20);
//1:获取操作文档的对象
IndexRequest request = new IndexRequest("offcn").id("1").source(data);
//2:添加数据,获取结果
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
//3:打印响应结果
System.out.println(response.getId());
}
- 根据id查询文档
/**
* 根据id查询文档
*/
@Test
public void findDocById() throws IOException {
GetRequest getReqeust = new GetRequest("offcn", "1");
//getReqeust.id("1");
GetResponse response = restHighLevelClient.get(getReqeust, RequestOptions.DEFAULT);
//获取数据对应的json
System.out.println(response.getSourceAsString());
}
- 根据id删除文档
/**
* 根据id删除文档
*/
@Test
public void delDoc() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("offcn", "1");
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(response.getId());
}
ElasticSearch的批量操作
bulk 批量操作脚本实现
需求:同时完成删除,更新,添加操作
jdbc批处理:
update delete insert
statement.execute(update);
statement.execute(delete);
statement.execute(insert);
Statement 对象将sql语句发送到数据库
addBatch(update);
addBatch(delete);
addBatch(insert);
在statement命令的列表中存放了3条sql语句
excuteBatch(); 一次将3条sql发送到数据库,数据库只需要调用一次底层就可以执行三条sql语句
POST _bulk
{"delete":{"_index":"stu","_id":"4"}}
{"create":{"_index":"stu","_id":"9"}}
{"name":"九号","age":18,"address":"北京"}
{"update":{"_index":"stu","_id":"2"}}
{"doc":{"name":"2号"}}
查询运行结果~ ;
bulk批量操作JavaAPI 实现
/**
* Bulk 批量操作
*/
@Test
public void test2() throws IOException {
//创建bulkrequest对象,整合所有操作
BulkRequest bulkRequest =new BulkRequest();
/*
# 1. 删除5号记录
# 2. 添加6号记录
# 3. 修改3号记录 名称为 “三号”
*/
//添加对应操作
//1. 删除5号记录
DeleteRequest deleteRequest=new DeleteRequest("person1","5");
bulkRequest.add(deleteRequest);
//2. 添加6号记录
Map<String, Object> map=new HashMap<>();
map.put("name","六号");
IndexRequest indexRequest=new IndexRequest("person1").id("6").source(map);
bulkRequest.add(indexRequest);
//3. 修改3号记录 名称为 “三号”
Map<String, Object> mapUpdate=new HashMap<>();
mapUpdate.put("name","三号");
UpdateRequest updateRequest=new UpdateRequest("person1","3").doc(mapUpdate);
bulkRequest.add(updateRequest);
//执行批量操作
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(response.status());
}
导入数据分析&创建索引
需求: 将数据库中Good表的数据导入到ElasticSearch当中
实现步骤:
分析goods表结构
创建goods索引
查询Goods表数据
批量添加到ElasticSearch中
PUT goods
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_smart"
},
"price": {
"type": "double"
},
"createTime": {
"type": "date"
},
"categoryName": {
"type": "keyword"
},
"brandName": {
"type": "keyword"
},
"spec": {
"type": "object"
},
"saleNum": {
"type": "integer"
},
"stock": {
"type": "integer"
}
}
}
}
title:商品标题
price:商品价格
createTime:创建时间
categoryName:分类名称。如:家电,手机
brandName:品牌名称。如:华为,小米
spec: 商品规格。如: spec:{"屏幕尺寸","5寸","内存大小","128G"}
saleNum:销量
stock:库存量
插入一条测试数据
POST goods/_doc/1
{
"title":"小米手机",
"price":1000,
"createTime":"2021-12-01",
"categoryName":"手机",
"brandName":"小米",
"saleNum":3000,
"stock":10000,
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"4.5"
}
}
导入数据代码实现
/**
* 从Mysql 批量导入 elasticSearch
*/
@Test
public void test3() throws IOException {
//1.查询所有数据,mysql
List<Goods> goodsList = goodsMapper.findAll();
//2.bulk导入
BulkRequest bulkRequest=new BulkRequest();
//2.1 循环goodsList,创建IndexRequest添加数据
for (Goods goods : goodsList) {
//2.2 设置spec规格信息 Map的数据 specStr:{key:value,key:value}
String specStr = goods.getSpecStr();
//将json格式字符串转为Map集合
Map map = JSON.parseObject(specStr, Map.class);
//设置spec map
goods.setSpec(map);
//将goods对象转换为json字符串
String data = JSON.toJSONString(goods);
IndexRequest indexRequest=new IndexRequest("goods").source(data,XContentType.JSON);
bulkRequest.add(indexRequest);
}
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(response.status());
}
termQuery(matchQuery) 词条查询
term查询和字段类型有关系,首先回顾一下ElasticSearch两个数据类型
ElasticSearch两个数据类型:
text:会分词,不支持聚合
keyword:不会分词,将全部内容作为一个词条,支持聚合
脚本实现:
#term查询: 查询title当中包含 华为
GET /goods/_search
{
"query": {
"term": {
"title": {
"value": "华为"
}
}
}
}
javaAPI代码实现:
/**
* termQuery:词条查询
*/
@Test
public void testTermQuery() throws IOException {
//那个索引库
SearchRequest searchRequest = new SearchRequest("goods");
//指定我们当前索引库中的指定映射的字段进行检索,检索关键字
SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
QueryBuilder query = QueryBuilders.termQuery("title","华为手机");//term词条查询
//QueryBuilder queryBuilder = QueryBuilders.matchQuery("title","华为手机");
sourceBulider.query(query);
searchRequest.source(sourceBulider);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Goods> goodsList = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为java
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
goodsList.add(goods);
}
for (Goods goods : goodsList) {
System.out.println(goods);
}
}

浙公网安备 33010602011771号