반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[es] Java High Level REST Client 본문

ElasticStack/Elasticsearch

[es] Java High Level REST Client

닉의네임 2022. 1. 5. 13:41
반응형

7.15.0 이후로 안나온다는건가..

 

bulid.gradle 디펜던시 추가

def elasticsearchVersion = '7.12.1'

// https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client
compile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-high-level-client', version: elasticsearchVersion
// https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client
compile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-client', version: elasticsearchVersion
// https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
compile group: 'org.elasticsearch', name: 'elasticsearch', version: elasticsearchVersion

 

인덱스 생성

package com.bbongdoo.doo.apis;

import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;

public class IndexApi {

    public static CreateIndexRequest createIndex(String indexName) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 0)
        );

        XContentBuilder settingBuilder = XContentFactory.jsonBuilder()
                .startObject()
                    .field("number_of_shards", 3)
                    .field("number_of_replicas", 0)
                .startObject("analysis")
                .startObject("tokenizer")
                    .startObject("doo-nori-tokenizer")
                        .field("type", "nori_tokenizer")
                        .field("decompound_mode", "mixed")
                        .field("user_dictionary", "user_dictionary.txt")
                    .endObject()
                .endObject()
                .startObject("analyzer")
                    .startObject("jamo-analyzer")
                        .field("type", "custom")
                        .field("tokenizer", "standard")
                        .array("filter", new String[]{
                                        "doo-jamo"
                                }
                        )
                    .endObject()
                    .startObject("chosung-analyzer")
                        .field("type", "custom")
                        .field("tokenizer", "standard")
                        .array("filter", new String[]{
                                        "doo-chosung"
                                }
                        )
                    .endObject()
                    .startObject("doo-nori-analyzer")
                        .field("type", "custom")
                        .field("tokenizer", "doo-nori-tokenizer")
                            .array("filter", new String[]{
                                            "doo-nori-posfilter",
                                            "nori_readingform",
                                            "doo-synonym-filter",
                                            "doo-stop-filter"
                                    }
                            )
                        .endObject()
                    .endObject()
                        .startObject("filter")
                            .startObject("doo-nori-posfilter")
                                .field("type", "nori_part_of_speech")
                                    .array("stoptaags", new String[]{
                                                    "E", "IC", "J", "MAG", "MM", "NA", "NR", "SC",
                                                    "SE", "SF", "SH", "SL", "SN", "SP", "SSC", "SSO",
                                                    "SY", "UNA", "UNKNOWN", "VA", "VCN", "VCP", "VSV",
                                                    "VV", "VX", "XPN", "XR", "XSA", "XSN", "XSV"
                                            }
                                    )
                            .endObject()
                            .startObject("doo-synonym-filter")
                                .field("type", "synonym")
                                .field("synonyms_path", "synonymsFilter.txt")
                            .endObject()
                            .startObject("doo-stop-filter")
                                .field("type", "stop")
                                .field("stopwords_path", "stopFilter.txt")
                            .endObject()
                        .endObject()
                    .endObject()
                .endObject();
        request.settings(settingBuilder);

        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                builder.startObject("id");
                {
                    builder.field("type", "long");
                }
                builder.endObject();

                builder.startObject("name");
                {
                    builder.field("type", "text");
                    builder.startObject("fields");
                        builder.startObject("nori");
                                builder.field("type", "text");
                                builder.field("analyzer", "doo-nori-analyzer");
                            builder.endObject();
                            builder.startObject("jamo");
                                builder.field("type", "text");
                                builder.field("analyzer", "jamo-analyzer");
                            builder.endObject();
                            builder.startObject("chosung");
                                builder.field("type", "text");
                                builder.field("analyzer", "chosung-analyzer");
                            builder.endObject();
                    builder.endObject();
                }
                builder.endObject();

                builder.startObject("category");
                {
                    builder.field("type", "text");
                }
                builder.endObject();

                builder.startObject("category1");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("category2");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("category3");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("category4");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("category5");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("price");
                {
                    builder.field("type", "long");
                }
                builder.endObject();

                builder.startObject("brand");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("keyword");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("image");
                {
                    builder.field("type", "text");
                }
                builder.endObject();
                builder.startObject("popular");
                {
                    builder.field("type", "integer");
                    builder.field("null_value", "1");
                }
                builder.endObject();
                builder.startObject("weight");
                {
                    builder.field("type", "float");
                }
                builder.endObject();
                builder.startObject("image_vector");
                {
                    builder.field("type", "dense_vector");
                    builder.field("dims", 128);
                }
                builder.endObject();
                builder.startObject("type");
                {
                    builder.field("type", "keyword");
                }
                builder.endObject();

                builder.startObject("created_time");
                {
                    builder.field("type", "date");
                    builder.field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||strict_date_hour_minute_second_millis||strict_date_optional_time");
                }
                builder.endObject();
                builder.startObject("updated_time");
                {
                    builder.field("type", "date");
                    builder.field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||strict_date_hour_minute_second_millis||strict_date_optional_time");
                }
                builder.endObject();


            }
            builder.endObject();
        }
        builder.endObject();

        request.mapping(builder);
        return request;

    }
}

