【ElasticSearch】索引(添加)

1、REST API

文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/indices-create-index.html

ignore_above 默认值 256

settings 中的index可以去掉

"settings": {
  "number_of_shards": 1,
  "number_of_replicas": 1
}

kibana

PUT /myindex
{
  "settings": {
    "index": {
      "number_of_shards": 3,
      "number_of_replicas": 3
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "title": {
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      }
    }
  },
  "aliases": {
    "myindex_alias": {}
  }
}

curl

curl -XPUT http://localhost:9200/myindex/_mapping -H 'Content-Type:application/json' -d '
{
  "settings": {
    "number_of_shards": 3,
     "number_of_replicas": 3
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "title": {
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      }
    }
  },
  "aliases": {
    "myindex_alias": {}
  }
}
'

 

2、Java REST Client

文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.13/java-rest-high-create-index.html

 

CreateIndexRequest request = new CreateIndexRequest("myindex");
// 设置
Settings.Builder settings = Settings.builder()
    .put("index.number_of_shards", 3)
    .put("index.number_of_replicas", 3);
request.settings(elasticsearchIndexRequest.getSettings());
// 别名
request.alias(new Alias("myindex_alias"));
// 映射
Map<String, Object> name = new HashMap<>();
name.put("type", "keyword");
Map<String, Object> age = new HashMap<>();
age.put("type", "integer");
Map<String, Object> raw = new HashMap<>();
raw.put("type", "keyword");
Map<String, Object> fields = new HashMap<>();
fields.put("raw", raw);
Map<String, Object> title = new HashMap<>();
title.put("type", "text");
title.put("fields", "fields");
Map<String, Object> properties = new HashMap<>();
properties.put("name", name);
properties.put("age", age);
properties.put("title", title);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
// 超时时间
request.setTimeout(TimeValue.timeValueMinutes(2));
request.setMasterTimeout(TimeValue.timeValueMinutes(1));
// 请求
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
if (createIndexResponse.isShardsAcknowledged()) {
    return true;
} else {
    return false;
}

 

posted @ 2023-06-17 16:17  翠微  阅读(37)  评论(0编辑  收藏  举报