org.elasticsearch.action.admin.indices.get.GetIndexRequest Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.get.GetIndexRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
ElasticsearchSchema(Map<String, Integer> esAddresses,
                    Map<String, String> esConfig, String esIndexName) {
    super();
    client = new ElasticsearchUtil().getEsClient(esAddresses, esConfig);
    if (client != null) {
        final String[] indices = client.admin().indices()
                .getIndex(new GetIndexRequest().indices(esIndexName))
                .actionGet().getIndices();
        if (indices.length == 1) {
            index = indices[0];
        } else {
            index = null;
        }
    } else {
        index = null;
    }

}
 
Example #2
Source File: InferenceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #3
Source File: ElasticsearchStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #4
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #5
Source File: ElasticsearchStoreWALTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #6
Source File: ElasticsearchConfiguration.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
public static boolean createIndex(RestHighLevelClient client, String indexName, String indexType) {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    try {
        if (!client.indices().exists(request)) {
            LOGGER.info("index {} does not exist, creating one", indexName);
            CreateIndexRequest createReq = new CreateIndexRequest(indexName);
            createReq.settings(getResourceContent(SETTINGS_RESOURCE_NAME), JSON);
            createReq.mapping(indexType, getResourceContent(MAPPING_RESOURCE_NAME), JSON);
            client.indices().create(createReq);
            return true;
        }
    } catch (IOException e) {
        throw new ConfigurationException(e);
    }
    return false;
}
 
Example #7
Source File: ElasticSearchSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void createIndexIfNeeded() throws IOException {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(elasticSearchConfig.getIndexName());
    boolean exists = getClient().indices().exists(request);

    if (!exists) {
        CreateIndexRequest cireq = new CreateIndexRequest(elasticSearchConfig.getIndexName());

        cireq.settings(Settings.builder()
           .put("index.number_of_shards", elasticSearchConfig.getIndexNumberOfShards())
           .put("index.number_of_replicas", elasticSearchConfig.getIndexNumberOfReplicas()));

        CreateIndexResponse ciresp = getClient().indices().create(cireq);
        if (!ciresp.isAcknowledged() || !ciresp.isShardsAcknowledged()) {
            throw new RuntimeException("Unable to create index.");
        }
    }
}
 
Example #8
Source File: ActionRequestRestExecuter.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
/**
 * Execute the ActionRequest and returns the REST response using the channel.
 */
public void execute() throws Exception {
       ActionRequest request = requestBuilder.request();

       //todo: maby change to instanceof multi?
       if(requestBuilder instanceof JoinRequestBuilder){
           executeJoinRequestAndSendResponse();
       }
	else if (request instanceof SearchRequest) {
		client.search((SearchRequest) request, new RestStatusToXContentListener<SearchResponse>(channel));
	} else if (requestBuilder instanceof SqlElasticDeleteByQueryRequestBuilder) {
           throw new UnsupportedOperationException("currently not support delete on elastic 2.0.0");
       }
       else if(request instanceof GetIndexRequest) {
           this.requestBuilder.getBuilder().execute( new GetIndexRequestRestListener(channel, (GetIndexRequest) request));
       }


	else {
		throw new Exception(String.format("Unsupported ActionRequest provided: %s", request.getClass().getName()));
	}
}
 
Example #9
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
ElasticsearchSchema(Map<String, Integer> esAddresses,
                    Map<String, String> esConfig, String esIndexName) {
    super();
    client = new ElasticsearchUtil().getEsClient(esAddresses, esConfig);
    if (client != null) {
        final String[] indices = client.admin().indices()
                .getIndex(new GetIndexRequest().indices(esIndexName))
                .actionGet().getIndices();
        if (indices.length == 1) {
            index = indices[0];
        } else {
            index = null;
        }
    } else {
        index = null;
    }

}
 