확인해보자

 

index metadata

{
   "version":7,
   "mapping_version":1,
   "settings_version":1,
   "aliases_version":2,
   "routing_num_shards":768,
   "state":"open",
   "settings":{
      "index":{
         "routing":{
            "allocation":{
               "include":{
                  "_tier_preference":"data_content"
               }
            }
         },
         "number_of_shards":"3",
         "provided_name":"shop-2022-01-05",
         "creation_date":"1641389393557",
         "analysis":{
            "filter":{
               "doo-stop-filter":{
                  "type":"stop",
                  "stopwords_path":"stopFilter.txt"
               },
               "doo-synonym-filter":{
                  "type":"synonym",
                  "synonyms_path":"synonymsFilter.txt"
               },
               "doo-nori-posfilter":{
                  "type":"nori_part_of_speech",
                  "stoptaags":[
                     "E",
                     "IC",
                     "J",
                     "MAG",
                     "MM",
                     "NA",
                     "NR",
                     "SC",
                     "SE",
                     "SF",
                     "SH",
                     "SL",
                     "SN",
                     "SP",
                     "SSC",
                     "SSO",
                     "SY",
                     "UNA",
                     "UNKNOWN",
                     "VA",
                     "VCN",
                     "VCP",
                     "VSV",
                     "VV",
                     "VX",
                     "XPN",
                     "XR",
                     "XSA",
                     "XSN",
                     "XSV"
                  ]
               }
            },
            "analyzer":{
               "doo-nori-analyzer":{
                  "filter":[
                     "doo-nori-posfilter",
                     "nori_readingform",
                     "doo-synonym-filter",
                     "doo-stop-filter"
                  ],
                  "type":"custom",
                  "tokenizer":"doo-nori-tokenizer"
               },
               "chosung-analyzer":{
                  "filter":[
                     "doo-chosung"
                  ],
                  "type":"custom",
                  "tokenizer":"standard"
               },
               "jamo-analyzer":{
                  "filter":[
                     "doo-jamo"
                  ],
                  "type":"custom",
                  "tokenizer":"standard"
               }
            },
            "tokenizer":{
               "doo-nori-tokenizer":{
                  "type":"nori_tokenizer",
                  "user_dictionary":"user_dictionary.txt",
                  "decompound_mode":"mixed"
               }
            }
         },
         "number_of_replicas":"0",
         "uuid":"KEZnaKs8TIWE61KyZcBQJw",
         "version":{
            "created":"7150199"
         }
      }
   },
   "mappings":{
      "_doc":{
         "properties":{
            "category2":{
               "type":"keyword"
            },
            "category3":{
               "type":"keyword"
            },
            "created_time":{
               "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||strict_date_hour_minute_second_millis||strict_date_optional_time",
               "type":"date"
            },
            "image":{
               "type":"text"
            },
            "updated_time":{
               "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||strict_date_hour_minute_second_millis||strict_date_optional_time",
               "type":"date"
            },
            "category4":{
               "type":"keyword"
            },
            "category5":{
               "type":"keyword"
            },
            "category1":{
               "type":"keyword"
            },
            "weight":{
               "type":"float"
            },
            "type":{
               "type":"keyword"
            },
            "price":{
               "type":"long"
            },
            "name":{
               "type":"text",
               "fields":{
                  "chosung":{
                     "analyzer":"chosung-analyzer",
                     "type":"text"
                  },
                  "jamo":{
                     "analyzer":"jamo-analyzer",
                     "type":"text"
                  },
                  "nori":{
                     "analyzer":"doo-nori-analyzer",
                     "type":"text"
                  }
               }
            },
            "id":{
               "type":"long"
            },
            "category":{
               "type":"text"
            },
            "keyword":{
               "type":"keyword"
            },
            "brand":{
               "type":"keyword"
            },
            "image_vector":{
               "dims":128,
               "type":"dense_vector"
            },
            "popular":{
               "null_value":1,
               "type":"integer"
            }
         }
      }
   },
   "aliases":[
      "shop"
   ],
   "primary_terms":{
      "0":1,
      "1":1,
      "2":1
   },
   "in_sync_allocations":{
      "0":[
         "hQV7y-lTSjGFjMTAFsz0MA"
      ],
      "1":[
         "hkT8Jz75QE2ZuRNyoahkWw"
      ],
      "2":[
         "_wBKMNVqSISoyfLxX8254g"
      ]
   },
   "rollover_info":{
      
   },
   "system":false,
   "timestamp_range":{
      "unknown":true
   }
}
GET shop/_doc/1
GET shop/_termvectors/1?fields=name
GET shop/_termvectors/1?fields=name.nori
GET shop/_termvectors/1?fields=name.jamo
GET shop/_termvectors/1?fields=name.chosung
반응형

'ElasticStack > Elasticsearch' 카테고리의 다른 글

[es] Bool Query  (0) 2022.01.10
[es] intervals query  (0) 2022.01.06
[es] 자동완성 2  (2) 2022.01.04
[es] 자동완성1  (0) 2022.01.04
[es] query score test  (0) 2021.12.26
Comments