wenwentana 2020-04-08
<!--刚开始少这个包创建索引失败 Validation Failed: 1: type is missing; 官方文档并没有给这个提示--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.1.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.1.0</version> </dependency>
/** * 连接ES * @return */ public RestHighLevelClient start() { RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.100.151", 9201, "http"), new HttpHost("192.168.100.151", 9202, "http"), new HttpHost("192.168.100.151", 9203, "http"))); return restHighLevelClient; } /** * 操作所用到的实体类 */ @Data class Article{ private long id; private String title; public Article(long id, String title) { this.id = id; this.title = title; } }
public void createIndex(RestHighLevelClient client) { //索引名称 CreateIndexRequest request = new CreateIndexRequest("hello"); //分片副本 request.settings(Settings.builder().put("index.number_of_shards", 5) .put("index.number_of_replicas", 1)); //内容 Map <String,Object> id = new HashMap <>(); id.put("type","text"); id.put("store",true); Map <String,Object> title = new HashMap <>(); title.put("type","text"); title.put("store",true); title.put("index",true); title.put("analyzer", "standard"); Map <String,Object> properties = new HashMap <>(); properties.put("id",id); properties.put("title",title); Map <String,Object> mapping = new HashMap <>(); mapping.put("properties",properties); request.mapping(mapping); try { CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(response.toString()); } catch (IOException e) { e.printStackTrace(); } }
public void getIndex(RestHighLevelClient client) throws IOException { //索引名称 GetIndexRequest request = new GetIndexRequest("hello"); try { boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } catch (IOException e) { e.printStackTrace(); } }
public void delIndex(RestHighLevelClient client){ DeleteIndexRequest request = new DeleteIndexRequest("hello"); try { AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.toString()); } catch (IOException e) { e.printStackTrace(); } }
public void createDocument(RestHighLevelClient client){ //索引名称 IndexRequest indexRequest = new IndexRequest("hello"); ObjectMapper mapper = new ObjectMapper(); Article article = new Article(3L, "web前端"); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(article); //可以设置文章ID indexRequest.id("5"); indexRequest.source(json, XContentType.JSON); IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT); } catch (Exception e) { e.printStackTrace(); } }
public void getDocument(RestHighLevelClient client){ GetRequest getRequest = new GetRequest("hello", "1"); GetResponse documentFields = null; try { documentFields = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(documentFields.toString()); } catch (IOException e) { e.printStackTrace(); } }
public void updateDocument(RestHighLevelClient client){ UpdateRequest updateRequest = new UpdateRequest("hello", "1"); Article article = new Article(2L, "java入门到放弃"); ObjectMapper mapper = new ObjectMapper(); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(article); IndexRequest indexRequest = new IndexRequest("hello"); indexRequest.source(json, XContentType.JSON); updateRequest.doc(indexRequest); UpdateResponse updateResponse = client.update( updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse); } catch (Exception e) { e.printStackTrace(); } }
public void delDocument(RestHighLevelClient client){ DeleteRequest request = new DeleteRequest("hello", "1"); try { DeleteResponse deleteResponse = client.delete( request, RequestOptions.DEFAULT); System.out.println(deleteResponse); } catch (IOException e) { e.printStackTrace(); } }
public void searchDocument(RestHighLevelClient client){ SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null; try { searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(searchResponse.toString()); } catch (IOException e) { e.printStackTrace(); } }
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。