ETJava Beta | Java    注册   登录
  • elasticsearch 获取数据

    发表于 2024-04-07 21:40:59     阅读(190)     博客类别:Elasticsearch

    elasticsearch 获取数据

    ElasticSearch提供了根据索引名称,类别,文档ID来获取数据

    案例

    package com.et.connection;
    
    import com.google.gson.JsonObject;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.TransportAddress;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @Author: ETJAVA
     * @CreateTime: 2024-04-07  21:26
     * @Description: TODO 读取索引
     * @Version: 1.0
     */
    public class SearchIndex {
    
        private static String host="192.168.199.126";
        private static int port=9300; // 程序连接的端口
    
        public static void main(String[] args) throws UnknownHostException {
    
            TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(SearchIndex.host), SearchIndex.port));
            // 创建索引 返回索引相关信息
            Map<String, Object> map = createIndex(client);
    
    
            // 根据索引名称+索引类型+索引ID查询索引
            getIndex(client,String.valueOf(map.get("indexName")),String.valueOf(map.get("indexType")),String.valueOf(map.get("indexId")));
    
    
        }
    
        /**
         * 创建索引
         * @param client
         * @return 返回索引ID,名称,类型
         */
        private static Map<String,Object> createIndex(TransportClient client){
            Map<String,Object> map = new HashMap<>();
            // 定义要添加的数据
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("name","Java");
            jsonObject.addProperty("publishDate","2001-01-01");
            jsonObject.addProperty("price",99.98);
    
            IndexResponse indexResponse = client.prepareIndex("book", "java") // 定义索引的位置和id
                    .setSource(jsonObject.toString(), XContentType.JSON) // 添加值
                    .get();// 获取返回值
    
            map.put("indexName",indexResponse.getIndex());
            map.put("indexType",indexResponse.getType());
            map.put("indexId",indexResponse.getId());
            return map;
        }
    
        /**
         * 获取索引
         * @param client 连接elasticsearch的客户端对象
         * @param indexName 索引名称
         * @param indexType 索引的类型
         * @param docId 索引id
         */
        private static void getIndex(TransportClient client,String indexName,String indexType,String docId){
            GetResponse getResponse = client.prepareGet(indexName, indexType, docId).get();
            System.out.println(getResponse.getSourceAsString());// 返回json格式数据
            System.out.println(getResponse.getSource());// 返回Map格式数据
            System.out.println(new String(getResponse.getSourceAsBytes()));// 返回byte数组
        }
    }