博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch2.2之javaApi
阅读量:5949 次
发布时间:2019-06-19

本文共 6303 字,大约阅读时间需要 21 分钟。

hot3.png

前言:elasticsearch虽然自带rest接口,但是在真正使用过程中可能更多的是通过不同编程语言的客户端进行交互。

因为代码里有或多或少的注释,所以直接贴代码:

package elasticsearcha;import com.alibaba.fastjson.JSON;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import entity.User;import org.elasticsearch.action.delete.DeleteResponse;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.get.MultiGetItemResponse;import org.elasticsearch.action.get.MultiGetResponse;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.action.search.MultiSearchResponse;import org.elasticsearch.action.search.SearchRequestBuilder;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.action.search.SearchType;import org.elasticsearch.action.update.UpdateResponse;import org.elasticsearch.client.Client;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.SearchHit;import org.junit.Test;import java.net.InetAddress;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;/** * Created by wangnian on 2016/5/29. */public class EsTest {    Client client = getClient("192.168.1.55");    //创建客户端    public Client getClient(String host) {        try {            Client client = TransportClient.builder().build()                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), 9300));            return client;        } catch (Exception ex) {            System.out.println(ex);        }        return null;    }    /**     * 将对象通过jackson.databind转换成byte[]     * 注意一下date类型需要格式化处理  默认是 时间戳     *     * @return     */    public byte[] convertByteArray(Object obj) {        ObjectMapper mapper = new ObjectMapper(); // create once, reuse        try {            byte[] json = mapper.writeValueAsBytes(obj);            return json;        } catch (JsonProcessingException e) {            e.printStackTrace();        }        return null;    }    /**     * 将对象通过JSONtoString转换成JSON字符串     * 使用fastjson 格式化注解  在属性上加入 @JSONField(format="yyyy-MM-dd HH:mm:ss")     *     * @return     */    public String jsonStr(Object obj) {        return JSON.toJSONString(obj);    }    /**     * 增删改查     *     * @throws Exception     */    @Test    public void test() throws Exception {        Map
json = new HashMap
(); json.put("name", "wangnian11"); json.put("postDate", new Date()); json.put("message", "trying out Elasticsearch"); User user = new User(); user.setId(2); user.setName("更新第一条"); user.setPostDate(new Date()); user.setMessage("这是一个测试的数据"); IndexResponse response = client.prepareIndex("index", "user", user.getId().toString())//参数说明: 索引,类型 ,_id .setSource(jsonStr(user))//setSource可以传以上map string byte[] 几种方式 .get(); boolean created = response.isCreated(); System.out.println("创建一条记录:" + created); //删除_id为1的类型 DeleteResponse response2 = client.prepareDelete("index", "user", "2").get(); System.out.println("删除一条数据:" + response2.isFound()); //更新 UpdateResponse updateResponse = client.prepareUpdate("index", "user", "1").setDoc(jsonBuilder() .startObject() .field("name", "王念") .endObject()) .get(); System.out.println("更新一条数据:" + updateResponse.isCreated()); //获取_id为1的类型 GetResponse response1 = client.prepareGet("index", "user", "1").get(); response1.getSourceAsMap();//获取值转换成map System.out.println("查询一条数据:" + JSON.toJSON(response1.getSourceAsMap())); } @Test public void test2() { //查询多个id的值 MultiGetResponse multiGetItemResponses = client.prepareMultiGet() .add("index", "user", "1") .add("index", "user", "2", "3", "4") .get(); for (MultiGetItemResponse itemResponse : multiGetItemResponses) { GetResponse response = itemResponse.getResponse(); if (response.isExists()) { String json = response.getSourceAsString(); System.out.println(json); } } //搜索 SearchResponse response = client.prepareSearch("index")//可以同时搜索多个索引prepareSearch("index","index2") .setTypes("user")//可以同时搜索多个类型 .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.termQuery("name", "张三")) // Query .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter .setFrom(0).setSize(2).setExplain(true) .execute() .actionGet(); forSearchResponse(response); System.out.println("总共查询到有:" + response.getHits().getTotalHits()); //多查询结果 SearchRequestBuilder srb1 = client .prepareSearch().setQuery(QueryBuilders.queryStringQuery("张三")).setSize(1); SearchRequestBuilder srb2 = client .prepareSearch().setQuery(QueryBuilders.matchQuery("name", "张三")).setSize(1); MultiSearchResponse sr = client.prepareMultiSearch() .add(srb1) .add(srb2) .execute().actionGet(); long nbHits = 0; for (MultiSearchResponse.Item item : sr.getResponses()) { SearchResponse response1 = item.getResponse(); forSearchResponse(response1); nbHits += response1.getHits().getTotalHits(); } System.out.println("多查询总共查询到有:" + nbHits); } public void forSearchResponse(SearchResponse response) { for (SearchHit hit1 : response.getHits()) { Map
source1 = hit1.getSource(); if (!source1.isEmpty()) { for (Iterator
> it = source1.entrySet().iterator(); it.hasNext(); ) { Map.Entry
entry = it.next(); System.out.println(entry.getKey() + "=======" + entry.getValue()); } } } }}

博客地址:

转载于:https://my.oschina.net/wangnian/blog/684686

你可能感兴趣的文章
Hadoop数据操作系统YARN全解析
查看>>
Django 运行报错 ImportError: No module named 'PIL'
查看>>
修改数据库的兼容级别
查看>>
Windows下同时安装两个版本Jdk
查看>>
uoj#228. 基础数据结构练习题(线段树)
查看>>
JS键盘事件监听
查看>>
ios开发周期之--(向上,向下,四舍五入)取整
查看>>
加油!
查看>>
拦截导弹问题(动态规划)
查看>>
iOS 单元测试(Unit Test 和 UI Test)
查看>>
[linux小技巧]
查看>>
文件下载_中文乱码:"Content-disposition","attachment; filename=中文名
查看>>
HBase 笔记3
查看>>
2017.11.23 display fun --STM8
查看>>
深入学习jQuery选择器系列第八篇——过滤选择器之伪子元素选择器
查看>>
一个关于log4j的悲伤的故事
查看>>
PCA
查看>>
ajax上传文件
查看>>
java中通过绝对路径将图片存入数据库
查看>>
简要记录浮点型数据的二进制存储格式
查看>>