Java使用JestClient操作ElasticSearch

个人使用Java操作Elasticsearch的记录,综合网络上很多的片段,自己进行修改后的,亲测可以使用,故上传做个备份。

Java操作代码:

package cn.xgs.JestClient;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.core.Suggest;
import io.searchbox.core.SuggestResult;
import io.searchbox.core.Update;

import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.xgs.entity.CsdnBlog;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.cluster.Health;
import io.searchbox.cluster.NodesInfo;
import io.searchbox.cluster.NodesStats;
import io.searchbox.core.Bulk;
import io.searchbox.core.Count;
import io.searchbox.core.CountResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.indices.ClearCache;
import io.searchbox.indices.CloseIndex;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.Flush;
import io.searchbox.indices.IndicesExists;
import io.searchbox.indices.Optimize;
import io.searchbox.indices.mapping.PutMapping;

/**
 * Elasticserach jestClient示例
 * @author zshuai
 *
 */

public class Jest {
	private static JestClient jestClient;
    private static String indexName = "article";  
    private static String typeName = "content"; 

    
    @Before
    public void getClient() throws Exception{
    	System.out.println("连接");
		JestClientFactory factory = new JestClientFactory();  
        factory.setHttpClientConfig(new HttpClientConfig  
                               .Builder("http://192.168.189.138:9200")  
                               .gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'hh:mm:ss").create())  
                               .connTimeout(1500)  
                               .readTimeout(3000)  
                               .multiThreaded(true)  
                               .build());
        jestClient=factory.getObject();
    }
  
    @After
    public void tearDown() throws Exception {  
        closeJestClient(jestClient);  
    }  

      
    /**  
     * 	关闭JestClient客户端  
     * @param jestClient  
     * @throws Exception  
     */  
    public void closeJestClient(JestClient jestClient) throws Exception {  
          
        if (jestClient != null) { 
        	System.out.println("关闭");
          jestClient.shutdownClient();  
        }  
    }
    
    /**
     * ---------------------------测试--------------------------
     */
    
    /**
     *	 创建索引
     * @throws Exception
     */
    @Test  
    public void createIndex() throws Exception {
    	
        JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    	System.out.println(jr.isSucceeded());  
    }  
  
    
    /**
     * Put映射 
     * @throws Exception
     */
    @Test  
    public void createIndexMapping() throws Exception {  
    	  
        String source = "{\"" + typeName + "\":{\"properties\":{"  
                + "\"author\":{\"type\":\"string\",\"index\":\"not_analyzed\"}"  
                + ",\"title\":{\"type\":\"string\"}"
                + ",\"content\":{\"type\":\"string\"}"
                + ",\"price\":{\"type\":\"string\"}"
                + ",\"view\":{\"type\":\"string\"}"
                + ",\"tag\":{\"type\":\"string\"}"
                + ",\"date\":{\"type\":\"date\",\"format\":\"yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"}"  
                + "}}}";  
        System.out.println(source);  
        
        PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
        JestResult jr = jestClient.execute(putMapping);  
        System.out.println(jr.isSucceeded());  
    }
    
    /**
     *	 插入多个
     * @throws IOException 
     */
    @Test
    public void insertMultiple() throws IOException{
       	CsdnBlog csdnBlog1=new CsdnBlog();
    	csdnBlog1.setAuthor("AAAA");
    	csdnBlog1.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
    	csdnBlog1.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
    	csdnBlog1.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
    	csdnBlog1.setView("100");
    	csdnBlog1.setTag("JAVA,ANDROID,C++,LINUX");
    	
    	
    	CsdnBlog csdnBlog2=new CsdnBlog();
    	csdnBlog2.setAuthor("BBBB");
    	csdnBlog2.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
    	csdnBlog2.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
    	csdnBlog2.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
    	csdnBlog2.setView("200");
    	csdnBlog2.setTag("JAVA,ANDROID,C++,LINUX");
    	
    	
    	CsdnBlog csdnBlog3=new CsdnBlog();
    	csdnBlog3.setAuthor("CCCC");
    	csdnBlog3.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
    	csdnBlog3.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
    	csdnBlog3.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
    	csdnBlog3.setView("300");
    	csdnBlog3.setTag("JAVA,ANDROID,C++,LINUX");
    	
    	CsdnBlog csdnBlog4=new CsdnBlog();
    	csdnBlog4.setAuthor("CCCC");
    	csdnBlog4.setTitile("中国获租巴基斯坦瓜达尔港2000亩土地 为期43年");
    	csdnBlog4.setContent("据了解,瓜达尔港务局于今年6月完成了1500亩土地的征收工作,另外500亩的征收工作也将很快完成");
    	csdnBlog4.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()));
    	
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值