I am trying to recreate this example using the Java API more or less.
I think all I need is to add a mapping to the index, but the Java API docs are not exactly clear on how to do this.
Please tell me how I create a mapping in Java that is the equivalent of this from the example in the documentation :
curl -X PUT localhost:9200/test/tweet/_mapping -d '{
"tweet" : {
"_ttl" : { "enabled" : true, "default" : "1d" }
}
}'
Here's my code:
package foo;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.io.IOException;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
public class MyTestClass {
private static Client getClient() {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
TransportClient transportClient = new TransportClient(settings);
transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
return (Client) transportClient;
}
public static void main(String[] args) throws IOException, InterruptedException {
final Client client = getClient();
// Create Index and set settings and mappings
final String indexName = "test";
final String documentType = "tweet";
final String documentId = "1";
final String fieldName = "foo";
final String value = "bar";
IndicesExistsResponse res = client.admin().indices().prepareExists(indexName).execute().actionGet();
if (res.isExists()) {
DeleteIndexRequestBuilder delIdx = client.admin().indices().prepareDelete(indexName);
delIdx.execute().actionGet();
}
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);
// MAPPING GOES HERE
// createIndexRequestBuilder.addMapping(documentType, WHATEVER THE MAPPING IS);
// MAPPING DONE
createIndexRequestBuilder.execute().actionGet();
// Add documents
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, documentType, documentId);
// build json object
XContentBuilder contentBuilder = jsonBuilder().startObject().prettyPrint();
contentBuilder.field(fieldName, value);
indexRequestBuilder.setSource(contentBuilder);
indexRequestBuilder.execute().actionGet();
// Get document
System.out.println(getValue(client, indexName, documentType, documentId, fieldName));
Thread.sleep(10000L);
// Try again
System.out.println(getValue(client, indexName, documentType, documentId, fieldName));
}
protected static String getValue(final Client client, final String indexName, final String documentType,
final String documentId, final String fieldName) {
GetRequestBuilder getRequestBuilder = client.prepareGet(indexName, documentType, documentId);
getRequestBuilder.setFields(new String[] { fieldName });
GetResponse response2 = getRequestBuilder.execute().actionGet();
String name = response2.getField(fieldName).getValue().toString();
return name;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…