spring-data-elasticsearch简单案例

关于spring-data-es注解以及案例还可以参照https://blog.youkuaiyun.com/qq_43652509/article/details/83989257

、spring-data-elasticsearch 的工程介绍
spring-data-elasticsearch 的工程,介绍 Spring Data Elasticsearch 简单的 ES 操作。Spring Data Elasticsearch 可以跟 JPA 进行类比。其使用方法也很简单

、版本对应关系,注意我这里用的是ElasticSearch 2.4.3。
Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
x <= 1.3.5 y <= 1.3.4 z <= 1.7.2*
x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0**

  • 只需要你修改下对应的 pom 文件版本号
    *- 下一个 ES 的版本会有重大的更新

**三、关于Elasticsearch 代码位置:(里面有高版本和低版本的两个案例和es6.3.0的windows带ik和head插件的包)
https://download.youkuaiyun.com/download/xiaolong2230/11216463
项目结构介绍

org.spring.springboot.controller - Controller 层
org.spring.springboot.repository - ES 数据操作层
org.spring.springboot.domain - 实体类
org.spring.springboot.service - ES 业务逻辑层
Application - 应用启动类
application.properties - 应用配置文件,应用启动会自动读取配置
另外可以看出,接口的命名是遵循规范的。常用命名规则如下:

关键字 方法命名

And          findByNameAndPwd
Or             findByNameOrSex
Is              findById
Between   findByIdBetween
Like           findByNameLike
NotLike     findByNameNotLike
OrderBy    findByIdOrderByXDesc
Not           findByNameNot

application.properties 配置 ES 地址

spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。

更多配置:

 spring.data.elasticsearch.cluster-name Elasticsearch    集群名。(默认值: elasticsearch)
 spring.data.elasticsearch.cluster-nodes    集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点。
 spring.data.elasticsearch.propertie     用来配置客户端的额外属性。
 spring.data.elasticsearch.repositories.enabled     开启 Elasticsearch 仓库。(默认值:true。)

1.编译工程
在项目根目录 spring-data-elasticsearch-crud,运行 maven 指令:

mvn clean install

2.运行工程
右键运行 Application 应用启动类(位置:/springboot-learning-example/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java)的 main 函数,这样就成功启动了 springboot-elasticsearch 案例。
用 Postman 工具新增两个城市

a. 新增城市信息

POST http://127.0.0.1:8080/api/city
{
    "id”:"1",
    "score":"5",
    "name":"上海",
    "description":"上海是个热城市"
}
 

POST http://127.0.0.1:8080/api/city
{
    "id":"2",
    "score”:"4",
    "name”:”温岭",
    "description":”温岭是个沿海城市"
}

可以打开 ES 可视化工具 head 插件:http://localhost:9200/_plugin/head/:
(如果不知道怎么安装,请查阅 《Elasticsearch 和插件 elasticsearch-head 安装详解》 https://blog.youkuaiyun.com/xiaolong2230/article/details/90610995
在「数据浏览」tab,可以查阅到 ES 中数据是否被插入,插入后的数据格式如下:

{
"_index": "cityindex",
"_type": "city",
"_id": "1",
"_version": 1,
"_score": 1,
"_source": {
  "id":"2",
    "score”:"4",
    "name”:”温岭",
    "description":”温岭是个沿海城市"
}
}

下面是基本查询语句的接口:
a. 普通查询,查询城市描述

GET http://localhost:8080/api/city ... on%3D温岭
返回 JSON 如下:

[
    {
        "id": 2,
        "name": "温岭",
        "description": "温岭是个沿海城市",
        "score": 4
    }
]

b. AND 语句查询

GET http://localhost:8080/api/city ... on%3D温岭&score=4
返回 JSON 如下:

[
    {
        "id": 2,
        "name": "温岭",
        "description": "温岭是个沿海城市",
        "score": 4
    }
]
如果换成 score=5 ,就没有结果了。

c. OR 语句查询

GET http://localhost:8080/api/city ... on%3D上海&score=4
返回 JSON 如下:

[
    {
        "id": 2,
        "name": "温岭",
        "description": "温岭是个沿海城市",
        "score": 4
    },
    {
        "id": 1,
        "name": "上海",
        "description": "上海是个好城市",
        "score": 3
    }
]

d. NOT 语句查询

GET http://localhost:8080/api/city ... on%3D温州
返回 JSON 如下:

[
    {
        "id": 2,
        "name": "温岭",
        "description": "温岭是个沿海城市",
        "score": 4
    },
    {
        "id": 1,
        "name": "上海",
        "description": "上海是个好城市",
        "score": 3
    }
]

e. LIKE 语句查询

GET http://localhost:8080/api/city ... on%3D城市
返回 JSON 如下:

[
    {
        "id": 2,
        "name": "温岭",
        "description": "温岭是个沿海城市",
        "score": 4
    },
    {
        "id": 1,
        "name": "上海",
        "description": "上海是个好城市",
        "score": 3
    }
]

四、如果在运行过程中帮出现elasticsearch链接报错,错误如下:

2017-09-25 11:41:28.855  INFO 8756 --- [][generic][T#1]] org.elasticsearch.client.transport       : [Ghaur] failed to get node info for {#transport#-1}{192.168.3.50}{192.168.3.50:9200}, disconnecting...

org.elasticsearch.transport.ReceiveTimeoutTransportException: [][192.168.3.50:9200][cluster:monitor/nodes/liveness] request_id [93] timed out after [5000ms]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:698) ~[elasticsearch-2.4.4.jar:2.4.4]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_102]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_102]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_102]

2017-09-25 11:41:38.861  INFO 8756 --- [][generic][T#1]] org.elasticsearch.client.transport       : [Ghaur] failed to get node info for {#transport#-1}{192.168.3.50}{192.168.3.50:9200}, disconnecting...

org.elasticsearch.transport.ReceiveTimeoutTransportException: [][192.168.3.50:9200][cluster:monitor/nodes/liveness] request_id [94] timed out after [5000ms]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:698) ~[elasticsearch-2.4.4.jar:2.4.4]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_102]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_102]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_102]

2017-09-25 11:41:48.866  INFO 8756 --- [][generic][T#1]] org.elasticsearch.client.transport       : [Ghaur] failed to get node info for {#transport#-1}{192.168.3.50}{192.168.3.50:9200}, disconnecting...

可能是以下原因:

  1. 能否ping通192.168.3.50,可能存在网络问题不通。
  2. ES的版本是什么,可以版本不兼容。
  3. 用的什么ES客户端,是rest的?如果是transport的话,端口9200不对,应该用9300。(java客户端默认是9300
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值