elasticsearch java索引的增删改查
1.创建索引并插入数据 Map<String, Object> json = new HashMap<String, Object>(); json.put("user", "kimchy5"); json.put("postDate", new Date()); json.put("message", "trying out Elasticsearch"); //参数设置 //Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch").build(); //TransportClient client = TransportClient.builder().settings(settings).build(); Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); // 第一个参数:索引名;第二个参数:索引类型;第三个参数:索引ID(相同的id时修改数据,默认为随机字符串) IndexResponse indexResponse = client.prepareIndex("twitter", "json", "1").setSource(json).get(); System.out.println(indexResponse); // 不指定id会随机生成一个 //client.prepareIndex("twitter", "tweet").setSource(json); client.close(); 2.索引数据批量插入(插入效率高) // 批量插入数据,删除和修改也可以,不举例了 BulkRequestBuilder bulkRequest = client.prepareBulk(); IndexRequest request = client.prepareIndex("twitter", "tweet", "1").setSource(json).request(); IndexRequest request2 = client.prepareIndex("twitter", "tweet", "2").setSource(json2).request(); bulkRequest.add(request); bulkRequest.add(request2); bulkRequest.execute().actionGet(); 3.判断索引是否存在 IndicesExistsRequest inExistsRequest = new IndicesExistsRequest("twitter"); IndicesExistsResponse inExistsResponse = client.admin().indices().exists(inExistsRequest).actionGet(); System.out.println("索引twitter是否存在:"+inExistsResponse.isExists()); 4.删除索引 //删除索引twitter client.admin().indices().prepareDelete("twitter").execute().actionGet(); //删除索引中的某个文档(一条数据) client.prepareDelete("twitter", "tweet", "1"); 5.查询索引库 Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); // 第一个参数:要查找的内容 第二,三个参数:要查找的字段 QueryBuilder qb = QueryBuilders.multiMatchQuery("喧嚣", "content","message"); // twitter:索引名 tweet:索引类型 SearchResponse response = client.prepareSearch("twitter").setTypes("tweet").setQuery(qb) //设置查询类型 SearchType.QUERY_AND_FETCH:全部获取 .setSearchType(SearchType.DFS_QUERY_AND_FETCH).execute().actionGet(); SearchHits hits = response.getHits(); if (hits.totalHits() > 0) { for (SearchHit hit : hits) { System.out.println("score:" + hit.getScore() + ":\t" + hit.getSource());// .get("title") } } else { System.out.println("搜到0条结果"); } client.close(); 6.刷新索引 //刷新所有 client.admin().indices().prepareRefresh().get(); //刷新某一个 client.admin().indices().prepareRefresh("twitter").get(); //刷新某个类型 client.admin().indices().prepareRefresh("twitter","tweet").get(); 7.更新索引数据 //更新索引(根据索引,类型,id) UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("twitter"); updateRequest.type("tweet"); updateRequest.id("1"); updateRequest.doc(jsonBuilder().startObject().field("content", "如果我说爱我没有如果").endObject()); client.update(updateRequest).get(); 或 UpdateRequest updateRequest = new UpdateRequest("twitter", "tweet", "1") .doc(jsonBuilder().startObject().field("content", "如果我说爱我没有如果").endObject()); client.update(updateRequest).get(); 8.插入更新 //插入更新(如果不存在id为3的则插入content为IndexRequest的内容,如果存在则更新content为UpdateRequest的内容) IndexRequest indexRequest = new IndexRequest("twitter", "tweet", "3") .source(jsonBuilder().startObject() .field("content", "每当我迷失在黑夜里") .endObject()); UpdateRequest updateRequest2 = new UpdateRequest("twitter", "tweet", "3") .doc(jsonBuilder().startObject() .field("content", "请照亮我前行") .endObject()) .upsert(indexRequest); client.update(updateRequest2).get(); client.close();