spring-data-elasticsearch使用笔记
使用spring-data遇到了一些问题,记录一下。
spring-data-elasticsearch版本选择
这里有一份官方github上的spring-data-elasticsearch与elasticsearch的对应关系表,但是不太完整,但是还是比较有参考价值的
| spring data elasticsearch | elasticsearch |
|---|---|
| 3.0.0.RC2 | 5.5.0 |
| 3.0.0.M4 | 5.4.0 |
| 2.0.4.RELEASE | 2.4.0 |
| 2.0.0.RELEASE | 2.2.0 |
| 1.4.0.M1 | 1.7.3 |
| 1.3.0.RELEASE | 1.5.2 |
| 1.2.0.RELEASE | 1.4.4 |
| 1.1.0.RELEASE | 1.3.2 |
| 1.0.0.RELEASE | 1.1.1 |
elasticsearch的客户端版本必须与服务端版本主版本保持一致。
参考:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html
The client must have the same major version (e.g. 2.x, or 5.x) as the nodes in the cluster. Clients may connect to clusters which have a different minor version (e.g. 2.3.x) but it is possible that new functionality may not be supported. Ideally, the client should have the same version as the cluster.
由于公司使用的elasticsearch 2.1.1,所以选择了spring-data-elasticsearch 2.1.7.RELEASE(2.1.7对应的原生客户端版本是elasticsearch 2.4.0,上面表格可能不完整)。
遇到异常java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation
原因是项目使用的springframework版本为4.1.6,而spring-data-elasticsearch 2.1.7默认依赖的spring-context是4.3.11,所以初步确定是我们的项目使用的spring版本太低导致。
参考spring的api文档,发现原来AnnotatedElementUtils.findMergedAnnotation是4.2版才有的(since 4.2)。
@Field日期类型
@Field(type = FieldType.Date,
index = FieldIndex.not_analyzed,
format = DateFormat.custom,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
private Date createTime;
@Document中动态indexName
配置Bean
@Component("esConfig")
public class ESConfig {
@Value("${app.env}")
private String env;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
}
@Document注解
@Document(indexName = "index-#{esConfig.env}", type = "typename", shards = 4, replicas = 1)
安装ik插件
从官网下载es版本对应版本的ik插件,https://github.com/medcl/elasticsearch-analysis-ik
我开发环境安装的es是2.4.1,一开始放在了D:\Program Files\下,结果加入ik插件后就启动不了了,原来是不支持带空格的路径,换了路径就好了。(参考)
测试
链接:http://localhost:9200/_analyze?analyzer=stardard&pretty=true&text=今天天气真好
{
"tokens" : [ {
"token" : "今",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
}, {
"token" : "天",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
}, {
"token" : "天",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
}, {
"token" : "气",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
}, {
"token" : "真",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
}, {
"token" : "好",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
} ]
}
链接:http://localhost:9200/_analyze?analyzer=ik&pretty=true&text=今天天气真好
{
"tokens" : [ {
"token" : "今天天气",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
}, {
"token" : "今天",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
}, {
"token" : "天天",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 2
}, {
"token" : "天气",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
}, {
"token" : "真好",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 4
} ]
}
head插件安装和使用
elasticsearch client api
or and
sql: select * from table where active=1 and ( name like '%?%' or code like '%?%' )
elasticsearch 用java client怎么写呢?
--------------------------------------
QueryBuilder qb = QueryBuilders.boolQuery()
.must(new QueryStringQueryBuilder("1").field("active"))
.must(QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("name", "小李子"))
.should(QueryBuilders.matchQuery("code", 小李子"))
);
in
sql: select * from table where name in ('tom', 'john');
QueryBuilder qb = QueryBuilders.boolQuery()
List<String> list = new ArrayList<String>();
list.add("tom");
list.add("john");
BoolQueryBuilder in = QueryBuilders.boolQuery();
for(String name : list) {
in.shoud(QueryBuilders.matchPhraseQuery("name", name));
}
qb.must(in);
打印dsl日志
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder);
builder.withPageable(new PageRequest(param.getPageNo(), 20))
.withSort(new FieldSortBuilder("updateTime").order(SortOrder.DESC));
SearchQuery searchQuery = builder.build();
logger.info("QueryDSL:\n{}", searchQuery.getQuery().toString());
spring-data-elasticsearch使用笔记的更多相关文章
- 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分
Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...
- elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...
- Elasticsearch基本用法(2)--Spring Data Elasticsearch
Spring Data Elasticsearch是Spring Data项目下的一个子模块. 查看 Spring Data的官网:http://projects.spring.io/spring-d ...
- Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...
- spring data elasticsearch 使用
很久之前就安装了elasticsearch,一直没用java用过,最近看了一下spring data系列的elasticsearch,这里写一篇心得. 如果尚未安装elasticsearch,可以 参 ...
- Spring Boot + Spring Data + Elasticsearch实例
Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...
- elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词
elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/rel ...
- How to provide highlighting with Spring data elasticsearch
How to provide highlighting with Spring data elasticsearch @Test public void shouldReturnHighlighted ...
- springboot集成spring data ElasticSearch
ES支持SpringBoot使用类似于Spring Data Jpa的方式查询,使得查询更加方便. 1.依赖引入 compile “org.springframework.boot:spring-bo ...
- SpringBoot整合Spring Data Elasticsearch
Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticsearch文档之间的映射 elasticsearch本质也是存 ...
随机推荐
- C# 的 Task、Thread、ThreadPool 之间有什么异同?
Thread就是Thread,需要自己调度,适合长跑型的操作. ThreadPool是Thread基础上的一个线程池,目的是减少频繁创建线程的开销.线程很贵,要开新的stack,要增加CPU上下文切换 ...
- vue生态圈
本文来自网易云社区 作者:刘凌阳 前言 公司社区上关于Vue的文章挺少的(少的可怜),不禁为Vue愤愤不平,此文应运而生. 但笔者水平有限,也写不了什么特别高深的东西,只能简单介绍下Vue生态圈,如有 ...
- 「POJ 1741」Tree
题面: Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define ...
- ubuntu14.04,安装Chrome(谷歌浏览器)
Linux:ubuntu14.04 一直都很喜欢谷歌浏览器,进入linux怎么能没有? 安装方法:谷歌浏览器官方下载的ubuntu版本,下载后点击即可安装. 下载地址:http://download. ...
- 51 nod 1267 4个数和为0
1267 4个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出& ...
- Javascript 上传文件到Azure存储
对一些前端工程师来讲,使用javascript上传文件到Azure存储中可能是需要掌握的技能,希望这篇博客能给到帮助. 在开始前我们需要了解以下几点: 共享访问签名(Shared Access Sig ...
- C# 精准计时之 QueryPerformanceCounter QueryPerformanceFrequency用法
C# 用法: public static class QueryPerformanceMethd { [DllImport("kernel32.dll")] public exte ...
- jq学习笔记(一)
1 .attr() 与 .removeAttr()方法 - atr()方法用来获取和设置元素属性 attr()有4个表达式: attr(传入属性名):获取属性的值 attr(属性名, 属性值):设置属 ...
- count distinct 组合使用
SELECT COUNT(DISTINCT Lbox_Sn) FROM Tab_History_Info
- shell-006:检测80端口的存活情况
注意细节问题,如下图所示 #!/bin/bash # 检测80端口是否存在 while : do n=`netstat -lnpt |grep ':80 ' |wc -l` if [ $n -eq ] ...