Elasticsearch 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