java操作ES的简单查询和bool查询
2021-03-29 00:28
标签:arc setting 不成功 ted 判断 socket api uil dad 基本操作通过client客户端对象获得客户端然后通过preparIndex等方法传入index,type,id参数和Kibaan中的操作一样 java操作ES的简单查询和bool查询 标签:arc setting 不成功 ted 判断 socket api uil dad 原文地址:https://www.cnblogs.com/xiaoruirui/p/13617092.html导入包
2.Java操作
public class EsTest {
//创建客户端
public TransportClient createClient() throws UnknownHostException {
return new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
}
//添加方法
@Test
public void testAddDoc() throws Exception{
TransportClient client = createClient();
//添加index,type,id
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("jiedada", "shuai", "1");
//准备数据
Map map = new HashMap();
map.put("id",1);
map.put("name","jiedada");
map.put("age",20);
map.put("sex",1);
//传入数据并且提交数据
IndexResponse indexResponse = indexRequestBuilder.setSource(map).get();
System.out.println(indexResponse.getResult());
//关闭客户端
client.close();
}
//修改方法
@Test
public void testUpdateDoc() throws Exception{
TransportClient client = createClient();
//添加index,type,id
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("jiedada", "shuai", "1");
//准备数据
Map map = new HashMap();
map.put("id",1);
map.put("name","jieshuai");
map.put("sex",1);
//传入数据并且提交数据
UpdateResponse updateResponse = updateRequestBuilder.setDoc(map).get();
System.out.println(updateResponse.getResult());
//关闭客户端
client.close();
}
//查询方法
@Test
public void testFindDoc() throws Exception{
TransportClient client = createClient();
GetRequestBuilder getRequestBuilder = client.prepareGet("jiedada", "shuai", "1");
GetResponse getFields = getRequestBuilder.get();
System.out.println(getFields.getSource());
client.close();
}
//删除方法方法
@Test
public void testDelDoc() throws Exception{
TransportClient client = createClient();
client.prepareDelete("jiedada", "shuai", "1").get();
client.close();
}
//批量操作
@Test
public void testBulkDoc() throws Exception{
TransportClient client = createClient();
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
//获得添加对象
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("jiedada", "shuai", "1");
//准备数据
Map map = new HashMap();
map.put("id",1);
map.put("name","jiedada");
map.put("sex",1);
indexRequestBuilder.setSource(map);
//传入数据并且提交数据
//获得更新对象
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("jiedada", "shuai", "1");
//准备数据
Map map1 = new HashMap();
map1.put("id",1);
map1.put("name","jieshuai");
map1.put("sex",1);
//传入数据并且提交数据
updateRequestBuilder.setDoc(map1);
BulkResponse bulkItemResponses = bulkRequestBuilder.add(indexRequestBuilder).add(updateRequestBuilder).get();
//获得多个对象
BulkItemResponse[] items = bulkItemResponses.getItems();
for (BulkItemResponse item : items) {
//判断是否保存成功,如果不成功
System.out.println(item.getFailure());
}
client.close();
}
//bool查询和过滤
@Test
public void testBoolDoc() throws Exception{
TransportClient client = createClient();
//获得search对象
SearchRequestBuilder search = client.prepareSearch("jiedada");
/**
* {
"query": {
"bool": { //booleanQuery 组合查询
"must": [ //查询 与(must) 或(should) 非(must not)
{
"match": {//标准查询(分词匹配) term:单词查询(部分词)
"description": "hello java"
}
}
],
"filter": { //过滤
"term": {"description": "hello world"}
}
}
},
"from": 20,
"size": 10,
"_source": ["fullName", "age", "email"],
"sort": [{"join_date": "desc"},{"age": "asc"}]
}
*/
//通过工具类获得must
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
List
文章标题:java操作ES的简单查询和bool查询
文章链接:http://soscw.com/index.php/essay/69290.html