首页 技术 正文
技术 2022年11月16日
0 收藏 590 点赞 4,283 浏览 4996 个字

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();}

  测试:

elasticsearch之入门hello(java)一

创建成功

检索数据(全文)

@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());
}
}

  elasticsearch之入门hello(java)一

检索数据(字段检索)  会对需要查询的词进行分词的

@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());}
}

 查询结果

elasticsearch之入门hello(java)一

 检索数据(模糊查询)  不会对需要查询的词条进行重新分词

@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();
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,299