导入包
<dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> </dependencies>
2.Java操作
基本操作通过client客户端对象获得客户端然后通过preparIndex等方法传入index,type,id参数和Kibaan中的操作一样
public class EsTest { //创建客户端 public TransportClient createClient() throws UnknownHostException { return new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); } //添加方法 @Test public void testAddDoc() throws Exception{ TransportClient client = createClient(); //添加index,type,id IndexRequestBuilder indexRequestBuilder = client.prepareIndex("jiedada", "shuai", "1"); //准备数据 Map map = new HashMap(); map.put("id",1); map.put("name","jiedada"); map.put("age",20); map.put("sex",1); //传入数据并且提交数据 IndexResponse indexResponse = indexRequestBuilder.setSource(map).get(); System.out.println(indexResponse.getResult()); //关闭客户端 client.close(); } //修改方法 @Test public void testUpdateDoc() throws Exception{ TransportClient client = createClient(); //添加index,type,id UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("jiedada", "shuai", "1"); //准备数据 Map map = new HashMap(); map.put("id",1); map.put("name","jieshuai"); map.put("sex",1); //传入数据并且提交数据 UpdateResponse updateResponse = updateRequestBuilder.setDoc(map).get(); System.out.println(updateResponse.getResult()); //关闭客户端 client.close(); } //查询方法 @Test public void testFindDoc() throws Exception{ TransportClient client = createClient(); GetRequestBuilder getRequestBuilder = client.prepareGet("jiedada", "shuai", "1"); GetResponse getFields = getRequestBuilder.get(); System.out.println(getFields.getSource()); client.close(); } //删除方法方法 @Test public void testDelDoc() throws Exception{ TransportClient client = createClient(); client.prepareDelete("jiedada", "shuai", "1").get(); client.close(); } //批量操作 @Test public void testBulkDoc() throws Exception{ TransportClient client = createClient(); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); //获得添加对象 IndexRequestBuilder indexRequestBuilder = client.prepareIndex("jiedada", "shuai", "1"); //准备数据 Map map = new HashMap(); map.put("id",1); map.put("name","jiedada"); map.put("sex",1); indexRequestBuilder.setSource(map); //传入数据并且提交数据 //获得更新对象 UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("jiedada", "shuai", "1"); //准备数据 Map map1 = new HashMap(); map1.put("id",1); map1.put("name","jieshuai"); map1.put("sex",1); //传入数据并且提交数据 updateRequestBuilder.setDoc(map1); BulkResponse bulkItemResponses = bulkRequestBuilder.add(indexRequestBuilder).add(updateRequestBuilder).get(); //获得多个对象 BulkItemResponse[] items = bulkItemResponses.getItems(); for (BulkItemResponse item : items) { //判断是否保存成功,如果不成功 System.out.println(item.getFailure()); } client.close(); } //bool查询和过滤 @Test public void testBoolDoc() throws Exception{ TransportClient client = createClient(); //获得search对象 SearchRequestBuilder search = client.prepareSearch("jiedada"); /** * { "query": { "bool": { //booleanQuery 组合查询 "must": [ //查询 与(must) 或(should) 非(must not) { "match": {//标准查询(分词匹配) term:单词查询(部分词) "description": "hello java" } } ], "filter": { //过滤 "term": {"description": "hello world"} } } }, "from": 20, "size": 10, "_source": ["fullName", "age", "email"], "sort": [{"join_date": "desc"},{"age": "asc"}] } */ //通过工具类获得must BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); List<QueryBuilder> must = queryBuilder.must(); //匹配条件 must.add(QueryBuilders.matchQuery("name","jiedada")); //过滤条件 queryBuilder.filter(QueryBuilders.rangeQuery("age").gt(18).lt(25)); search.setQuery(queryBuilder); //设置排序 search.addSort("age", SortOrder.DESC); //分页 search.setFrom(0); search.setSize(5); //提交 SearchResponse searchResponse = search.get(); //命中的内容和 SearchHits hits = searchResponse.getHits(); System.out.println("命中条数+"+hits.getTotalHits()); for (SearchHit hit : hits) { System.out.println(hit.getSource()); } } }