ES之案例分析

创建索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
PUT demo
{
"mappings":{
"doc":{
"properties":{
"title":{
"type":"text",
"analyzer": "ik_max_word"
},
"content":{
"type":"text",
"analyzer": "ik_max_word"
},
"uniqueId":{
"type":"keyword",
"index":"not_analyzed"
},
"created": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
},
"settings":{
"number_of_shards":3,
"number_of_replicas":1
}
}

关键java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder = JsonXContent.contentBuilder()
.startObject()
.startObject("mappings")
.startObject("doc")
.startObject("properties")
.startObject("title")
.field("type","text")
.field("analyzer","ik_max_word")
.endObject()
.startObject("content")
.field("type","text")
.field("index","analyzed")
.field("analyzer","ik_max_word")
.endObject()
.startObject("uniqueId")
.field("type","keyword")
.field("index","not_analyzed")
.endObject()
.startObject("created")
.field("type","date")
.field("format","strict_date_optional_time||epoch_millis")
.endObject()
.endObject()
.endObject()
.endObject()
.startObject("settings")
.field("number_of_shards",3)
.field("number_of_replicas",1)
.endObject()
.endObject();
indexRequest.source(builder);
// 生成json字符串
String source = indexRequest.source().utf8ToString();
HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
// 使用RestClient进行操作 而非rhlClient
Response response = client.performRequest("put", "/demo", Collections.<String, String> emptyMap(),
entity);

创建映射

1
2
3
4
5
6
7
8
9
10
POST newspaper/sports/_mapping
{
"properties":{
"content":{
"type":"text",
"analyzer":"ik_max_word",
"index":"analyzed"
}
}
}

关键java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("content")
.field("type","text")
.field("analyzer","ik_max_word")
.field("index","analyzed")
.endObject()
.endObject()
.endObject();
indexRequest.source(builder);
String source = indexRequest.source().utf8ToString();
HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
Response response = client.performRequest("post", "/newspaper/sports/_mapping", Collections.<String, String> emptyMap(),
entity);

关于索引的其他操作

  1. 判断索引是否存在
1
2
Response response = restClient.performRequest("HEAD", index);
boolean exist = response.getStatusLine().getReasonPhrase().equals("OK");
  1. 删除索引
1
Response response = restClient.performRequest("DELETE", indexName);

查询文档

实例1:2018年1月26日早八点到晚八点关于费德勒的前十条体育新闻的标题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
POST demo/demo/_search
{
"from":"0",
"size":"10",
"_source":["title"],
"query":{
"bool":{
"must":{
"match":{
"title":"费德勒"
}
},
"must":{
"term":{"tag":"体育"}
},
"must":{
"range":{
"publishTime":{
"gte":"2018-01-26T08:00:00Z",
"lte":"2018-01-26T20:00:00Z"
}
}
}
}
}

}

对应的 RestHighLevelClient 方式的关键代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.fetchSource(new String[]{"title"}, new String[]{});
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "费德勒");
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("tag", "体育");
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishTime");
rangeQueryBuilder.gte("2018-01-26T08:00:00Z");
rangeQueryBuilder.lte("2018-01-26T20:00:00Z");
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
boolBuilder.must(matchQueryBuilder);
boolBuilder.must(termQueryBuilder);
boolBuilder.must(rangeQueryBuilder);
sourceBuilder.query(boolBuilder);
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
searchRequest.source(sourceBuilder);

更新文档

1
2
3
4
5
6
POST demo/demo/2/_update
{
"doc":{
"tag":"网球"
}
}

关键java代码:

1
2
3
4
UpdateRequest updateRequest = new UpdateRequest(index, type, id);
Map<String, String> map = new HashMap<>();
map.put("tag", "网球");
updateRequest.doc(map);

删除文档

  1. ID删除方式
1
DELETE demo/demo/3

关键java代码:

1
2
DeleteRequest deleteRequest = new DeleteRequest(_index,_type,_id);
DeleteResponse response = restHighLevelClient.delete(deleteRequest);
  1. Delete By Query,即先查询再删除的方式
1
2
3
4
5
6
7
8
POST demo/demo/_delete_by_query
{
"query":{
"match":{
"content":"test1"
}
}
}

关键java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.timeout(new TimeValue(2, TimeUnit.SECONDS));
TermQueryBuilder termQueryBuilder1 = QueryBuilders.termQuery("content.keyword", _deleteText);
sourceBuilder.query(termQueryBuilder1);
SearchRequest searchRequest = new SearchRequest(_index);
searchRequest.types(_type);
searchRequest.source(sourceBuilder);
SearchResponse response = rhlClient.search(searchRequest);
SearchHits hits = response.getHits();
List<String> docIds = new ArrayList<>(hits.getHits().length);
for (SearchHit hit : hits) {
docIds.add(hit.getId());
}

BulkRequest bulkRequest = new BulkRequest();
for (String id : docIds) {
DeleteRequest deleteRequest = new DeleteRequest(_index, _type, _id);
bulkRequest.add(deleteRequest);
}
restHighLevelClient.bulk(bulkRequest);

附录

一个实际项目的mapping设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
"mapping": {
"doc": {
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"apply_no": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"child_type": {
"type": "keyword"
},
"description": {
"type": "text",
"analyzer": "ik_max_word"
},
"emerging": {
"type": "nested",
"properties": {
"code": {
"type": "keyword"
},
"isChosen": {
"type": "boolean"
},
"level": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
},
"goodsInquiries": {
"type": "long"
},
"goods_type": {
"type": "keyword"
},
"goodsinquiries": {
"type": "long"
},
"hit_time": {
"type": "date"
},
"hits": {
"type": "long"
},
"id": {
"type": "long"
},
"ipc_code": {
"type": "keyword"
},
"ipc_name": {
"type": "text",
"analyzer": "ik_max_word"
},
"name": {
"type": "text",
"analyzer": "standard"
},
"owner": {
"type": "keyword"
},
"price": {
"type": "long"
},
"publish_time": {
"type": "date"
},
"status": {
"type": "keyword"
},
"tags": {
"type": "text",
"analyzer": "ik_max_word"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"update_time": {
"type": "date"
},
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
文章作者: gqsu
文章链接: http://www.ipdax.com/2018/04/03/elasticsearch-案例分析java代码/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 技术笔记分享
支付宝打赏
微信打赏