Elasticsearch的JavaAPI
获取客户端对象
public class App { private TransportClient client; //获取客户端对象
@Before
public void getClinet() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //获得客户端对象
client = new PreBuiltTransportClient(settings); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.11"), 9300));
System.out.println(client.toString()); }
## 创建索引
//创建索引
@Test
public void createIndex() {
client.admin().indices().prepareCreate("blog1").get();
client.close();
} //删除索引
@Test
public void deleteIndex() {
client.admin().indices().prepareDelete("blog").get();
client.close();
}
新建文档
//新建文档
@Test
public void createIndexByJson() {
//创建文档内容 String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服务器\","
+ "\"content\":\"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口\"" + "}"; //创建
IndexResponse response = client.prepareIndex("blog", "article", "1").setSource(json).execute().actionGet();
//打印返回值
System.out.println("索引" + response.getIndex());
System.out.println("类型" + response.getType());
System.out.println("id" + response.getId());
System.out.println("结果" + response.getResult());
client.close();
} //创建文档以hashmap
@Test
public void createIndexBymap() {
HashMap<String, Object> json = new HashMap<String, Object>(); json.put("id", "2");
json.put("title", "hph");
json.put("content", "博客 hph.blog");
IndexResponse response = client.prepareIndex("blog", "article", "2").setSource(json).execute().actionGet();
//打印返回值
System.out.println("索引" + response.getIndex());
System.out.println("类型" + response.getType());
System.out.println("id" + response.getId());
System.out.println("结果" + response.getResult());
client.close();
}
//创建文档以hashmap
@Test
public void createIndexBymap() {
HashMap<String, Object> json = new HashMap<String, Object>(); json.put("id", "2");
json.put("title", "hph");
json.put("content", "博客 hph.blog");
IndexResponse response = client.prepareIndex("blog", "article", "2").setSource(json).execute().actionGet();
//打印返回值
System.out.println("索引" + response.getIndex());
System.out.println("类型" + response.getType());
System.out.println("id" + response.getId());
System.out.println("结果" + response.getResult());
client.close(); } //创建文档已bulder
@Test
public void createIndexbyBulider() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("id", "4").field("title", "博客").field("content", "hphblog.cn").endObject();
IndexResponse response = client.prepareIndex("blog", "article", "3").setSource(builder).execute().actionGet();
//打印返回值
System.out.println("索引" + response.getIndex());
System.out.println("类型" + response.getType());
System.out.println("id" + response.getId());
System.out.println("结果" + response.getResult());
client.close();
}
查询索引
//单个索引查询
@Test
public void queryIndex() {
//查询
GetResponse response = client.prepareGet("blog", "article", "2").get(); //打印
System.out.println(response.getSourceAsString()); //关闭资源
client.close(); } //多个索引查询
@Test
public void queryMultiIndex() { //查询
MultiGetResponse response = client.prepareMultiGet().add("blog", "article", "3")
.add("blog", "article", "1")
.add("blog", "article", "2").get(); for (MultiGetItemResponse multiGetItemResponse : response) {
GetResponse response1 = multiGetItemResponse.getResponse(); //判断是否存在
if (response1.isExists()) {
System.out.println(response1.getSourceAsString());
}
}
} //更改数据
@Test
public void update() throws ExecutionException, InterruptedException, IOException {
UpdateRequest updateRequest = new UpdateRequest("blog", "article", "2");
updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("id", "2")
.field("title", "hphblog").field("content", "大数据博客学习技术分享").endObject()
); UpdateResponse response = client.update(updateRequest).get(); //打印返回值
System.out.println("索引" + response.getIndex());
System.out.println("类型" + response.getType());
System.out.println("id" + response.getId());
System.out.println("结果" + response.getResult());
client.close(); }
更新文档
//更新文档updaset
@Test
public void upsert() throws IOException, ExecutionException, InterruptedException {
//没有就创建
IndexRequest indexRequest = new IndexRequest("blog", "article", "5");
indexRequest.source(XContentFactory.jsonBuilder().startObject().field("id", "5")
.field("title", "大数据技术分享的学习").field("content", "大数据技术的分享与学习希望和大家多多交流").endObject
());
//有文档内容就更新
UpdateRequest updateRequest = new UpdateRequest("blog", "article", "5");
updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("id", "5")
.field("title", "hphblog").field("content", "技术分享、技术交流、技术学习").endObject()
); updateRequest.upsert(indexRequest); UpdateResponse response = client.update(updateRequest).get(); //打印返回值
System.out.println("索引" + response.getIndex());
System.out.println("类型" + response.getType());
System.out.println("id" + response.getId());
System.out.println("结果" + response.getResult());
client.close(); }
删除文档
//删除文档
@Test
public void delete() {
client.prepareDelete("blog", "article", "5").get();
client.close(); }
查询所有
//查询所有
@Test
public void matchAllquery() {
//1.执行查询
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.matchAllQuery()).get(); SearchHits hits = response.getHits(); System.out.println("查询的结果为" + hits.getTotalHits()); Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println(next.getSourceAsString());
} client.close();
} //分词查询
@Test
public void query() {
//1.执行查询
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.queryStringQuery("学习全文")).get(); SearchHits hits = response.getHits(); System.out.println("查询的结果为" + hits.getTotalHits()); Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println(next.getSourceAsString());
} client.close();
}
通配符查询
//通配符查询
@Test
public void wildcardQuery() {
//1.执行查询
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.wildcardQuery("content", "分")).get(); SearchHits hits = response.getHits(); System.out.println("查询的结果为" + hits.getTotalHits()); Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println(next.getSourceAsString());
} client.close();
}
模糊查询
//模糊查询
@Test
public void fuzzyQuery() {
//1.执行查询
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.fuzzyQuery("title", "LuceNe")).get(); SearchHits hits = response.getHits(); System.out.println("查询的结果为" + hits.getTotalHits()); Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println(next.getSourceAsString());
} client.close();
} //映射相关的操作
@Test
public void createMapping() throws Exception { // 1设置mapping
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")
.startObject("properties")
.startObject("id1")
.field("type", "string")
.field("store", "yes")
.endObject()
.startObject("title2")
.field("type", "string")
.field("store", "no")
.endObject()
.startObject("content")
.field("type", "string")
.field("store", "yes")
.endObject()
.endObject()
.endObject()
.endObject();
添加mapping
// 2 添加mapping
PutMappingRequest mapping = Requests.putMappingRequest("blog1").type("article").source(builder); client.admin().indices().putMapping(mapping).get(); // 3 关闭资源
client.close();
}
原文:http://hphblog.cn/2018/12/16/Elasticsearch%E7%9A%84JavaAPI/
Elasticsearch的JavaAPI的更多相关文章
- 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之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 ...
- Elasticsearch JavaApi
官网JavaApi地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html 博 ...
- ElasticSearch的基本用法与集群搭建
一.简介 ElasticSearch和Solr都是基于Lucene的搜索引擎,不过ElasticSearch天生支持分布式,而Solr是4.0版本后的SolrCloud才是分布式版本,Solr的分布式 ...
- ElasticSearch Java api 详解_V1.0
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
随机推荐
- taro refs引用
创建 Refs Taro 支持使用字符串和函数两种方式创建 Ref: 使用字符串创建 ref 通过函数创建 ref(推荐) 你也可以通过传递一个函数创建 ref, 在函数中被引用的组件会作为函数的第一 ...
- 【转】基于keras 的神经网络股价预测模型
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY from matplotli ...
- Qt中多线程问题
1. 出现的问题 编写视频解码器程序时,把解码那部分单独置于一个线程中进行处理,后来实际应用到项目中发现内存泄漏很严重 问题就出现在多线程中,每次视频解码器程序关闭时首先必须释放其所涉及的堆空间, 由 ...
- 基于Elasticsearch的智能客服机器人
本次分享主要会介绍一下ES是如何帮我们完成NLP的任务的.在做NLP相关任务的时候,ES的相似度算法并不足以支撑用户的搜索,需要使用一些与语义相关的方法进行改进.但是ES的很多特性对我们优化搜索体验是 ...
- Java NIO通信的基础,基于TCP C/S例子介绍
为了更好的理解Netty异步事件驱动网络通信框架,有必要先了解一点Java NIO原生的通信理论,下面将结合基于TCP的例子程序,含客户端和服务端的源码,实现了Echo流程. Java NIO的核心概 ...
- azure 1元试用,如何创建虚拟机等
付了1元后,直接进 https://manage.windowsazure.cn 创建虚拟机即可.
- line-height的理解
font-size:0清除display:inline-block元素换行符间隙,比如两个img标签之间有换行符 行内元素的高度是由其行高决定的. Div或者其他元素内的图片,底部会有间隙,原因是图片 ...
- 无需AutoCAD,用C#生成DWG文件
是一个类库:Teigha.NET for .DWG 利用它就可以在无需安装AutoCAD软件的情况下,生成.读取DWG文件,适合那些导入导出的场合. Teigha曾用名OpenDWG .DWGdire ...
- 详细记录sql运行时间(精确到毫秒)
写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的 ...
- [蓝桥杯]ALGO-124.算法训练_数字三角形
问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●<三角形行数≤: ●三角 ...