/
Mapping.java
138 lines (111 loc) · 4.76 KB
/
Mapping.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.indexer;
import com.google.common.collect.Maps;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.client.Client;
import org.graylog2.indexer.messages.Messages;
import org.graylog2.plugin.Tools;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Representing the message type mapping in ElasticSearch. This is giving ES more
* information about what the fields look like and how it should analyze them.
*
* @author Lennart Koopmann <lennart@socketfeed.com>
*/
@SuppressWarnings({"unchecked"})
public class Mapping {
public static PutMappingRequest getPutMappingRequest(final Client client, final String index, final String analyzer) {
final PutMappingRequestBuilder builder = client.admin().indices().preparePutMapping(new String[] {index});
builder.setType(Messages.TYPE);
final Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("properties", partFieldProperties(analyzer));
mapping.put("dynamic_templates", partDefaultAllInDynamicTemplate());
mapping.put("_source", enabledAndCompressed()); // Compress source field..
mapping.put("_ttl", enabled()); // Enable purging by TTL.
// TODO: use multimap?
final Map<String, Map<String, Object>> completeMapping = Maps.newHashMap();
completeMapping.put(Messages.TYPE, mapping);
builder.setSource(completeMapping);
return builder.request();
}
/*
* Disable analyzing for every field by default.
*/
// TODO warnings
@SuppressWarnings("rawtypes")
private static List partDefaultAllInDynamicTemplate() {
final List dynamicTemplates = new LinkedList();
final Map template = new HashMap();
final Map defaultAll = new HashMap();
final Map notAnalyzed = new HashMap();
notAnalyzed.put("index", "not_analyzed");
// Match all.
defaultAll.put("match", "*");
// Analyze nothing by default.
defaultAll.put("mapping", notAnalyzed);
template.put("store_generic", defaultAll);
dynamicTemplates.add(template);
return dynamicTemplates;
}
/*
* Enable analyzing for some fields again. Like for message and full_message.
*/
// TODO warnings
@SuppressWarnings("rawtypes")
private static Map partFieldProperties(String analyzer) {
final Map<String, Map<?, ?>> properties = Maps.newHashMap();
properties.put("message", analyzedString(analyzer));
properties.put("full_message", analyzedString(analyzer));
// http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html
// http://www.elasticsearch.org/guide/reference/mapping/date-format.html
properties.put("timestamp", typeTimeWithMillis());
// to support wildcard searches in source we need to lowercase the content (wildcard search lowercases search term)
properties.put("source", analyzedString("analyzer_keyword"));
return properties;
}
private static Map<String, String> analyzedString(String analyzer) {
final Map<String, String> type = Maps.newHashMap();
type.put("index", "analyzed");
type.put("type", "string");
type.put("analyzer", analyzer);
return type;
}
private static Map<String, Serializable> typeTimeWithMillis() {
final Map<String, Serializable> type = Maps.newHashMap();
type.put("type", "date");
type.put("format", Tools.ES_DATE_FORMAT);
type.put("doc_values", true);
return type;
}
private static Map<String, Boolean> enabled() {
final Map<String, Boolean> e = Maps.newHashMap();
e.put("enabled", true);
return e;
}
private static Map<String, Boolean> enabledAndCompressed() {
final Map<String, Boolean> e = Maps.newHashMap();
e.put("enabled", true);
e.put("compress", true);
return e;
}
}