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