Dates in Elasticsearch 5 with Groovy/Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='org.elasticsearch.client', module='transport', version='5.0.2') | |
@Grab(group='org.apache.logging.log4j', module='log4j-core', version='2.6.2') | |
@Grab(group='org.apache.logging.log4j', module='log4j-api', version='2.6.2') | |
import org.elasticsearch.action.admin.indices.create.CreateIndexAction | |
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder | |
import org.elasticsearch.action.index.IndexAction | |
import org.elasticsearch.action.index.IndexRequestBuilder | |
import org.elasticsearch.action.search.SearchResponse | |
import org.elasticsearch.action.support.WriteRequest | |
import org.elasticsearch.client.transport.TransportClient | |
import org.elasticsearch.common.settings.Settings | |
import org.elasticsearch.common.transport.InetSocketTransportAddress | |
import org.elasticsearch.transport.Netty4Plugin | |
import org.elasticsearch.transport.client.PreBuiltTransportClient | |
import org.elasticsearch.indices.IndexAlreadyExistsException | |
import java.time.Instant | |
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder | |
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery | |
List<String> hostAndPort = ['localhost:9300'] | |
Settings settings = Settings.builder() | |
.put("cluster.name", "elasticsearch") | |
.build() | |
TransportClient client = new PreBuiltTransportClient(settings, Collections.emptyList()) | |
hostAndPort.each { spec -> | |
def (host,port) = spec.split(':') | |
if (!port) { | |
port = '9300' | |
} | |
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.valueOf(port))) | |
} | |
CreateIndexRequestBuilder createIndexRequestBuilder = new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE) | |
.setIndex("test") | |
.addMapping("test", jsonBuilder().startObject() | |
.startObject("properties") | |
.startObject("doc_date") | |
.field("type", "date") | |
.field("format", "strict_date_optional_time||epoch_millis") | |
.field("store", true) | |
.endObject() | |
.endObject() | |
.endObject()) | |
try { | |
createIndexRequestBuilder.execute().actionGet() | |
} catch (IndexAlreadyExistsException e) { | |
} | |
IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client, IndexAction.INSTANCE) | |
.setIndex("test") | |
.setType("test") | |
.setId("1") | |
.setSource(jsonBuilder().startObject().field("doc_date", Instant.now()).endObject()) | |
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) | |
indexRequestBuilder.execute().actionGet() | |
SearchResponse response = client.prepareSearch() | |
.setQuery(matchAllQuery()) | |
.addStoredField("doc_date") | |
.execute().actionGet() | |
println response | |
response.hits.hits.each { hit -> | |
println hit.fields.get("doc_date").value() | |
Instant instant = Instant.parse(hit.fields.get("doc_date").value()) | |
long millis = instant.toEpochMilli() | |
println millis | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment