1、创建Client
private TransportClient client; @BeforeEach public void test1() throws UnknownHostException { Settings settings = Settings.builder().put("cluster.name", "myes").build(); client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node01"),9300)).addTransportAddress(new TransportAddress(InetAddress.getByName("node02"),9300)); }
2、自己拼装json创建索引保存到myindex1索引库下面的article当中去
/** * 插入json格式的索引数据 */ @Test public void createIndex(){ String json = "{" + ""user":"kimchy"," + ""postDate":"2013-01-30"," + ""message":"travelying out Elasticsearch"" + "}"; IndexResponse indexResponse = client.prepareIndex("myindex1", "article", "1").setSource(json, XContentType.JSON).get(); client.close(); }
3、使用map创建索引
@Test public void index2() throws Exception { HashMap<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("name", "zhangsan"); jsonMap.put("sex", "1"); jsonMap.put("age", "18"); jsonMap.put("address", "bj"); IndexResponse indexResponse = client.prepareIndex("myindex1", "article", "2") .setSource(jsonMap) .get(); client.close(); }
4、XcontentBuilder实现创建索引
/** * 通过XContentBuilder来实现索引的创建 * @throws IOException */ @Test public void index3() throws IOException { IndexResponse indexResponse = client.prepareIndex("myindex1", "article", "3") .setSource(new XContentFactory().jsonBuilder() .startObject() .field("name", "lisi") .field("age", "18") .field("sex", "0") .field("address", "bj") .endObject()) .get(); client.close(); }
5、将对象转换为json格式字符串进行创建索引
/** * 将java对象转换为json格式字符串进行创建索引 */ @Test public void objToIndex(){ Person person = new Person(); person.setAge(18); person.setId(20); person.setName("张三丰"); person.setAddress("武当山"); person.setEmail("zhangsanfeng@163.com"); person.setPhone("18588888888"); person.setSex(1); String json = JSONObject.toJSONString(person); System.out.println(json); client.prepareIndex("myindex1","article","32").setSource(json,XContentType.JSON).get(); client.close(); }
6、批量创建索引
/** * 批量创建索引 * @throws IOException */ @Test public void index4() throws IOException { BulkRequestBuilder bulk = client.prepareBulk(); bulk.add(client.prepareIndex("myindex1", "article", "4") .setSource(new XContentFactory().jsonBuilder() .startObject() .field("name", "wangwu") .field("age", "18") .field("sex", "0") .field("address", "bj") .endObject())); bulk.add(client.prepareIndex("news", "article", "5") .setSource(new XContentFactory().jsonBuilder() .startObject() .field("name", "zhaoliu") .field("age", "18") .field("sex", "0") .field("address", "bj") .endObject())); BulkResponse bulkResponse = bulk.get(); System.out.println(bulkResponse); client.close(); }
2.更新索引
2.1根据系统给数据生成的id来进行更新索引
/** * 更新索引 * 根据数据id来进行更新索引 */ @Test public void updateIndex(){ Person guansheng = new Person(5, "宋江", 88, 0, "水泊梁山", "17666666666", "wusong@itcast.com","及时雨宋江"); client.prepareUpdate().setIndex("myindex1").setType("article").setId("8") .setDoc(JSONObject.toJSONString(guansheng),XContentType.JSON) .get(); client.close(); }
4、删除索引
4.1按照id进行删除
/** * 按照id进行删除数据 */ @Test public void deleteById(){ DeleteResponse deleteResponse = client.prepareDelete("myindex1", "article", "8").get(); client.close(); }
4.2删除整个索引库
/** * 删除索引 * 删除整个索引库 */ @Test public void deleteIndex(){ DeleteIndexResponse indexsearch = client.admin().indices().prepareDelete("myindex1").execute().actionGet(); client.close(); }
5、查询索引
5.1初始化一批数据到索引库中准备查询
/** * 初始化一批数据到索引库当中去准备做查询使用 * 注意这里初始化的时候,需要给我们的数据设置分词属性 * @throws Exception */ @Test public void createIndexBatch() throws Exception { Settings settings = Settings .builder() .put("cluster.name", "myes") //节点名称, 在es配置的时候设置 //自动发现我们其他的es的服务器 .put("client.transport.sniff", "true") .build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));//以本机作为节点 //创建映射 XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("properties") // .startObject("m_id").field("type","keyword").endObject() .startObject("id").field("type", "integer").endObject() .startObject("name").field("type", "text").field("analyzer", "ik_max_word").endObject() .startObject("age").field("type", "integer").endObject() .startObject("sex").field("type", "text").field("analyzer", "ik_max_word").endObject() .startObject("address").field("type", "text").field("analyzer", "ik_max_word").endObject() .startObject("phone").field("type", "text").endObject() .startObject("email").field("type", "text").endObject() .startObject("say").field("type", "text").field("analyzer", "ik_max_word").endObject() .endObject() .endObject(); //pois:索引名 cxyword:类型名(可以自己定义) PutMappingRequest putmap = Requests.putMappingRequest("indexsearch").type("mysearch").source(mapping); //创建索引 client.admin().indices().prepareCreate("indexsearch").execute().actionGet(); //为索引添加映射 client.admin().indices().putMapping(putmap).actionGet(); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); Person lujunyi = new Person(2, "玉麒麟卢俊义", 28, 1, "水泊梁山", "17666666666", "lujunyi@itcast.com","hello world今天天气还不错"); Person wuyong = new Person(3, "智多星吴用", 45, 1, "水泊梁山", "17666666666", "wuyong@itcast.com","行走四方,抱打不平"); Person gongsunsheng = new Person(4, "入云龙公孙胜", 30, 1, "水泊梁山", "17666666666", "gongsunsheng@itcast.com","走一个"); Person guansheng = new Person(5, "大刀关胜", 42, 1, "水泊梁山", "17666666666", "wusong@itcast.com","我的大刀已经饥渴难耐"); Person linchong = new Person(6, "豹子头林冲", 18, 1, "水泊梁山", "17666666666", "linchong@itcast.com","梁山好汉"); Person qinming = new Person(7, "霹雳火秦明", 28, 1, "水泊梁山", "17666666666", "qinming@itcast.com","不太了解"); Person huyanzhuo = new Person(8, "双鞭呼延灼", 25, 1, "水泊梁山", "17666666666", "huyanzhuo@itcast.com","不是很熟悉"); Person huarong = new Person(9, "小李广花荣", 50, 1, "水泊梁山", "17666666666", "huarong@itcast.com","打酱油的"); Person chaijin = new Person(10, "小旋风柴进", 32, 1, "水泊梁山", "17666666666", "chaijin@itcast.com","吓唬人的"); Person zhisheng = new Person(13, "花和尚鲁智深", 15, 1, "水泊梁山", "17666666666", "luzhisheng@itcast.com","倒拔杨垂柳"); Person wusong = new Person(14, "行者武松", 28, 1, "水泊梁山", "17666666666", "wusong@itcast.com","二营长。。。。。。"); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "1") .setSource(JSONObject.toJSONString(lujunyi), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "2") .setSource(JSONObject.toJSONString(wuyong), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "3") .setSource(JSONObject.toJSONString(gongsunsheng), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "4") .setSource(JSONObject.toJSONString(guansheng), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "5") .setSource(JSONObject.toJSONString(linchong), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "6") .setSource(JSONObject.toJSONString(qinming), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "7") .setSource(JSONObject.toJSONString(huyanzhuo), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "8") .setSource(JSONObject.toJSONString(huarong), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "9") .setSource(JSONObject.toJSONString(chaijin), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "10") .setSource(JSONObject.toJSONString(zhisheng), XContentType.JSON) ); bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "11") .setSource(JSONObject.toJSONString(wusong), XContentType.JSON) ); bulkRequestBuilder.get(); client.close(); }
5.2通过数据id使用prepareGet来查询索引
/** * 通过id来进行精确查询 */ @Test public void query1() { GetResponse documentFields = client.prepareGet("indexsearch", "mysearch", "11").get(); String index = documentFields.getIndex(); String type = documentFields.getType(); String id = documentFields.getId(); System.out.println(index); System.out.println(type); System.out.println(id); Map<String, Object> source = documentFields.getSource(); for (String s : source.keySet()) { System.out.println(source.get(s)); } }
5.3查询索引库当中的所有数据
/** * 查询所有数据 */ @Test public void queryAll() { SearchResponse searchResponse = client .prepareSearch("indexsearch") .setTypes("mysearch") .setQuery(new MatchAllQueryBuilder()) .get(); SearchHits searchHits = searchResponse.getHits(); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); } client.close(); }
5.4RangeQuery范围值查询
/** * 查找年龄18到28的人,包含18和28 */ @Test public void rangeQuery(){ SearchResponse searchResponse = client.prepareSearch("indexsearch") .setTypes("mysearch") .setQuery(new RangeQueryBuilder("age").gt(17).lt(29)) .get(); SearchHits hits = searchResponse.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsString()); } client.close(); }
5.5termQuery词条查询
/** * 词条查询 */ @Test public void termQuery(){ SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch") .setQuery(new TermQueryBuilder("say", "熟悉")) .get(); SearchHits hits = searchResponse.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsString()); } }
5.6fuzzyQuery模糊查询
/** * fuzzyQuery表示英文单词的最大可纠正次数,最大可以自动纠正两次 */ @Test public void fuzzyQuery(){ SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch") .setQuery(QueryBuilders.fuzzyQuery("say", "helOL").fuzziness(Fuzziness.TWO)) .get(); SearchHits hits = searchResponse.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsString()); } client.close(); }
5.7wildCardQuery通配符查询
/** * 模糊匹配查询有两种匹配符,分别是" * " 以及 " ? ", 用" * "来匹配任何字符,包括空字符串。用" ? "来匹配任意的单个字符 */ @Test public void wildCardQueryTest(){ SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch") .setQuery(QueryBuilders.wildcardQuery("say", "hel*")) .get(); SearchHits hits = searchResponse.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsString()); } client.close(); }
5.8boolQuery 多条件组合查询
/** * 多条件组合查询 boolQuery * 查询年龄是18到28范围内且性别是男性的,或者id范围在10到13范围内的 * */ @Test public void boolQueryTest(){ RangeQueryBuilder age = QueryBuilders.rangeQuery("age").gt(17).lt(29); TermQueryBuilder sex = QueryBuilders.termQuery("sex", "1"); RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gt("9").lt("15"); SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch") .setQuery( QueryBuilders.boolQuery().should(id) .should(QueryBuilders.boolQuery().must(sex).must(age))) .get(); SearchHits hits = searchResponse.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsString()); } client.close(); }
6.分页与高亮查询
/*分页查询 */ @Test public void getPageIndex(){ int pageSize = 5; int pageNum = 2; int startNum = (pageNum-1)*5; SearchResponse searchResponse = client.prepareSearch("indexsearch") .setTypes("mysearch") .setQuery(QueryBuilders.matchAllQuery()) .addSort("id",SortOrder.ASC) .setFrom(startNum) .setSize(pageSize) .get(); SearchHits hits = searchResponse.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsString()); } client.close(); }