Last active
August 29, 2015 14:00
-
-
Save jhsbeat/11342294 to your computer and use it in GitHub Desktop.
Elasticsearch overwriting mapping problem GIST
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
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Properties; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; | |
import org.elasticsearch.action.bulk.BulkRequestBuilder; | |
import org.elasticsearch.action.bulk.BulkResponse; | |
import org.elasticsearch.client.transport.TransportClient; | |
import org.elasticsearch.common.settings.ImmutableSettings; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.common.transport.InetSocketTransportAddress; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.transaction.TransactionConfiguration; | |
import org.springframework.transaction.annotation.Transactional; | |
import org.springframework.util.Log4jConfigurer; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = "classpath:spring/analysor/context-*.xml") | |
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) | |
@Transactional | |
public class GistTest { | |
private final Properties prop = new Properties(); | |
private InputStream input = null; | |
private TransportClient client = null; | |
private final String index = "my_index"; | |
private final String type = "my_type"; | |
static { | |
try { | |
Log4jConfigurer.initLogging("classpath:properties/log4j.properties"); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Before | |
public void setup() throws IOException { | |
} | |
@Test | |
public void testPutMappingTemplate() throws Exception{ | |
System.out.println("Read _Mapping Template Test START"); | |
input = new GistTest().getClass().getClassLoader().getResourceAsStream("properties/application.properties"); | |
prop.load(input); | |
// Elasticsearch Transport client 생성 | |
Settings settings = ImmutableSettings.settingsBuilder() | |
.put("cluster.name", prop.getProperty("es.cluster.name")) | |
.put("action.auto_create_index", "true") | |
.build(); | |
client = new TransportClient(settings); | |
client.addTransportAddress(new InetSocketTransportAddress(prop.getProperty("es.host.name"), Integer.parseInt(prop.getProperty("es.transport.port")))); | |
String json = ""; | |
try { | |
json += "{\"" + type + "\":"; | |
BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("properties/es_template/my_mapping_template.json"))); | |
StringBuilder sb = new StringBuilder(); | |
String readLine = ""; | |
while((readLine = br.readLine()) != null){ | |
sb.append(readLine); | |
} | |
br.close(); | |
System.out.println("##### Content: " + sb.toString()); | |
json += sb.toString(); | |
json += "}"; | |
PutMappingResponse pmrs = client.admin().indices().preparePutMapping(index).setType(type).setSource(json).setIgnoreConflicts(true).execute().actionGet(); | |
if (pmrs == null || !pmrs.isAcknowledged()) { | |
System.out.println("Put Mapping Failure."); | |
} else { | |
System.out.println("Put Mapping Success."); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
System.out.println("##### JSON : " + json); | |
System.out.println("Read UserData _Mapping Template Test END"); | |
assert(true); | |
} | |
@Test | |
public void testIndexNewObject() throws Exception{ | |
input = new GistTest().getClass().getClassLoader().getResourceAsStream("properties/application.properties"); | |
prop.load(input); | |
// Elasticsearch Transport client 생성 | |
Settings settings = ImmutableSettings.settingsBuilder() | |
.put("cluster.name", prop.getProperty("es.cluster.name")) | |
.put("action.auto_create_index", "true") | |
.build(); | |
client = new TransportClient(settings); | |
client.addTransportAddress(new InetSocketTransportAddress(prop.getProperty("es.host.name"), Integer.parseInt(prop.getProperty("es.transport.port")))); | |
BulkRequestBuilder bulkBuilder = client.prepareBulk(); | |
MyData myData = new MyData(); | |
myData.setDataId(new Long(2)); | |
List<InnerData> myFieldList = new ArrayList<InnerData>(); | |
InnerData innerData = new InnerData(); | |
innerData.setMyFieldExact("ABCD-1"); | |
myFieldList.add(innerData); | |
innerData = new InnerData(); | |
innerData.setMyFieldExact("ABCD-2"); | |
myFieldList.add(innerData); | |
myData.setMyFieldList(myFieldList); | |
ObjectMapper mapper = new ObjectMapper(); | |
String json = mapper.writeValueAsString(myData); | |
bulkBuilder.add(client.prepareIndex(index, type, "my_id") | |
.setSource(json)); | |
BulkResponse bulkRes = bulkBuilder.execute().actionGet(); | |
if(bulkRes.hasFailures()){ | |
assert(false); | |
} | |
assert(true); | |
} | |
} |
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
public class InnerData { | |
private static final long serialVersionUID = -3921932215406305574L; | |
private String myFieldExact; | |
public String getMyFieldExact() { | |
return myFieldExact; | |
} | |
public void setMyFieldExact(String myFieldExact) { | |
this.myFieldExact = myFieldExact; | |
} | |
} |
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
{ | |
"properties":{ | |
"dataId":{ | |
"type":"long" | |
}, | |
"myFieldList":{ | |
"properties":{ | |
"myFieldExact":{ | |
"type":"string", | |
"index": "not_analyzed", | |
"dynamic": false | |
} | |
} | |
} | |
} | |
} |
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
import java.util.List; | |
public class MyData { | |
private static final long serialVersionUID = -3921932215406305574L; | |
private Long dataId; | |
private List<InnerData> myFieldList; | |
public Long getDataId() { | |
return dataId; | |
} | |
public void setDataId(Long dataId) { | |
this.dataId = dataId; | |
} | |
public List<InnerData> getMyFieldList() { | |
return myFieldList; | |
} | |
public void setMyFieldList(List<InnerData> myFieldList) { | |
this.myFieldList = myFieldList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment