赞
踩
目标:通过分词能达到预期的数据检索结果,并能完成数据的统计分析
ES版本:7.10.2
客户端语言:DSL、Java
针对Java 使用的是spring-data-elasticsearch-4.2.x框架
POST hotel/_doc/001
{
"title": "喜来乐酒店",
"city": "苏州",
"price": 578.23
}
查询这块是最复杂的,也是难点所在
GET hotel/_doc/001
GET hotel/_search
{
"query": {
"term": {
"city": "苏州"
}
}
}
GET hotel/_search
{
"query": {
"match": {
"title": "来乐"
}
}
}
ES底层是无法完成部分字段更新功能的,只能对整个文档进行更新,即使ES提供了部分字段更新的api,也是内部先把整个文档获取出来,然后再进行更新,每次更新后,对应的版本号也会自动增加.
POST /hotel/_update/001
{
"doc": {
"title" : "喜来乐酒店",
"city" : "南京",
"price" : 578.23
}
}
POST /hotel/_update_by_query
{
"query": {
"term": {
"city": {
"value": "盐城"
}
}
},
"script": {
"source": "ctx._source['city']='苏州'",
"lang": "painless"
}
}
JAVA 实现方式
String script = "ctx._source['city']=params['newCityName']";
Map<String, Object> params = Collections.singletonMap("newCityName", "苏州");
UpdateByQueryRequest request = new UpdateByQueryRequest("hotel") //
.setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("city", "盐城"))) //
.setAbortOnVersionConflict(true) //
.setRefresh(true) //
.setScript(new Script(ScriptType.INLINE, "painless", script, params)); //
elasticsearchRestTemplate.execute(client -> client.updateByQuery(request, RequestOptions.DEFAULT));
DELETE /hotel/_doc/001
POST /hotel/_delete_by_query
{
"query": {
"term": {
"city": {
"value": "盐城"
}
}
}
}
JAVA 实现方式
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("city", "盐城"))
.build();
elasticsearchRestTemplate.delete(searchQuery, Hotel.class);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。