SHOW:
|
|
- or go back to the newest paste.
1 | - | @Grab(group='org.elasticsearch.client', module='transport', version='5.0.2') |
1 | + | @Grab(group='org.elasticsearch.client', module='transport', version='5.0.2') |
2 | - | @Grab(group='org.apache.logging.log4j', module='log4j-core', version='2.6.2') |
2 | + | @Grab(group='org.apache.logging.log4j', module='log4j-core', version='2.6.2') |
3 | - | @Grab(group='org.apache.logging.log4j', module='log4j-api', version='2.6.2') |
3 | + | @Grab(group='org.apache.logging.log4j', module='log4j-api', version='2.6.2') |
4 | - | |
4 | + | |
5 | - | import org.elasticsearch.action.admin.indices.create.CreateIndexAction |
5 | + | import org.elasticsearch.action.admin.indices.create.CreateIndexAction |
6 | - | import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder |
6 | + | import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder |
7 | - | import org.elasticsearch.action.index.IndexAction |
7 | + | import org.elasticsearch.action.index.IndexAction |
8 | - | import org.elasticsearch.action.index.IndexRequestBuilder |
8 | + | import org.elasticsearch.action.index.IndexRequestBuilder |
9 | - | import org.elasticsearch.action.search.SearchResponse |
9 | + | import org.elasticsearch.action.search.SearchResponse |
10 | - | import org.elasticsearch.action.support.WriteRequest |
10 | + | import org.elasticsearch.action.support.WriteRequest |
11 | - | import org.elasticsearch.client.transport.TransportClient |
11 | + | import org.elasticsearch.client.transport.TransportClient |
12 | - | import org.elasticsearch.common.settings.Settings |
12 | + | import org.elasticsearch.common.settings.Settings |
13 | - | import org.elasticsearch.common.transport.InetSocketTransportAddress |
13 | + | import org.elasticsearch.common.transport.InetSocketTransportAddress |
14 | - | import org.elasticsearch.transport.Netty4Plugin |
14 | + | import org.elasticsearch.transport.Netty4Plugin |
15 | - | import org.elasticsearch.transport.client.PreBuiltTransportClient |
15 | + | import org.elasticsearch.transport.client.PreBuiltTransportClient |
16 | - | import org.elasticsearch.indices.IndexAlreadyExistsException |
16 | + | import org.elasticsearch.indices.IndexAlreadyExistsException |
17 | - | |
17 | + | |
18 | - | import java.time.Instant |
18 | + | import java.time.Instant |
19 | - | |
19 | + | |
20 | - | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder |
20 | + | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder |
21 | - | import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery |
21 | + | import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery |
22 | - | |
22 | + | |
23 | - | List<String> hostAndPort = ['localhost:9300'] |
23 | + | List<String> hostAndPort = ['localhost:9300'] |
24 | - | Settings settings = Settings.builder() |
24 | + | Settings settings = Settings.builder() |
25 | - | .put("cluster.name", "elasticsearch") |
25 | + | .put("cluster.name", "elasticsearch") |
26 | - | .build() |
26 | + | .build() |
27 | - | TransportClient client = new PreBuiltTransportClient(settings, Collections.emptyList()) |
27 | + | TransportClient client = new PreBuiltTransportClient(settings, Collections.emptyList()) |
28 | - | hostAndPort.each { spec -> |
28 | + | hostAndPort.each { spec -> |
29 | - | def (host,port) = spec.split(':') |
29 | + | def (host,port) = spec.split(':') |
30 | - | if (!port) { |
30 | + | if (!port) { |
31 | - | port = '9300' |
31 | + | port = '9300' |
32 | - | } |
32 | + | } |
33 | - | client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.valueOf(port))) |
33 | + | client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.valueOf(port))) |
34 | } | |
35 | - | |
35 | + | |
36 | - | CreateIndexRequestBuilder createIndexRequestBuilder = new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE) |
36 | + | CreateIndexRequestBuilder createIndexRequestBuilder = new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE) |
37 | - | .setIndex("test") |
37 | + | .setIndex("test") |
38 | - | .addMapping("test", jsonBuilder().startObject() |
38 | + | .addMapping("test", jsonBuilder().startObject() |
39 | - | .startObject("properties") |
39 | + | .startObject("properties") |
40 | - | .startObject("doc_date") |
40 | + | .startObject("doc_date") |
41 | - | .field("type", "date") |
41 | + | .field("type", "date") |
42 | - | .field("format", "strict_date_optional_time||epoch_millis") |
42 | + | .field("format", "strict_date_optional_time||epoch_millis") |
43 | - | .field("store", true) |
43 | + | .field("store", true) |
44 | - | .endObject() |
44 | + | .endObject() |
45 | - | .endObject() |
45 | + | .endObject() |
46 | - | .endObject()) |
46 | + | .endObject()) |
47 | - | try { |
47 | + | try { |
48 | - | createIndexRequestBuilder.execute().actionGet() |
48 | + | createIndexRequestBuilder.execute().actionGet() |
49 | - | } catch (IndexAlreadyExistsException e) { |
49 | + | } catch (IndexAlreadyExistsException e) { |
50 | } | |
51 | - | |
51 | + | |
52 | - | IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client, IndexAction.INSTANCE) |
52 | + | IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client, IndexAction.INSTANCE) |
53 | - | .setIndex("test") |
53 | + | .setIndex("test") |
54 | - | .setType("test") |
54 | + | .setType("test") |
55 | - | .setId("1") |
55 | + | .setId("1") |
56 | - | .setSource(jsonBuilder().startObject().field("doc_date", Instant.now()).endObject()) |
56 | + | .setSource(jsonBuilder().startObject().field("doc_date", Instant.now()).endObject()) |
57 | - | .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
57 | + | .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
58 | - | |
58 | + | |
59 | - | indexRequestBuilder.execute().actionGet() |
59 | + | indexRequestBuilder.execute().actionGet() |
60 | - | |
60 | + | |
61 | - | SearchResponse response = client.prepareSearch() |
61 | + | SearchResponse response = client.prepareSearch() |
62 | - | .setQuery(matchAllQuery()) |
62 | + | .setQuery(matchAllQuery()) |
63 | - | .addStoredField("doc_date") |
63 | + | .addStoredField("doc_date") |
64 | - | .execute().actionGet() |
64 | + | .execute().actionGet() |
65 | - | |
65 | + | |
66 | - | println response |
66 | + | println response |
67 | - | |
67 | + | |
68 | - | response.hits.hits.each { hit -> |
68 | + | response.hits.hits.each { hit -> |
69 | - | println hit.fields.get("doc_date").value() |
69 | + | println hit.fields.get("doc_date").value() |
70 | - | Instant instant = Instant.parse(hit.fields.get("doc_date").value()) |
70 | + | Instant instant = Instant.parse(hit.fields.get("doc_date").value()) |
71 | - | long millis = instant.toEpochMilli() |
71 | + | long millis = instant.toEpochMilli() |
72 | - | println millis |
72 | + | println millis |
73 | } |