Advertisement
hivefans

es_write_search

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