Elasticsearch JavaApi
官网JavaApi地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html
博客:http://blog.csdn.net/molong1208/article/details/50512149
1.创建索引与数据
把json字符写入索引,索引库名为twitter、类型为tweet,id为1
语法
import static org.elasticsearch.common.xcontent.XContentFactory.*;
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
相关用例
public static boolean create(String index, String type, @Nullable String id,String json){
//index:索引库名
//type:类型
//id:文档的id
//json:json字符串
//response.isCreated():创建是否成功
IndexResponse response = client.prepareIndex(index, type, id)
// .setSource("{ \"title\": \"Mastering ElasticSearch\"}")
.setSource(json)
.execute().actionGet();
return response.isCreated();
}
2.删除索引与数据
索引库名为twitter、类型为tweet,id为1
语法
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();
相关用例
public static boolean remove(String index, String type, String id){
//index:索引库名
//type:类型
//id:文档的id
//response.isFound():是否删除成功
DeleteResponse response = client.prepareDelete(index, type, id).get();
return response.isFound();
}
3.修改数据
你可以创建一个UpdateRequest并将其发送到客户端:
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("type");
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject());
client.update(updateRequest).get();
相关用例
public static boolean update(String index, String type, String id,XContentBuilder endObject)
throws IOException, InterruptedException, ExecutionException{ // XContentBuilder endObject = XContentFactory.jsonBuilder()
// .startObject()
// .field("name", "jackRose222")
// .field("age", 28)
// .field("address","上海徐家汇")
// .endObject(); //index:索引库名
//type:类型
//endObject:使用JSON格式返回内容生成器 UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index);
updateRequest.type(type);
updateRequest.id(id);
updateRequest.doc(endObject);
UpdateResponse updateResponse = client.update(updateRequest).get(); return updateResponse.isCreated(); }
也可以用prepareUpdate()方法
client.prepareUpdate("ttl", "doc", "1")
.setDoc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.get();
相关用例
public static boolean update2(String index, String type, String id,
Map<String,Object> fieldMap) throws IOException, InterruptedException, ExecutionException{ //index:索引库名
//type:类型
//endObject:使用JSON格式返回内容生成器 //使用JSON格式返回内容生成器
XContentBuilder xcontentbuilder = XContentFactory.jsonBuilder(); if(fieldMap!=null && fieldMap.size() >0){
xcontentbuilder.startObject(); for (Map.Entry<String, Object> map : fieldMap.entrySet()) {
if(map != null){
xcontentbuilder.field(map.getKey(),map.getValue());
}
} xcontentbuilder.endObject(); UpdateResponse updateResponse = client.prepareUpdate(index, type, id)
.setDoc(xcontentbuilder)
.get(); return updateResponse.isCreated();
} return false; }
4.查询
4.1搜索API允许一个执行一个搜索查询,返回搜索结果匹配的查询。它可以跨越一个或多个指标和执行一个或多个类型。查询可以使用查询提供的Java API。搜索请求的主体使用SearchSourceBuilder构建。这是一个例子:
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
SearchResponse response = client.prepareSearch("index1", "index2")
.setTypes("type1", "type2")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.get();
请注意,所有参数都是可选的。这是最小的搜索你可以写
// MatchAll on the whole cluster with all default options
SearchResponse response = client.prepareSearch().get();
尽管Java API定义了额外的搜索类型QUERY_AND_FETCH DFS_QUERY_AND_FETCH,这些模式内部优化和不应该由用户显式地指定的API。
相关用例
public static SearchResponse search(String index, String type) {
// 查询全部
// SearchResponse response2 =
// client.prepareSearch().execute().actionGet();
// 按照索引与类型查询
//index:索引库名
//type:类型
SearchResponse response = client.prepareSearch(index).setTypes(type)
// .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
// .setQuery(QueryBuilders.termQuery("multi", "test")) // Query
// .setFrom(0)
// .setSize(5)
// .setExplain(true)
.execute().actionGet();
return response;
}
4.2多条件查询
http://blog.csdn.net/zx711166/article/details/77847120
public class EsBool{
public void BoolSearch(TransportClient client){
//多条件设置
MatchPhraseQueryBuilder mpq1 = QueryBuilders
.matchPhraseQuery("pointid","W3.UNIT1.10LBG01CP301");
MatchPhraseQueryBuilder mpq2 = QueryBuilders
.matchPhraseQuery("inputtime","2016-07-21 00:00:01");
QueryBuilder qb2 = QueryBuilders.boolQuery()
.must(mpq1)
.must(mpq2);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(qb2);
//System.out.println(sourceBuilder.toString());
//查询建立
SearchRequestBuilder responsebuilder = client
.prepareSearch("pointdata").setTypes("pointdata");
SearchResponse myresponse=responsebuilder
.setQuery(qb2)
.setFrom(0).setSize(50)
.addSort("inputtime", SortOrder.ASC)
//.addSort("inputtime", SortOrder.DESC)
.setExplain(true).execute().actionGet();
SearchHits hits = myresponse.getHits();
for(int i = 0; i < hits.getHits().length; i++) {
System.out.println(hits.getHits()[i].getSourceAsString());
}
}
}
Elasticsearch JavaApi的更多相关文章
- ElasticSearch(java) 创建索引
搜索]ElasticSearch Java Api(一) -创建索引 标签: elasticsearchapijavaes 2016-06-19 23:25 33925人阅读 评论(30) 收藏 举报 ...
- Elasticsearch的javaAPI之percolator
Elasticsearch的javaAPI之percolator percolator同意一个在index中注冊queries,然后发送包括doc的请求,返回得到在index中注冊过的而且匹配doc的 ...
- elasticsearch的javaAPI之query
elasticsearch的javaAPI之query API the Search API同意运行一个搜索查询,返回一个与查询匹配的结果(hits). 它能够在跨一个或多个index上运行, 或者一 ...
- Elasticsearch深入搜索之全文搜索及JavaAPI使用
一.基于词项与基于全文 所有查询会或多或少的执行相关度计算,但不是所有查询都有分析阶段. 和一些特殊的完全不会对文本进行操作的查询(如 bool 或 function_score )不同,文本查询可以 ...
- 通过Shell命令与JavaAPI读取ElasticSearch数据 (能力工场小马哥)
主要内容: 通过JavaAPI和Shell命令两种方式操作ES集群 集群环境: 两个 1,未配置集群名称的单节点(模拟学习测试环境); 2,两个节点的集群(模拟正常生产环境). JDK8+Elasti ...
- Elasticsearch的javaAPI之get,delete,bulk
Elsasticsearch的javaAPI之get get API同意依据其id获得指定index中的基于json document.以下的样例得到一个JSON document(index为twi ...
- Elasticsearch的javaAPI之query dsl-queries
Elasticsearch的javaAPI之query dsl-queries 和rest query dsl一样,elasticsearch提供了一个完整的Java query dsl. 查询建造者 ...
- elasticsearch的javaAPI之index
Index API 原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html ...
- ElasticSearch的javaAPI之Client
翻译的原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html#node-c ...
随机推荐
- FT5X06 如何应用在10寸电容屏(linux-3.5电容屏驱动简析&移植10寸电容屏驱动到Android4.2) (by liukun321咕唧咕唧)
这是几个月以前的东西了,在彻底遗忘之前拿出来好好写写.做个笔记,也算是造福后来人了.在做这个项目之前,没有做过电容屏的驱动,印象中的电容触摸屏是不需要校正的.IC支持多大的屏就要配多大的屏.但是拿到需 ...
- MyEclipse 报错:Errors running builder 'DeploymentBuilder' on project '工程名'
并没有更换MyEclipse版本,只是重新卸载了下,然后就报错误,参考了网上的文章 解决版本 .就是删除工程下部署文件
- 《java入门第一季》之面向对象面试题(this和super的区别)
this和super的区别? 分别是什么呢? this代表本类对象的引用. super代表父类存储空间的标识(可以理解为父类引用,可以操作父类的成员) 怎么用呢? A:调用成员变量 this.成员变量 ...
- android 向webview传值
android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用. 效果图: (一)Android部分: 布局代码: <spa ...
- 插件化开发—动态加载技术加载已安装和未安装的apk
首先引入一个概念,动态加载技术是什么?为什么要引入动态加载?它有什么好处呢?首先要明白这几个问题,我们先从 应用程序入手,大家都知道在Android App中,一个应用程序dex文件的方法数最大不能超 ...
- "《算法导论》之‘线性表’":双向循环链表
本文双链表介绍部分参考自博文数组.单链表和双链表介绍 以及 双向链表的C/C++/Java实现. 1 双链表介绍 双向链表(双链表)是链表的一种.和单链表一样,双链表也是由节点组成,它的每个数据结点中 ...
- android 高仿京东
android 高仿京东源码,两年前的作品,最近发现一些老代码,发布出来给初学者学习,下面附上几张展示的效果,有需要的请到下面地址下载,记得stare哦 https://github.com/xian ...
- HBase 运维分析
问题分析的主要手段 1.监控系统:首先用于判断系统各项指标是否正常,明确系统目前状况 2.服务端日志:查看例如region移动轨迹,发生了什么动作,服务端接受处理了哪些客户端请求. 3.gc日志:gc ...
- glib-dbus 在ubuntu9.10 和 ubuntu10.04 上安装环境的搭建
dbus-glib 安装环境搭建 安装 dbus apt-get install dbus 安装 d-feet ,用于查看 session bus 和 system bus apt-get insta ...
- 【Django】 gunicorn部署纪要
使用Gunicorn 来部署Django应用, 没有一步一步写怎么操作,简单记录下重要的点,方面以后查阅. 主要的方式还是Nginx反向代理到Gunicorn, Gunicorn wsgi来启动Dja ...