Example #10
Source File: ElasticSearchService.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean existIndex() {
    try {
        if (this.instance() == null) return false;
        GetIndexRequest request = new GetIndexRequest();
        request.indices(name);
        request.local(false);
        request.humanReadable(true);
        return client.indices().exists(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error(e.getMessage());
        return false;
    }
}
 
Example #11
Source File: ITElasticSearchClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private JsonObject getIndex(String indexName) throws IOException {
    indexName = client.formatIndexName(indexName);
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);

    Response response = getRestHighLevelClient().getLowLevelClient()
                                                .performRequest(HttpGet.METHOD_NAME, "/" + indexName);
    InputStreamReader reader = new InputStreamReader(response.getEntity().getContent());
    Gson gson = new Gson();
    return undoFormatIndexName(gson.fromJson(reader, JsonObject.class));
}
 
Example #12
Source File: ElasticIndexManager.java    From cicada with MIT License 5 votes vote down vote up
private List<String> getAllIndice() {
  final List<String> indice = new LinkedList<String>();
  final GetIndexResponse resp = client.admin().indices().getIndex(new GetIndexRequest()).actionGet();

  for (final String indexName : resp.indices()) {
    if (indexName.startsWith(props.getEsSpanIndexPrefix()) //
        || indexName.startsWith(props.getEsAnnotationIndexPrefix())) {
      indice.add(indexName);
    }
  }

  return indice;
}
 
Example #13
Source File: ElasticsearchRule.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    if (! client.indices().exists(request)) {
        CreateIndexRequest createReq = new CreateIndexRequest(indexName);
        byte[] settings = toByteArray(getClass().getClassLoader().getResourceAsStream(SETTINGS_RESOURCE_NAME));
        createReq.settings(new String(settings), JSON);
        byte[] mapping = toByteArray(getClass().getClassLoader().getResourceAsStream(MAPPING_RESOURCE_NAME));
        createReq.mapping("doc", new String(mapping), JSON);
        client.indices().create(createReq);
    }
}
 
Example #14
Source File: GetIndexRequestRestListener.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Override
public RestResponse buildResponse(GetIndexResponse getIndexResponse, XContentBuilder builder) throws Exception {
    GetIndexRequest.Feature[] features = getIndexRequest.features();
    String[] indices = getIndexResponse.indices();

    builder.startObject();
    for (String index : indices) {
        builder.startObject(index);
        for (GetIndexRequest.Feature feature : features) {
            switch (feature) {
                case ALIASES:
                    writeAliases(getIndexResponse.aliases().get(index), builder, channel.request());
                    break;
                case MAPPINGS:
                    writeMappings(getIndexResponse.mappings().get(index), builder, channel.request());
                    break;
                case SETTINGS:
                    writeSettings(getIndexResponse.settings().get(index), builder, channel.request());
                    break;
                default:
                    throw new IllegalStateException("feature [" + feature + "] is not valid");
            }
        }
        builder.endObject();

    }
    builder.endObject();

    return new BytesRestResponse(RestStatus.OK, builder);
}
 
Example #15
Source File: IndexServiceImpl.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> show(String indexName) throws IOException {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient()
            .indices().get(request, RequestOptions.DEFAULT);
    ImmutableOpenMap<String, MappingMetaData> mappOpenMap = getIndexResponse.getMappings().get(indexName);
    List<AliasMetaData> indexAliases = getIndexResponse.getAliases().get(indexName);

    String settingsStr = getIndexResponse.getSettings().get(indexName).toString();
    Object settingsObj = null;
    if (StrUtil.isNotEmpty(settingsStr)) {
        settingsObj = JSONObject.parse(settingsStr);
    }
    Map<String, Object> result = new HashMap<>(1);
    Map<String, Object> indexMap = new HashMap<>(3);
    Map<String, Object> mappMap = new HashMap<>(mappOpenMap.size());
    List<String> aliasesList = new ArrayList<>(indexAliases.size());
    indexMap.put("aliases", aliasesList);
    indexMap.put("settings", settingsObj);
    indexMap.put("mappings", mappMap);
    result.put(indexName, indexMap);
    //获取mappings数据
    for (ObjectCursor<String> key : mappOpenMap.keys()) {
        MappingMetaData data = mappOpenMap.get(key.value);
        Map<String, Object> dataMap = data.getSourceAsMap();
        mappMap.put(key.value, dataMap);
    }
    //获取aliases数据
    for (AliasMetaData aliases : indexAliases) {
        aliasesList.add(aliases.getAlias());
    }
    return result;
}
 
Example #16
Source File: FilterActionTest.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testMissingIndicesQuery() throws FoxtrotException {
    final ObjectMapper mapper = getMapper();
    List<Document> documents = TestUtils.getQueryDocumentsDifferentDate(mapper, new Date(2014 - 1900, 4, 1).getTime());
    documents.addAll(TestUtils.getQueryDocumentsDifferentDate(mapper, new Date(2014 - 1900, 4, 5).getTime()));
    getQueryStore().save(TestUtils.TEST_TABLE_NAME, documents);
    for(Document document : documents) {
        getElasticsearchConnection().getClient()
                .admin()
                .indices()
                .prepareRefresh(ElasticsearchUtils.getCurrentIndex(TestUtils.TEST_TABLE_NAME, document.getTimestamp()))
                .execute()
                .actionGet();
    }
    GetIndexResponse response = getElasticsearchConnection().getClient()
            .admin()
            .indices()
            .getIndex(new GetIndexRequest())
            .actionGet();
    // Find all indices returned for this table name.. (using regex to match)
    assertEquals(3, Arrays.stream(response.getIndices())
            .filter(index -> index.matches(".*-" + TestUtils.TEST_TABLE_NAME + "-.*"))
            .count());

    Query query = new Query();
    query.setLimit(documents.size());
    query.setTable(TestUtils.TEST_TABLE_NAME);
    BetweenFilter betweenFilter = new BetweenFilter();
    betweenFilter.setField("_timestamp");
    betweenFilter.setFrom(documents.get(0)
                                  .getTimestamp());
    betweenFilter.setTo(documents.get(documents.size() - 1)
                                .getTimestamp());
    betweenFilter.setTemporal(true);
    query.setFilters(Lists.<Filter>newArrayList(betweenFilter));
    QueryResponse actualResponse = QueryResponse.class.cast(getQueryExecutor().execute(query));
    assertEquals(documents.size(), actualResponse.getDocuments()
            .size());
}
 
Example #17
Source File: ElasticSearchRestHighImpl.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
/**
 * This method will do the health check of elastic search.
 *
 * @return boolean
 */
@Override
public Future<Boolean> healthCheck() {

  GetIndexRequest indexRequest =
      new GetIndexRequest().indices(ProjectUtil.EsType.user.getTypeName());
  Promise<Boolean> promise = Futures.promise();
  ActionListener<Boolean> listener =
      new ActionListener<Boolean>() {
        @Override
        public void onResponse(Boolean getResponse) {
          if (getResponse) {
            promise.success(getResponse);
          } else {
            promise.success(false);
          }
        }

        @Override
        public void onFailure(Exception e) {
          promise.failure(e);
          ProjectLogger.log(
              "ElasticSearchRestHighImpl:healthCheck: error " + e.getMessage(),
              LoggerEnum.INFO.name());
        }
      };
  ConnectionManager.getRestClient().indices().existsAsync(indexRequest, listener);

  return promise.future();
}
 
