Elasticsearch教程 ,Elasticsearch count 查询,Elasticsearch 查询是否存在

JSON 2017-01-03 23:50:25 67761

一、Elasticsearch Count查询

当我们使用  Elasticsearch  的时候,如果只想知道符合条件的结果集,应该怎么查询?

更多教程点击:  Elasticsearch教程  。

1.1 Elasticsearch count Java API 查询

Client client = ESTools.client;
SearchResponse response = client.prepareSearch(MappingManager.ASK)
.setTypes(MappingManager.ASK)
.setQuery(new TermQueryBuilder("id", id))//设置查询类型
.setSearchType(SearchType.COUNT)//设置查询类型,有的版本可能过期
.setSize(0)//设置返回结果集为0
.get();
long length = response.getHits().totalHits();

最后返回了符合结果集的Count 数量,但是不返回结果集,不反回结果集靠size = 0  来决定,当然我觉得  Elasticsearch  在一些版本里应该会对数据级别的Count 查询应该有更好的优化,自己对应想当前版本的  API  。我的Version:2.0.2

1.2 Elasticsearch count Http API 查询

POST - http://192.168.0.1:9200/index/type/_search/
{
  "size" : 0,
  "query" : {
    "term" : {
      "id" : "adf183208e9a4116353e9d9cd78f2b6a"
    }
  }
}



1.3 Elasticsearch Index Count查询

CountResponse response = client.prepareCount("index1","index2").get();
 long count = response.getCount();//返回当前index Count数量

1.4 Elasticsearch Type Count查询

CountResponse response = client.prepareCount("index1","index2").setTypes("type1","type2").get();
 long count = response.getCount();//返回符合条件的数据

二、Elasticsearch 查询数据是否存在

我也是认为  Elasticsearch  一些版本会有这个方法。下面看看官方的介绍:https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-exists.html

2.1 curl 方式查询数据是否存在:

查询:

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search/exists?q=user:kimchy'

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search/exists' -d '
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}'

返回结果:

{
    "exists" : true
}

  Java  API 我这个版本我没找到,其他版本有一些应该有 Java API

2.2 Elasticsearch Java API 数据Exists判断。

/**
 * 判断数据是否存在
 * @param id
 * @return
 */
public static boolean existsById(String id){
	Client client = ESTools.client;
	 SearchRequestBuilder searchBuilder = client.prepareSearch(MappingManager.ASK)
			.setTypes(MappingManager.ASK)
				.setQuery(new TermQueryBuilder("id", id))//设置查询类型
					.setSearchType(SearchType.COUNT)//设置查询类型,有的版本可能过期
						.setSize(0);//设置返回结果集为0
					SearchResponse response = searchBuilder.get();
	long length = response.getHits().totalHits();
	return length > 0;
}

2.3 Elasticsearch Java API 判断 Index 是否存在。

//Index 可以多个
 ExistsRequest request = new ExistsRequest("index1","index2");  
 ExistsResponse response = client.exists(request).get();
 //返回是否存在
 boolean exists = response.exists();

2.4 Elasticsearch Java API 判断 Type 是否存在。

//Index 可以多个
 ExistsRequest request = new ExistsRequest("index1","index2").types("type1","type2");  
 ExistsResponse response = client.exists(request).get();
 //返回是否存在
 boolean exists = response.exists();

版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/206.html

转载时必须以链接形式注明原始出处及本声明。


如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询
Elasticsearch 教程Elasticsearch 日期查询详解,Elasticsearch Date 查询Java API
Elasticsearch 聚合(aggregation)查询返回所有
Elasticsearch教程(五) elasticsearch Mapping的创建
Elasticsearch教程Elasticsearch配置文件 — elasticsearch.yml
Elasticsearch教程(六) elasticsearch Client创建
Elasticsearch教程Elasticsearch Java API创建Mapping,指定分词器
Elasticsearch教程(八) elasticsearch delete 删除数据(Java)
Elasticsearch 教程Elasticsearch部署阿里云集群,支持外网请求方式
Elasticsearch教程(四) elasticsearch head 插件安装和使用
最新文章
Java赋值运算符 13
XML内部实体和外部实体 163
Java面向对象编程概念 143
PHP回显语句 91
Linux—文件树 116
C语言while循环和do while循环 131
Python元组剖析 200
MySQL触发器教程 314
sql使用布尔运算符和关系运算符 258
C语言的变量和常量 296
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 681996
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674719
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 602117
免费天气API,天气JSON API,不限次数获取十五天的天气预报 577117
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 552917
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509353
Elasticsearch教程(四) elasticsearch head 插件安装和使用 480007
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 264571
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244246
Elasticsearch教程(一),全程直播(小白级别) 225569
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...