1.书写pom.xml文件
<dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.37</version> </dependency> </dependencies>
2.创建一个文档
@Test //创建一个文档 public void test01() throws Exception { // 创建连接elasticsearch服务器对象 Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); // 描述json对象 XContentBuilder xContentBuilder = XContentFactory .jsonBuilder() .startObject() .field("id", 1) .field("content", "QQ音乐是腾讯公司推出的一款网络音乐服务产品,海量音乐在线试听、新歌热歌在线首发、歌词翻译、手机铃声下载、高品质无损音乐试听、海量无损曲库、正版音乐下载、空间") .field("title", "QQ音乐-千万正版音乐海量无损曲库新歌热歌天天畅听的高").endObject(); client.prepareIndex("jk1", "article", "1").setSource(xContentBuilder) .get(); client.close(); }
测试:
创建成功
检索数据(全文)
@Test//对全文进行搜索 public void test02() throws Exception{ Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); //搜索数据 SearchResponse response = client.prepareSearch("jk1").setTypes("article").setQuery(QueryBuilders.matchAllQuery()).get(); //获取命中次数 SearchHits hits = response.getHits(); System.out.println("命中的次数为:"+hits.getTotalHits()); for (Iterator iterator = hits.iterator(); iterator.hasNext();) { SearchHit searchHit = (SearchHit) iterator.next(); System.out.println(searchHit.getSourceAsString()); } }
检索数据(字段检索) 会对需要查询的词进行分词的
@Test //对所有字段进行分词查询 public void test03() throws Exception{ Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); //搜索数据 SearchResponse response = client.prepareSearch("jk1").setTypes("article").setQuery(QueryBuilders.queryStringQuery("QQ腾讯")).get(); //获取命中次数 SearchHits hits = response.getHits(); System.out.println("tes3t命中的次数为:"+hits.getTotalHits()); for (Iterator iterator = hits.iterator(); iterator.hasNext();) { SearchHit searchHit = (SearchHit) iterator.next(); System.out.println(searchHit.getSourceAsString()); } }
查询结果
检索数据(模糊查询) 不会对需要查询的词条进行重新分词
@Test //模糊查询 public void test04() throws Exception{ Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); //搜索数据 SearchResponse response = client.prepareSearch("jk1").setTypes("article").setQuery(QueryBuilders.wildcardQuery("content", "*QQ*")).get(); //获取命中次数 SearchHits hits = response.getHits(); System.out.println("命中的次数为:"+hits.getTotalHits()); for (Iterator iterator = hits.iterator(); iterator.hasNext();) { SearchHit searchHit = (SearchHit) iterator.next(); System.out.println(searchHit.getSourceAsString()); } }
查询结果:命中的次数为:0
创建索引
@Test // 创建索引 public void test01() throws IOException { Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); client.admin().indices().prepareCreate("jk2").get(); }
删除索引
@Test // 删除索引 public void test02() throws IOException { Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); client.admin().indices().prepareDelete("jk2").get(); }
创建映射
@Test // 映射文件 的操作 public void test03() throws Exception { Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); // 描述json对象 创建连接搜索服务器对象 XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() .startObject().startObject("article").startObject("properties") .startObject("id").field("type", "String") .field("store", "yes").endObject().startObject("title") .field("type", "string").field("store", "yes") .field("analyzer", "ik").endObject().startObject("content") .field("type", "string").field("store", "yes") .field("analyzer", "ik").endObject().endObject().endObject() .endObject(); PutMappingRequest mappingRequest = Requests.putMappingRequest("jk2") .type("article").source(xContentBuilder); client.admin().indices().putMapping(mappingRequest).get(); // 关闭连接 client.close(); }
文档的操作
@Test // 映射文件 的操作 public void test04() throws Exception { Client client = TransportClient .builder() .build() .addTransportAddress( new InetSocketTransportAddress(InetAddress .getByName("127.0.0.1"), 9300)); // 描述json 数据 /* * {id:xxx, title:xxx, content:xxx} */ Article article = new Article(); article.setId("2"); article.setTitle("搜索工作其实很henbu快乐"); article.setContent("我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。"); String jsonString = JSON.toJSONString(article); // 建立文档 client.prepareIndex("jk2", "article", article.getId().toString()) .setSource(jsonString).get(); //修改文档 // client.prepareUpdate("jk2", "article", article.getId().toString()).setDoc(jsonString).get(); // client.update(new UpdateRequest("jk2", "article", article.getId().toString()).doc(jsonString)).get(); //删除文档 // client.prepareDelete("jk2", "article", article.getId().toString()).get(); // client.delete(new DeleteRequest("jk2", "article", article.getId().toString())).get(); // 关闭连接 client.close(); }
原文地址:https://www.cnblogs.com/fjkgrbk/p/elasticsearch_java.html
时间: 2024-10-28 07:28:22