Example #18
Source File: Elasticsearch2Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    //----es scher error --
    Thread.currentThread().setName("getTable_001");
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example #19
Source File: ElasticsearchIndexInitializer.java    From staccato with Apache License 2.0 5 votes vote down vote up
public boolean indexExists(CollectionMetadata collection) throws Exception {
    String initialIndexName = collection.getId() + "-000001";

    if (client.indices().exists(new GetIndexRequest().indices(initialIndexName), RequestOptions.DEFAULT)) {
        log.info("Found existing index '" + initialIndexName + "' for collection '" + collection.getId() + "'");
        return true;
    }

    log.debug("No index '" + initialIndexName + "' matched for collection '" + collection.getId() + "'");
    return false;
}
 
Example #20
Source File: AbstractESTest.java    From elasticsearch-migration with Apache License 2.0 5 votes vote down vote up
@SneakyThrows
protected boolean checkIndexExists(String indexName) {
    final ActionFuture<GetIndexResponse> response = client.admin().indices().getIndex(new GetIndexRequest().indices(indexName));

    try {
        return response.get() != null;
    } catch (ExecutionException e) {
        return false;
    }
}
 
Example #21
Source File: EsUtil.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * 判断索引库是否存在
 *
 * @param index
 * @return
 * @throws IOException
 */
public static boolean exitsIndex(String index) throws IOException {
    try {
        GetIndexRequest getRequest = new GetIndexRequest();
        getRequest.indices(index);
        getRequest.local(false);
        getRequest.humanReadable(true);
        return client.indices().exists(getRequest, RequestOptions.DEFAULT);
    } finally {
        if (isAutoClose) {
            close();
        }
    }
}
 
Example #22
Source File: Elasticsearch2Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
private static GetIndexRequest createGetIndexRequest(String index)
{
    return new GetIndexRequest()
            .local(true)
            .indices(index)
            .features(GetIndexRequest.Feature.MAPPINGS)
            //lenient because we throw our own errors looking at the response e.g. if something was not resolved
            //also because this way security doesn't throw authorization exceptions but rather honours ignore_unavailable
            .indicesOptions(IndicesOptions.lenientExpandOpen());
}
 
Example #23
Source File: Elasticsearch6Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example #24
Source File: Elasticsearch6Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
private static GetIndexRequest createGetIndexRequest(String index)
{
    return new GetIndexRequest()
            .local(true)
            .indices(index)
            .features(GetIndexRequest.Feature.MAPPINGS)
            //lenient because we throw our own errors looking at the response e.g. if something was not resolved
            //also because this way security doesn't throw authorization exceptions but rather honours ignore_unavailable
            .indicesOptions(IndicesOptions.lenientExpandOpen());
}
 
Example #25
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example #26
Source File: Elasticsearch5Client.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
private static GetIndexRequest createGetIndexRequest(String index)
{
    return new GetIndexRequest()
            .local(true)
            .indices(index)
            .features(GetIndexRequest.Feature.MAPPINGS)
            //lenient because we throw our own errors looking at the response e.g. if something was not resolved
            //also because this way security doesn't throw authorization exceptions but rather honours ignore_unavailable
            .indicesOptions(IndicesOptions.lenientExpandOpen());
}
 
Example #27
Source File: ElasticClientHelper.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check if an index exists with the given name 
 * Note: this is an synchronous call
 *
 * @param indexName elastic index name / pattern
 * @return true if index exists
 */
