使用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:
{}", searchQuery.getQuery().toString());