一,创建es的连接 public TransportClient transportClient(){ Settings settings = Settings.builder() .put("cluster.name", "elasticsearch") .build(); TransportClient client = new PreBuiltTransportClient(settings); TransportAddress transportAddress = null; try { transportAddress = new TransportAddress(InetAddress.getByName(host), Integer.valueOf(port)); } catch (UnknownHostException e) { e.printStackTrace(); } client.addTransportAddress(transportAddress); return client; }
二,java es api的添加操作
transportClient.prepareIndex("search_index", "doc").setSource(json, XContentType.JSON).get();
三,java es api的修改操作
transportClient.prepareUpdate("search_index", "doc","gug4u3EBSHOgAqyn-5aI").setSource(json, XContentType.JSON).get();
四,java es api的删除操作
transportClient.prepareDelete("search_index", "doc", "gug4u3EBSHOgAqyn-5aI").get();
五,查询QueryBuilders
BoolQueryBuilder queryBuilder= QueryBuilders.boolQuery();
1,matchAllQuery匹配所有
queryBuilder.matchAllQuery();
2,termQuery精准匹配,大小写敏感且不支持
queryBuilder.termQuery("key", value) 一次匹配一个值,完全匹配
queryBuilder.termsQuery("key", obj1, obj2..) 一次匹配多个值
3,matchPhraseQuery对中文精确匹配
queryBuilder.matchPhraseQuery("key", value)
4,matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
queryBuilder.matchQuery(key, value);
5,multiMatchQuery("text", "field1", "field2"..); 匹配多个字段, field有通配符忒行
queryBuilder.multiMatchQuery(value, key1, key2, key3);
6,组合查询
* must: AND * mustNot: NOT * should:: OR
queryBuilder.must(QueryBuilders.termQuery("user", "kimchy")) .mustNot(QueryBuilders.termQuery("message", "nihao")) .should(QueryBuilders.termQuery("gender", "male"));
7,