Elasticsearch Index API

Index API

์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑ, ์‚ญ์ œ, ์กด์žฌ์—ฌ๋ถ€, status(open/close) ์—ฌ๋ถ€, shrink(์ƒค๋“œ ์ถ•์†Œ), split(์ƒค๋“œ ํ™•์žฅ), refresh(๋ฃจ์”ฌ์˜ flush๋กœ ์ด ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜์–ด์•ผ ์„ธ๊ทธ๋จผํŠธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋ผ ์ƒˆ๋กœ์šด doc์ด ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ์ปค๋„์—์„œ ์‹œ์Šคํ…œ ์บ์‹œ์— ํŒŒ์ผ๋งŒ ์ƒ์„ฑ/์ฃผ๊ธฐ์ ์œผ๋กœ ์ž๋™ ํ˜ธ์ถœ ๋œ๋‹ค.), flush(๋ฃจ์”ฌ์˜ commit์œผ๋กœ dixc์— ์˜์†์„ฑ์œผ๋กœ ์ €์žฅ์™ธ๋˜๋Š” ๋‹จ๊ณ„) , forcemerrge(๋ฃจ์”ฌ์˜ merge๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ํ•ฉ์น˜๋Š” ์ž‘์—…์œผ๋กœ memory,disk๋“ฑ์„ ์ ˆ์•ฝํ•˜๊ณ  ์ธ๋ฑ์‹ฑ์„ ์ƒˆ๋กœ ์ตœ์ ํ™”ํ•˜์—ฌ ์ˆ˜ํ–‰), rollover(active index๊ฐ€ ์˜ค๋ž˜๋ฌ๊ฑฐ๋‚˜ ํฌ๊ธฐ๊ฐ€ ์ปค๋ฒผ ๋นˆ ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ต์ฒด๊ฐ€ ํ•„์š”ํ• ๋•Œ ์‚ฌ์šฉ)

GetIndex

GetIndexRequest request = new GetIndexRequest("*");
GetIndexResponse response = client.indices().get(request,RequestOptions.DEFAULT);

client์˜ index์˜ ์ •๋ณด๋ฅผ client.indices().get()์„ ํ†ตํ•ด ์กฐํšŒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋•Œ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ GetIndexRequest()๋ฅผ ํ†ตํ•ด ํŠน์ • index๋ฅผ ๋ฆฌ์ •ํ•  ์ˆ˜ ์žˆ๊ณ  *์„ ์ฃผ์–ด์ง€๋ฉด ๋ชจ๋“  index๋ฅผ ์กฐํšŒํ•œ๋‹ค. (/_cat/indices ์™€ ๋™์ผ)

GetIndexResponse

๋‚ด๋ถ€ ํ•„๋“œ๋กœ mappings,aliases,settings,defaultSettings,dataStreams,indices ๊ฐ€ ์กด์žฌํ•˜๊ณ  getter๋ฅผ ํ†ตํ•ด ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

1. settings

{
    "posts_index" :{
        "index.analysis.analyzer.default_analyzer.tokenizer":"custom_nori",
        "index.analysis.analyzer.default_analyzer.type":"custom",
        "index.analysis.tokenizer.custom_nori.decompound_mode":"mixed",
        "index.analysis.tokenizer.custom_nori.type":"nori_tokenizer",
        "index.creation_date":"1628747766121",
        "index.number_of_replicas":"1",
        "index.number_of_shards":"1",
        "index.provided_name":"posts_index",
        "index.routing.allocation.include._tier_preference":"data_content",
        "index.uuid":"XiDS1v2oRw6EAumynNOvMw",
        "index.version.created":"7130499"
    }
}
response.getSettings().get("posts_index").get("index.uuid");
response.getSetting("posts_index","index.uuid");

index๋“ค์˜ id, ์ƒ์„ฑ์ผ์ž, analyzer๋“ฑ๊ณผ ๊ฐ™์€ ์„ธํŒ…์ •๋ณด๊ฐ€ Map์˜ ํ˜•ํƒœ๋กœ ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์— getSettings()์—์„œ get()์„ ์ด์ค‘์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ getSetting()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์„ธํŒ…์ •๋ณด๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

2. indices

์กฐํšŒํ•œ index๋“ค์˜ ์ด๋ฆ„๋“ค์„ String[]ํ˜•ํƒœ๋กœ ํฌํ•จํ•˜๊ณ ์žˆ๋Š” ํ•„๋“œ

response.getIndices();  //[posts_index]

3. aliases

response.getAliases()

{
  "posts_index": [
    {
      "posts_index_write": {}
    }
  ]
}

index์˜ ๋ณ„์นญ๋“ค์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ ์žˆ๋Š” ํ•„๋“œ๋กœ /_cat/aliases๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐํšŒํ•œ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค. ์ด๋„ Mapํ˜•ํƒœ๋กœ ์ €์žฅ์„ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ key๋กœ๋Š” index๋ช…, value๋กœ๋Š” aliases์˜ ๋ฉ”ํƒ€์ •๋ณดํ•„๋“œ list ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค.

+) /_cat/indices

์œ„์˜ ๋ช…๋ น์–ด๋‚˜ ?v๋ฅผ ์ถ”๊ฐ€ํ•ด column์„ ํฌํ•จํ•ด ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด lowClient๋ฅผ ์ด์šฉํ•ด ์กฐํšŒํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Response response = client.getLowLevelClient().performRequest(new Request("GET","/_cat/indices?v"));
String indicies = EntityUtils.toString(response.getEntity(), "UTF-8");

/* response ํ˜•์‹
health status index       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   posts_index XiDS1v2oRw6EAumynNOvMw   1   1        119           26    147.9kb        147.9kb
*/

String[] lines = indices.split("\n");
String[] columns = lines[0].split("\\s+");

JsonArray jsonArray = new JsonArray();
for(int i=1; i < lines.length; i++){
    JsonObject jsonObject = new JsonObject();

    String[] states = lines[1].split("\\s+");
    for(int j=0; j < columns.length; j++){
    jsonObject.addProperty(columns[j],states[j]);
    }
    jsonArray.add(jsonObject);
}
JsonObject jsonObject = new JsonObject();
jsonObject.add("indices",jsonArray);

return ResponseEntity.ok(jsonObject.toString());

์‘๋‹ต์ด ๋ฌธ์ž์—ด๋กœ ์œ„์™€ ๊ฐ™์ด ์˜ค๊ธฐ ๋•Œ๋ฌธ์— jsonํ˜•ํƒœ์˜ ๋‹ต์„ ์›ํ•˜๋ฉด ๋ณ„๋„๋กœ ํŒŒ์‹ฑ์„ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ EntityUtils์ด ์•„๋‹Œ getContent()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ InputStream์œผ๋กœ ํ•œ ์ค„ ์”ฉ ์ฝ์–ด ํŒŒ์‹ฑํ•ด์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

Last updated