elasticsearch建立索引库

留胡子的饼干 / 22 /

ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。
https://ckai.xyz

1、导入依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

修改elasticsearch版本

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

2、使用elasticsearch
(1)配置类
初始化RestHighLevelClient

@Bean
public RestHighLevelClient client(){
    log.warn("创建Es的Rest客户端");
    return new RestHighLevelClient(RestClient.builder(HttpHost.create(EsConstant.ES_URL)));
}

(2)常量类

public interface EsConstant {

String ES_URL = "http://192.168.138.100:9200";

String HOTEL_INDEX = "hotel";

String HOTEL_MAPPING = "{\n" +
        "  \"mappings\": {\n" +
        "    \"properties\": {\n" +
        "      \"id\":{\n" +
        "        \"type\": \"keyword\"\n" +
        "      },\n" +
        "      \"name\":{\n" +
        "        \"type\":\"text\",\n" +
        "        \"analyzer\": \"ik_smart\",\n" +
        "        \"copy_to\": \"all\"\n" +
        "      },\n" +
        "      \"address\":{\n" +
        "        \"type\": \"text\",\n" +
        "        \"analyzer\": \"ik_smart\",\n" +
        "        \"copy_to\": \"all\"\n" +
        "      },\n" +
        "      \"price\":{\n" +
        "        \"type\": \"integer\"\n" +
        "      },\n" +
        "      \"score\":{\n" +
        "        \"type\": \"integer\"\n" +
        "      },\n" +
        "      \"brand\":{\n" +
        "        \"type\": \"keyword\",\n" +
        "        \"copy_to\": \"all\"\n" +
        "      },\n" +
        "      \"city\":{\n" +
        "        \"type\": \"keyword\",\n" +
        "        \"copy_to\": \"all\"\n" +
        "      },\n" +
        "      \"starName\":{\n" +
        "        \"type\": \"keyword\"\n" +
        "      },\n" +
        "      \"business\":{\n" +
        "        \"type\": \"text\",\n" +
        "        \"analyzer\": \"ik_smart\",\n" +
        "        \"copy_to\": \"all\"\n" +
        "      },\n" +
        "      \"location\":{\n" +
        "        \"type\": \"geo_point\"\n" +
        "      },\n" +
        "      \"pic\":{\n" +
        "        \"type\": \"keyword\",\n" +
        "        \"index\": false\n" +
        "      },\n" +
        "      \"all\":{\n" +
        "        \"type\": \"text\",\n" +
        "        \"analyzer\": \"ik_smart\"\n" +
        "      }\n" +
        "    }\n" +
        "  }\n" +
        "}";
}

(3)测试类

@SpringBootTest
class HotelDemoApplicationTests {

@Autowired
RestHighLevelClient client;

/**
 *删除索引
 * @throws Exception
 */
@Test
public void deleteTest() throws Exception {
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(EsConstant.HOTEL_INDEX);
    AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
    boolean ack = response.isAcknowledged();
    System.out.println(ack);
}

/**
 * 查询索引
 * @throws Exception
 */
@Test
public void getIndexTest() throws Exception {
    GetIndexRequest GetIndexRequest = new GetIndexRequest(EsConstant.HOTEL_INDEX);
    GetIndexResponse response = client.indices().get(GetIndexRequest, RequestOptions.DEFAULT);
    Map<String, MappingMetadata> map = response.getMappings();
    System.out.println(map);
}

/**
 * 创建索引
 * @throws Exception
 */
@Test
public void createIndexTest() throws Exception {
    //执行创建索引的对象,指定索引名
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(EsConstant.HOTEL_INDEX);
    //指定映射
    createIndexRequest.source(EsConstant.HOTEL_MAPPING, XContentType.JSON);
    //创建索引
    CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    String index = response.index();
    boolean ack = response.isAcknowledged();
    System.out.println(index + ":" + ack);
}

@Test
void contextLoads() throws IOException {
    client.close();
}

}

elasticsearch建立索引库
作者
留胡子的饼干
许可协议
CC BY 4.0
发布于
2023-09-16
修改于
2024-04-06
Bonnie image
尚未登录