public boolean indexExists(String indexName) {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    try {
        return client.indices().exists(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        logger.log(Level.WARNING, "Failed to query elastic", e);
        return false;
    }
}
 
Example #28
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<GetIndexResponse> getIndex(GetIndexRequest request) {
    return execute(GetIndexAction.INSTANCE, request);
}
 
Example #29
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * 创建索引
 * 
 * @throws IOException
 */
private static void createIndex() throws IOException {

	// 类型
	String type = "_doc";
	String index = "test1";
	// setting 的值
	Map<String, Object> setmapping = new HashMap<>();
	// 分区数、副本数、缓存刷新时间
	setmapping.put("number_of_shards", 10);
	setmapping.put("number_of_replicas", 1);
	setmapping.put("refresh_interval", "5s");
	Map<String, Object> keyword = new HashMap<>();
	//设置类型
	keyword.put("type", "keyword");
	Map<String, Object> lon = new HashMap<>();
	//设置类型
	lon.put("type", "long");
	Map<String, Object> date = new HashMap<>();
	//设置类型
	date.put("type", "date");
	date.put("format", "yyyy-MM-dd HH:mm:ss");

	Map<String, Object> jsonMap2 = new HashMap<>();
	Map<String, Object> properties = new HashMap<>();
	//设置字段message信息
	properties.put("uid", lon);
	properties.put("phone", lon);
	properties.put("msgcode", lon);
	properties.put("message", keyword);
	properties.put("sendtime", date);
	Map<String, Object> mapping = new HashMap<>();
	mapping.put("properties", properties);
	jsonMap2.put(type, mapping);

	GetIndexRequest getRequest = new GetIndexRequest();
	getRequest.indices(index);
	getRequest.types(type);
	getRequest.local(false);
	getRequest.humanReadable(true);
	boolean exists2 = client.indices().exists(getRequest, RequestOptions.DEFAULT);
	//如果存在就不创建了
	if(exists2) {
		System.out.println(index+"索引库已经存在!");
		return;
	}
	// 开始创建库
	CreateIndexRequest request = new CreateIndexRequest(index);
	try {
		// 加载数据类型
		request.settings(setmapping);
		//设置mapping参数
		request.mapping(type, jsonMap2);
		//设置别名
		request.alias(new Alias("pancm_alias"));
		CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
		boolean falg = createIndexResponse.isAcknowledged();
		if(falg){
			System.out.println("创建索引库:"+index+"成功!" );
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

}
 
Example #30
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * 创建索引
 *
 * @throws IOException
 */
private static void createIndex() throws IOException {

    // 类型
    String type = "_doc";
    String index = "student";
    // setting 的值
    Map<String, Object> setmapping = new HashMap<>();
    // 分区数、副本数、缓存刷新时间
    setmapping.put("number_of_shards", 10);
    setmapping.put("number_of_replicas", 1);
    setmapping.put("refresh_interval", "5s");
    Map<String, Object> keyword = new HashMap<>();
    //设置类型
    keyword.put("type", "keyword");
    Map<String, Object> lon = new HashMap<>();
    //设置类型
    lon.put("type", "long");
    Map<String, Object> date = new HashMap<>();
    //设置类型
    date.put("type", "date");
    date.put("format", "yyyy-MM-dd");

    Map<String, Object> date2 = new HashMap<>();
    //设置类型
    date2.put("type", "date");
    date2.put("format", "yyyy-MM-dd HH:mm:ss.SSS");
    Map<String, Object> jsonMap2 = new HashMap<>();
    Map<String, Object> properties = new HashMap<>();
    //设置字段message信息
    properties.put("uid", lon);
    properties.put("grade", lon);
    properties.put("class", lon);
    properties.put("age", lon);
    properties.put("name", keyword);
    properties.put("createtm", date);
    properties.put("updatetm", date2);
    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", properties);
    jsonMap2.put(type, mapping);

    GetIndexRequest getRequest = new GetIndexRequest();
    getRequest.indices(index);
    getRequest.types(type);
    getRequest.local(false);
    getRequest.humanReadable(true);
    boolean exists2 = client.indices().exists(getRequest, RequestOptions.DEFAULT);
    //如果存在就不创建了
    if(exists2) {
        System.out.println(index+"索引库已经存在!");
        return;
    }
    // 开始创建库
    CreateIndexRequest request = new CreateIndexRequest(index);
    try {
        // 加载数据类型
        request.settings(setmapping);
        //设置mapping参数
        request.mapping(type, jsonMap2);
        //设置别名
        request.alias(new Alias("pancm_alias"));
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        boolean falg = createIndexResponse.isAcknowledged();
        if(falg){
            System.out.println("创建索引库:"+index+"成功!" );
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}