本文整理汇总了Java中io.searchbox.indices.CreateIndex类的典型用法代码示例。如果您正苦于以下问题:Java CreateIndex类的具体用法?Java CreateIndex怎么用?Java CreateIndex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CreateIndex类属于io.searchbox.indices包,在下文中一共展示了CreateIndex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUp
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
@PostConstruct
public void setUp() throws IOException {
CountResult result = client.execute(new Count.Builder()
.addIndex(AddOnInfoAndVersions.ES_INDEX)
.build());
if (result.isSucceeded()) {
logger.info("Existing ES index with " + result.getCount() + " documents");
} else {
// need to create the index
logger.info("Creating new ES index: " + AddOnInfoAndVersions.ES_INDEX);
handleError(client.execute(new CreateIndex.Builder(AddOnInfoAndVersions.ES_INDEX).build()));
}
logger.info("Updating mappings on ES index");
handleError(client.execute(new PutMapping.Builder(AddOnInfoAndVersions.ES_INDEX,
AddOnInfoAndVersions.ES_TYPE,
loadResource("elasticsearch/addOnInfoAndVersions-mappings.json")).build()));
}
示例2: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
public void createIndex() {
Settings.Builder settingsBuilder = Settings.settingsBuilder();
settingsBuilder.put("number_of_shards", esConfig.getNumberOfShards());
settingsBuilder.put("number_of_replicas", esConfig.getNumberOfReplicas());
// No nice way to build this with elasticsearch library
String mappings = "{ \"" + DEFAULT_TYPE + "\" : { \"properties\" : { \"@timestamp\" : {\"type\" : \"date\", \"format\" : \"epoch_millis\"}}}}";
try {
logger.info("Creating test index on ES, named {} with {} shards and {} replicas", indexName, esConfig.getNumberOfShards(), esConfig.getNumberOfReplicas());
client.execute(new CreateIndex.Builder(indexName).settings(settingsBuilder.build().getAsMap()).build());
client.execute(new PutMapping.Builder(indexName, DEFAULT_TYPE, mappings).build());
indexCreated = true;
} catch (IOException e) {
throw new RuntimeException("Could not create index in elasticsearch!", e);
}
}
示例3: createIndicesForTopics
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
public void createIndicesForTopics(Set<String> assignedTopics) {
for (String index : indicesForTopics(assignedTopics)) {
if (!indexExists(index)) {
CreateIndex createIndex = new CreateIndex.Builder(index).build();
try {
JestResult result = client.execute(createIndex);
if (!result.isSucceeded()) {
String msg = result.getErrorMessage() != null ? ": " + result.getErrorMessage() : "";
throw new ConnectException("Could not create index '" + index + "'" + msg);
}
} catch (IOException e) {
throw new ConnectException(e);
}
}
}
}
示例4: validateIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
public void validateIndex(String indexName) {
try {
IndicesExists indicesExistsRequest = new IndicesExists.Builder(indexName).build();
logger.debug("created indexExistsRequests: {}", indicesExistsRequest);
JestResult existsResult = client.execute(indicesExistsRequest);
logger.debug("indexExistsRequests result: {}", existsResult);
if (!existsResult.isSucceeded()) {
Settings settings = Settings.builder()
// .put("index.analysis.analyzer.default.type", "keyword")
.put("index.store.type", "mmapfs")
.build();
CreateIndex createIndexRequest = new CreateIndex.Builder(indexName).settings(settings).build();
execute(createIndexRequest);
//TODO: Make this work. Using the above "keyword" configuration in the meantime.
PutMapping putMapping = new PutMapping.Builder(indexName, "_default_", STRING_NOT_ANALYZED).build();
execute(putMapping);
logger.info("created index with settings: {}, indexName: {}, putMapping: {}", settings, indexName, putMapping);
}
} catch (IOException e) {
logger.error("failed to connect to elastic cluster", e);
}
}
示例5: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
@Override
public boolean createIndex() {
JestClient client = esrResource.getClient();
try{
JestResult result = client.execute(new IndicesExists.Builder(index).build());
if(result.getResponseCode() != 200){
client.execute(new CreateIndex.Builder(index).build());
return true;
}
}catch(IOException ioe){
getMonitor().error("Unable to create index", ioe);
}
return false;
}
示例6: deleteAll
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
@Override
public void deleteAll() throws IOException {
// Delete the index, if it exists.
JestResult result = client.execute(new IndicesExists.Builder(indexName).build());
if (result.isSucceeded()) {
result = client.execute(new DeleteIndex.Builder(indexName).build());
if (!result.isSucceeded()) {
throw new IOException(
String.format("Failed to delete index %s: %s", indexName, result.getErrorMessage()));
}
}
// Recreate the index.
result = client.execute(new CreateIndex.Builder(indexName).settings(getMappings()).build());
if (!result.isSucceeded()) {
String error =
String.format("Failed to create index %s: %s", indexName, result.getErrorMessage());
throw new IOException(error);
}
}
示例7: builderIndex_OneRecord
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
public void builderIndex_OneRecord(String json, boolean cleanBeforeInsert) {
long start = System.currentTimeMillis();
try {
if (cleanBeforeInsert) {
jestHttpClient.execute(new DeleteIndex(new DeleteIndex.Builder(_esIndexName)));
}
JestResult jestResult = jestHttpClient.execute(new IndicesExists.Builder(_esIndexName).build());
if (!jestResult.isSucceeded()) {
jestHttpClient.execute(new CreateIndex.Builder(_esIndexName).build());
}
Index index = new Index.Builder(json)
.index(_esIndexName)
.type(_esTypeName)
.build();
jestHttpClient.execute(index);
// jestHttpClient.shutdownClient();
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
LOG.info("->> One Record(default id): time for create index --> " + (end - start) + " milliseconds");
}
示例8: builderIndex_Bulk
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
public void builderIndex_Bulk(List<Entity> entityList, boolean cleanBeforeInsert) {
int nRecords = entityList.size();
long start = System.currentTimeMillis();
try {
if (cleanBeforeInsert) {
jestHttpClient.execute(new DeleteIndex(new DeleteIndex.Builder(_esIndexName)));
}
JestResult jestResult = jestHttpClient.execute(new IndicesExists.Builder(_esIndexName).build());
if (!jestResult.isSucceeded()) {
jestHttpClient.execute(new CreateIndex.Builder(_esIndexName).build());
}
SerializeBeans2JSON sb2json = new SerializeBeans2JSON();
Bulk.Builder bulkBuilder = new Bulk.Builder();
for (int i = 0; i < nRecords; i++) {
Index index = new Index
.Builder(sb2json.serializeBeans2JSON(entityList.get(i)))
.index(_esIndexName)
.type(_esTypeName)
.id(entityList.get(i).getEntityID())
.build();
bulkBuilder.addAction(index);
}
jestHttpClient.execute(bulkBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
LOG.info("->> Bulk: total time for create index --> " + (end - start) + " milliseconds, # of records: " + nRecords);
}
示例9: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
private static void createIndex(String indexName, JestClient client) {
try {
client.execute(new CreateIndex.Builder(indexName).build());
} catch (IOException e) {
e.printStackTrace();
}
}
示例10: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
@Override
public boolean createIndex(String indexName, Object settings) {
CreateIndex.Builder createIndexBuilder = new CreateIndex.Builder(indexName);
if (settings instanceof String) {
createIndexBuilder.settings(String.valueOf(settings));
} else if (settings instanceof Map) {
createIndexBuilder.settings(settings);
} else if (settings instanceof XContentBuilder) {
createIndexBuilder.settings(settings);
}
return executeWithAcknowledge(createIndexBuilder.build());
}
示例11: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
/**
* Create an index with the given settings as json.
*
* @param index the name of the index
* @param settings the settings json
*/
public void createIndex(String index, JsonObject settings) {
JestResult result = execute(new CreateIndex.Builder(index).settings(settings).build());
if (!result.isSucceeded()) {
throw new ElasticsearchIndexCreateException(index, result.getErrorMessage());
}
}
示例12: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
/**
* Create an index in Elasticsearch. If necessary, this function should
* check whether a new index is required.
*
* @return true if a new index has been created, false otherwise
*/
public boolean createIndex() {
JestClient client = esrResource.getClient();
try {
JestResult result = client.execute(new IndicesExists.Builder(index).build());
if (result.getResponseCode() != 200) {
client.execute(new CreateIndex.Builder(index).build());
return true;
}
} catch (IOException ioe) {
getMonitor().error("Unable to create index", ioe);
}
return false;
}
示例13: init
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
@PostConstruct
public void init() throws Exception {
log.info("Instantiating jestSearchIndexingService...");
log.info(String.format("Checking if %s needs to be created", LOCATION_INDEX_NAME));
IndicesExists indicesExists = new IndicesExists.Builder(LOCATION_INDEX_NAME).build();
JestResult r = jestClient.execute(indicesExists);
if (!r.isSucceeded()) {
log.info("Index does not exist. Creating index...");
// create new index (if u have this in elasticsearch.yml and prefer
// those defaults, then leave this out
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
settings.put("number_of_shards", 1);
settings.put("number_of_replicas", 0);
CreateIndex.Builder builder = new CreateIndex.Builder(LOCATION_INDEX_NAME);
builder.settings(settings.build().getAsMap());
CreateIndex createIndex = builder.build();
log.info(createIndex.toString());
jestClient.execute(createIndex);
log.info("Index created");
} else {
log.info("Index already exist!");
}
}
示例14: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
/**
* @param indexName
* @throws Exception
*/
private void createIndex(String indexName) throws Exception {
URL settings = getClass().getResource("index-settings.json"); //$NON-NLS-1$
String source = IOUtils.toString(settings);
JestResult response = esClient.execute(new CreateIndex.Builder(indexName).settings(source).build());
if (!response.isSucceeded()) {
throw new StorageException("Failed to create index " + indexName + ": " + response.getErrorMessage()); //$NON-NLS-1$ //$NON-NLS-2$
}
}
示例15: createIndex
import io.searchbox.indices.CreateIndex; //导入依赖的package包/类
/**
* Creates an index.
* @param indexName
* @throws Exception
*/
protected void createIndex(JestClient client, String indexName, String settingsName) throws Exception {
URL settings = AbstractClientFactory.class.getResource(settingsName);
String source = IOUtils.toString(settings);
JestResult response = client.execute(new CreateIndex.Builder(indexName).settings(source).build());
if (!response.isSucceeded()) {
// When running in e.g. Wildfly, the Gateway exists as two separate WARs - the API and the
// runtime Gateway itself. They both create a registry and thus they both try to initialize
// the ES index if it doesn't exist. A race condition could result in both WARs trying to
// create the index. So a result of "IndexAlreadyExistsException" should be ignored.
if (!response.getErrorMessage().startsWith("IndexAlreadyExistsException")) { //$NON-NLS-1$
throw new Exception("Failed to create index: '" + indexName + "' Reason: " + response.getErrorMessage()); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}