• 谷粒商城学习—— P126 springboot 整合es测试


    https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-requests.html#java-rest-low-usage-request-options

    https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html

    package com.atguigu.gulimall.search.config;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * 1、导入依赖
     * 2、编写配置,给容器中注入一个RestHighLevelClient
     * 3、参照API https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/index.html
     */
    @Configuration
    public class GulimallElasticSearchConfig {
        public static final RequestOptions COMMON_OPTIONS;
        static {
            RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
    //        builder.addHeader("Authorization", "Bearer " + TOKEN);
    //        builder.setHttpAsyncResponseConsumerFactory(
    //                new HttpAsyncResponseConsumerFactory
    //                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
            COMMON_OPTIONS = builder.build();
        }
    
        @Bean
        public RestHighLevelClient esRestClient(){
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("192.168.56.10", 9200, "http")));
            return client;
        }
    }
    package com.atguigu.gulimall.search;
    
    import com.alibaba.fastjson.JSON;
    import com.atguigu.gulimall.search.config.GulimallElasticSearchConfig;
    import lombok.Data;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    import java.io.IOException;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class GulimallSearchApplicationTests {
        @Resource private RestHighLevelClient client;
    
        /**
         * 测试存储数据到es
         */
        @Test
        public void indexData() throws IOException {
            IndexRequest indexRequest = new IndexRequest("users");//向users下存数据
            indexRequest.id("1");//不设会自动生成
    //        indexRequest.source("userName", "zhangsan","age", 18,"gender", "男");//数据内容形式,k-v形式
            User user= new User();
            user.setUserName("zhangsan");
            user.setAge(18);
            user.setGender("男");
            String jsonString = JSON.toJSONString(user);
            indexRequest.source(jsonString, XContentType.JSON);//数据内容形式,json形式
            //执行操作。索引数据不存在数据就生成,存在就更新
            IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
            System.out.println(index);
        }
        @Data
        static
        class User{
            private String userName;
            private Integer age;
            private String gender;
        }
        @Test
        public void contextLoads() {
            System.out.println(client);
        }
    }

    结果查询:

    get users/_search

    结果:

    {
      "took" : 393,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "users",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "age" : 18,
              "gender" : "男",
              "userName" : "zhangsan"
            }
          }
        ]
      }
    }
  • 相关阅读:
    java集合归纳
    判断回文数
    29:四则运算计算表达式的值
    getOutString 输出弹出字符串
    两个字符串中不同元素的个数
    字符串各个字符ASCII值加5
    23:一个整数的二进制表示中有多少个1
    Java进程间通信
    转 双重检查锁定与延迟初始化
    Key-Value键值存储原理初识(NOSQL)
  • 原文地址:https://www.cnblogs.com/yanan7890/p/15626877.html
Copyright © 2020-2023  润新知