上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索。
String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));
String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));
String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));
String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));
String data5 = JsonUtil.model2Json(new Blog(5, "Git基本知识git", "2016-06-19", "Shell是什么..."));
String data6 = JsonUtil.model2Json(new Blog(6, "C++基本知识", "2016-06-19", "Shell是什么..."));
String data7 = JsonUtil.model2Json(new Blog(7, "Mysql基本知识", "2016-06-19", "git是什么..."));
一次查询可分为下面四个步骤:
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();
package cn.com.bropen.es;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import static org.elasticsearch.index.query.QueryBuilders.*;
public class ElasticSearchGet {
public static void main(String[] args) {
// client startup
try {
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
QueryBuilder qb1 = termQuery("title", "hibernate");
QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
.actionGet();
SearchHits hits = response.getHits();
if (hits.totalHits() > 0) {
for (SearchHit hit : hits) {
System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title")
}
} else {
System.out.println("搜到0条结果");
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
查询结果:
log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
score:0.5: {posttime=2016-06-19, id=1, title=git简介, content=SVN与Git最主要的区别...}
score:0.17673586: {posttime=2016-06-19, id=7, title=Mysql基本知识, content=git是什么...}
score:0.049935166: {posttime=2016-06-19, id=5, title=Git基本知识git, content=Shell是什么...}
参考资料: