ElasticSearch Java Api(二) -检索索引库
来源:互联网 发布:天猫魔盘连接不上网络 编辑:程序博客网 时间:2024/06/04 17:43
上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索。
一、准备数据
- 1
- 2
- 3
- 4
- 5
- 6
- 7
二、查询
一次查询可分为下面四个步骤:
1.创建连接ElasticSearch服务的client.
索引在ElasticSearch服务器上,进行索引的查询首先要和服务器创建连接,这是第一步。Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
2.创建QueryBuilder.
QueryBuilder可以设置单个字段的查询,也可以设置多个字段的查询.
e.g.1: 查询title字段中包含hibernate
关键字的文档:QueryBuilder qb1 = termQuery("title", "hibernate");
e.g.2: 查询title字段或content字段中包含
git
关键字的文档:QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
3.执行查询
通过client设置查询的index、type、query.返回一个SearchResponse对象:SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute() .actionGet();
4.处理查询结果
SearchResponse对象的getHits()方法获取查询结果,返回一个SearchHits的集合,遍历集合获取查询的文档信息:SearchHits hits = response.getHits();
三、java代码实现
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
查询结果:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
阅读全文
0 0
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api -删除索引
- Elasticsearch Java API之清空索引
- elasticsearch清空索引缓存 java api
- ElasticSearch Java Api(四) -删除索引
- Elasticsearch java api(五) Bulk批量索引
- [ElasticSearch]Java API 之 索引管理
- ElasticSearch Java Api(一) -创建索引
- ElasticSearch Java Api(四) -删除索引
- Elasticsearch java api(五) Bulk批量索引
- Elasticsearch java api(五) Bulk批量索引
- 二叉树
- ElasticSearch Java Api(一) -创建索引
- Magic Powder
- 学生成绩统计
- Android构建boot.img
- ElasticSearch Java Api(二) -检索索引库
- Shrio RBAC框架
- js---js中数组遍历方法forEach与map()有什么区别?
- 6.13用数组与if语句来计算数之和
- java项目jsp中如何引用本地路径图片
- 截取图片接口的路径
- 时钟的实时显示
- card效果的实现
- 1126. Eulerian Path (25)