ElasticSearch的java api
pom
- <dependencies>
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>transport</artifactId>
- <version>6.0.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.9.1</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.2</version>
- </dependency>
- </dependencies>
创建索引
- package com.zy.esapi;
- import com.google.gson.Gson;
- import org.elasticsearch.action.bulk.BulkRequestBuilder;
- import org.elasticsearch.action.bulk.BulkResponse;
- import org.elasticsearch.action.index.IndexResponse;
- import org.elasticsearch.client.transport.TransportClient;
- import org.elasticsearch.common.settings.Settings;
- import org.elasticsearch.common.transport.TransportAddress;
- import org.elasticsearch.common.xcontent.XContentFactory;
- import org.elasticsearch.common.xcontent.XContentType;
- import org.elasticsearch.transport.client.PreBuiltTransportClient;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.HashMap;
- /**
- * create by zy
- * TODO:创建索引
- */
- public class CreateIndexTest {
- private TransportClient client;
- private IndexResponse indexResponse;
- /**
- * 创建client
- *
- * @throws UnknownHostException
- */
- @Before
- public void initClient() throws UnknownHostException {
- client = new PreBuiltTransportClient(Settings.builder().put("cluster.name", "myes").build())
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.41"), 9300))
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.42"), 9300))
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.43"), 9300));
- }
- /**
- * 常用的 map格式
- *
- * @throws Exception
- */
- @Test
- public void index1() throws Exception {
- HashMap<String, String> jsonMap = new HashMap<String, String>();
- jsonMap.put("id", "1");
- jsonMap.put("name", "zhangsan");
- jsonMap.put("sex", "1");
- jsonMap.put("age", "18");
- jsonMap.put("address", "beijing");
- //index type id
- indexResponse = client.prepareIndex("user", "baseinfo", "1")
- .setSource(jsonMap)
- .get();
- }
- /**
- * 自己拼装json
- *
- * @throws Exception
- */
- @Test
- public void index2() throws Exception {
- String json = "{" +
- "\"id\":\"2\"," +
- "\"name\":\"kimchy\"," +
- "\"sex\":\"1\"," +
- "\"age\":\"22\"," +
- "\"address\":\"shanghai\"" +
- "}";
- indexResponse = client.prepareIndex("user", "baseinfo", "2")
- .setSource(json, XContentType.JSON)
- .get();
- }
- /**
- * XcontentBuilder
- *
- * @throws IOException
- */
- @Test
- public void index3() throws IOException {
- indexResponse = client.prepareIndex("user", "baseinfo", "3")
- .setSource(new XContentFactory().jsonBuilder()
- .startObject()
- .field("id", "3")
- .field("name", "lisi")
- .field("age", "18")
- .field("sex", "0")
- .field("address", "beijing")
- .endObject())
- .get();
- }
- /**
- * 常用的 对象json格式
- */
- @Test
- public void index4() {
- Gson gson = new Gson();
- UserBaseInfo user = new UserBaseInfo("6", "xueyou", "13", "1", "xianggang");
- String json = gson.toJson(user);
- indexResponse = client.prepareIndex("user", "baseinfo", user.getId())
- .setSource(json, XContentType.JSON)
- .get();
- }
- /**
- * 批量创建
- * 批量操作,可以提高创建索引的速度,主要减少网络请求。
- * 如果正常情况,创建一个文档就会发送一次网络请求,其实就是发起一次http请求。
- * bulkIndex就可以将多个文档合并在一起之后,发送一次请求。
- *
- * @throws IOException
- */
- @Test
- public void index5() throws IOException {
- BulkRequestBuilder bulk = client.prepareBulk();
- bulk.add(client.prepareIndex("user", "baseinfo", "4")
- .setSource(new XContentFactory().jsonBuilder()
- .startObject()
- .field("id", "4")
- .field("name", "wangwu")
- .field("age", "18")
- .field("sex", "0")
- .field("address", "hangzhou")
- .endObject()));
- bulk.add(client.prepareIndex("user", "baseinfo", "5")
- .setSource(new XContentFactory().jsonBuilder()
- .startObject()
- .field("id", "5")
- .field("name", "zhaoliu")
- .field("age", "18")
- .field("sex", "0")
- .field("address", "shenzhen")
- .endObject()));
- BulkResponse bulkResponse = bulk.get();
- System.out.println(bulkResponse);
- }
- /**
- * 打印返回信息
- */
- @After
- public void printResultAndCloseClient() {
- System.out.println("index:" + indexResponse.getIndex());
- System.out.println("type:" + indexResponse.getType());
- System.out.println("id:" + indexResponse.getId());
- System.out.println("version:" + indexResponse.getVersion());
- System.out.println("status:" + indexResponse.getResult());
- client.close();
- }
- }
删除索引
- package com.zy.esapi;
- import org.elasticsearch.action.ActionListener;
- import org.elasticsearch.action.delete.DeleteResponse;
- import org.elasticsearch.client.transport.TransportClient;
- import org.elasticsearch.common.settings.Settings;
- import org.elasticsearch.common.transport.TransportAddress;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.index.reindex.BulkByScrollResponse;
- import org.elasticsearch.index.reindex.DeleteByQueryAction;
- import org.elasticsearch.rest.RestStatus;
- import org.elasticsearch.transport.client.PreBuiltTransportClient;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- /**
- * create by zy
- * TODO:
- */
- public class DeleteIndexTest {
- private TransportClient client;
- private DeleteResponse response;
- @Before
- public void init() throws UnknownHostException {
- Settings settings = Settings.builder().put("cluster.name", "myes").build();
- client = new PreBuiltTransportClient(settings)
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.41"), 9300))
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.42"), 9300))
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.43"), 9300));
- }
- @After
- public void close() throws UnknownHostException {
- if (response != null) {
- String index = response.getIndex();
- String type = response.getType();
- String id = response.getId();
- long version = response.getVersion();
- System.out.println("index " + index + " type" + type + "" + id + "" + version + "" + version);
- RestStatus status = response.status();
- System.out.println("status:" + status.getStatus());
- client.close();
- }
- }
- /**
- * 根据文档进行删除
- *
- * @throws UnknownHostException
- */
- @Test
- public void delete1() throws UnknownHostException {
- response = client.prepareDelete("user", "baseinfo", "1").get();
- }
- @Test
- /**
- * 根据根据查询结果删除数据,并触发相关事件
- */
- public void delete2() throws UnknownHostException {
- DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
- .filter(QueryBuilders.matchQuery("sex", "1"))
- .source("user")
- .execute(new ActionListener<BulkByScrollResponse>() {
- public void onResponse(BulkByScrollResponse response) {
- long deleted = response.getDeleted();
- System.out.println("---------------" + deleted);
- }
- public void onFailure(Exception e) {
- System.out.println("------------错误了");
- }
- });
- }
- }
查询
- package com.zy.esapi;
- import com.google.gson.Gson;
- import org.elasticsearch.action.get.GetResponse;
- import org.elasticsearch.action.get.MultiGetItemResponse;
- import org.elasticsearch.action.get.MultiGetResponse;
- import org.elasticsearch.action.search.SearchRequestBuilder;
- import org.elasticsearch.action.search.SearchResponse;
- import org.elasticsearch.client.transport.TransportClient;
- import org.elasticsearch.common.settings.Settings;
- import org.elasticsearch.common.text.Text;
- import org.elasticsearch.common.transport.TransportAddress;
- import org.elasticsearch.common.xcontent.XContentType;
- import org.elasticsearch.index.query.MatchAllQueryBuilder;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.search.SearchHit;
- import org.elasticsearch.search.SearchHits;
- import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
- import org.elasticsearch.search.sort.SortOrder;
- import org.elasticsearch.transport.client.PreBuiltTransportClient;
- import org.junit.Before;
- import org.junit.Test;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- /**
- * create by zy
- * TODO:查询
- */
- public class QueryTest {
- private TransportClient client;
- /**
- * 创建client
- *
- * @throws UnknownHostException
- */
- @Before
- public void initClient() throws UnknownHostException {
- client = new PreBuiltTransportClient(Settings.builder().put("cluster.name", "myes").build())
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.41"), 9300))
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.42"), 9300))
- .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.44.43"), 9300));
- }
- /**
- * 根据id查询一个
- */
- @Test
- public void query1() {
- GetResponse response = client.prepareGet("user", "baseinfo", "1").get();
- String index = response.getIndex();
- String type = response.getType();
- String id = response.getId();
- System.out.println(response.getSourceAsString());
- System.out.println(index);
- System.out.println(type);
- System.out.println(id);
- client.close();
- }
- /**
- * 查询多个id
- */
- @Test
- public void query2() {
- MultiGetResponse responses = client.prepareMultiGet()
- .add("user", "baseinfo", "1")
- .add("user", "baseinfo", "2")
- .add("user", "baseinfo", "3")
- .add("user", "baseinfo", "4")
- .get();
- ArrayList<UserBaseInfo> users = new ArrayList<UserBaseInfo>();
- for (MultiGetItemResponse response : responses) {
- String json = response.getResponse().getSourceAsString();
- Gson gson = new Gson();
- UserBaseInfo user = gson.fromJson(json, UserBaseInfo.class);
- users.add(user);
- }
- System.out.println(users);
- client.close();
- }
- @Test
- public void query3() {
- SearchResponse response = client.prepareSearch("user").setTypes("baseinfo")
- //所有的查询方式都可以直接new出来,
- .setQuery(new MatchAllQueryBuilder())
- //5.x以后如果用字段排序,需要在setting中预先设置fielddata=true
- .addSort("id", SortOrder.DESC)
- //浅分页
- .setFrom(1).setSize(3)
- .get();
- SearchHits hits = response.getHits();
- for (SearchHit hit : hits) {
- System.out.println(hit.getSourceAsString());
- //docid没有值,旧版本有值
- //int docId = hit.docId();
- //System.out.println(docId);
- }
- }
- /**
- *
- DELETE user
- PUT /user/?pretty
- PUT user/_mapping/baseinfo
- {
- "properties":{
- "id":{
- "type":"text",
- "fielddata":true
- },
- "name":{
- "type":"text"
- },
- "age":{
- "type":"text"
- },
- "sex":{
- "type":"text"
- },
- "address":{
- "type":"text"
- }
- }
- }
- */
- //--------------------------------search 高亮--------------------------------
- //建立索引 需要用ik分词器
- /**
- * DELETE allarticle
- * PUT /allarticle?pretty
- * {
- * "settings" : {
- * "analysis" : {
- * "analyzer" : {
- * "ik" : {
- * "tokenizer" : "ik_max_word"
- * }
- * }
- * }
- * },
- * "mappings" : {
- * "article" : {
- * "dynamic" : true,
- * "properties" : {
- * "id" : {
- * "type" : "text",
- * "fielddata": true
- * },
- * "title" : {
- * "type" : "text",
- * "analyzer" : "ik_max_word"
- * },
- * "content" : {
- * "type" : "text",
- * "analyzer" : "ik_max_word"
- * }
- * }
- * }
- * }
- * }
- */
- /**
- * 数据准备
- *
- * @throws UnknownHostException
- */
- @Test
- public void initPageData() throws UnknownHostException {
- for (int i = 1; i <= 100; i++) {
- // 描述json 数据
- Article article = new Article();
- article.setId(i + "");
- article.setTitle(i + "搜索工作其实很快乐");
- article.setContent(i
- + "我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。");
- Gson gson = new Gson();
- String json = gson.toJson(article);
- // 建立文档
- client.prepareIndex("allarticle", "article", article.getId())
- .setSource(json, XContentType.JSON)
- .get();
- }
- //释放资源
- client.close();
- }
- @Test
- //高亮查询
- public void test11() throws Exception {
- // 搜索数据
- SearchRequestBuilder searchRequestBuilder = client
- .prepareSearch("allarticle").setTypes("article")
- //.setQuery(QueryBuilders.termQuery("title", "搜索"));
- .setQuery(QueryBuilders.multiMatchQuery("搜索", "title", "content"));//这种多字段查询效率高些
- //设置高亮数据
- HighlightBuilder hiBuilder = new HighlightBuilder();
- //设置格式
- hiBuilder.preTags("<font style='color:red'>");
- hiBuilder.postTags("</font>");
- //设置字段
- hiBuilder.field("title");
- hiBuilder.field("content");
- searchRequestBuilder.highlighter(hiBuilder);
- //获得查询结果数据
- SearchResponse searchResponse = searchRequestBuilder.get();
- //获取查询结果集
- SearchHits searchHits = searchResponse.getHits();
- System.out.println("共搜到:" + searchHits.getTotalHits() + "条结果!");
- //遍历结果
- for (SearchHit hit : searchHits) {
- System.out.println("start************************************");
- System.out.println("String方式打印文档搜索内容:");
- System.out.println(hit.getSourceAsString());
- System.out.println("-----------------------------------------");
- System.out.println("Map方式打印高亮内容");
- System.out.println(hit.getHighlightFields());
- System.out.println("-----------------------------------------");
- System.out.println("遍历高亮集合,打印高亮片段:");
- Text[] titles = hit.getHighlightFields().get("title").getFragments();
- for (Text str : titles) {
- System.out.println(str);
- }
- Text[] contents = hit.getHighlightFields().get("content").getFragments();
- for (Text str : contents) {
- System.out.println(str);
- }
- System.out.println("end**************************************");
- }
- //释放资源
- client.close();
- }
- }
其他类
- package com.zy.esapi;
- /**
- * create by zy
- * TODO:
- */
- public class Article {
- private String id;
- private String title;
- private String content;
- public Article() {
- }
- public Article(String id, String title, String content) {
- this.id = id;
- this.title = title;
- this.content = content;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- @Override
- public String toString() {
- return "Article{" +
- "id='" + id + '\'' +
- ", title='" + title + '\'' +
- ", content='" + content + '\'' +
- '}';
- }
- }
- package com.zy.esapi;
- import java.io.Serializable;
- /**
- * create by zy
- * TODO:
- */
- public class UserBaseInfo implements Serializable {
- private String id;
- private String name;
- private String age;
- private String sex;
- private String address;
- public UserBaseInfo() {
- }
- public UserBaseInfo(String id, String name, String age, String sex, String address) {
- this.id = id;
- this.name = name;
- this.age = age;
- this.sex = sex;
- this.address = address;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public String toString() {
- return "UserBaseInfo{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", age='" + age + '\'' +
- ", sex='" + sex + '\'' +
- ", address='" + address + '\'' +
- '}';
- }
- }
ElasticSearch的java api的更多相关文章
- Elasticsearch中JAVA API的使用
1.Elasticsearch中Java API的简介 Elasticsearch 的Java API 提供了非常便捷的方法来索引和查询数据等. 通过添加jar包,不需要编写HTTP层的代码就可以开始 ...
- ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程
前言 在上一篇中介绍了ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解,本篇文章就来讲解下 ElasticSearch 6.x官方Java API的 ...
- 使用Java操作Elasticsearch(Elasticsearch的java api使用)
1.Elasticsearch是基于Lucene开发的一个分布式全文检索框架,向Elasticsearch中存储和从Elasticsearch中查询,格式是json. 索引index,相当于数据库中的 ...
- ElasticSearch AggregationBuilders java api常用聚会查询
以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: "mappings": { "pl ...
- elasticsearch常用JAVA API 实例
1.引入dependency <dependency> <groupId>org.springframework.data</groupId> <artifa ...
- ElasticSearch排序Java api简单Demo
代码: String time1 = ConstValue.GetCurrentDate(); SortBuilder sortBuilder = SortBuilders.fieldSort(&qu ...
- Elasticsearch的Java API做类似SQL的group by聚合。
https://www.cnblogs.com/kangoroo/p/8033955.html
- Elasticsearch JAVA api搞定groupBy聚合
本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合.为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...
- Elasticsearch JAVA api轻松搞定groupBy聚合
本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合. 为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...
随机推荐
- vue-router教程一(安装篇)
Installation安装 #直接下载/cdn https://unpkg.com/vue-router/dist/vue-router.js Unpkg.com提供基于NPM的CDN链接.上述链接 ...
- elixir 集成ejabberd
备注: 我开发测试的环境时centos 1. 预备环境 1. openssl yum install -y openssl-devel 2. xml yum install -y expat-dev ...
- jest js 测试框架-简单方便人性化
1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...
- Automating CSS Regression Testing
The following is a guest post by Garris Shipon . We've touched on the four types of CSS testing here ...
- Oracle数据库安装图文操作步骤1
Oracle数据库安装图文操作步骤 一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官 ...
- sql的一些事件处理
select getdate() select Convert(varchar(10),getdate(),120) yyyy-mm-ddselect Convert(varchar(20),getd ...
- Ubuntu sudo: add-apt-repository: command not found
安装缺少的指令即可 $ sudo apt-get install software-properties-common python-software-properties
- Hibernate学习6—Hibernate 映射类型
第一节:基本类型映射 com.cy.model.Book.java: package com.cy.model; import java.sql.Blob; import java.util.Date ...
- centos7系统安装python3,pip3,django
首先去python官网下载python3的源码包,网址:https://www.python.org/ 或者直接wget下载 wget https://www.python.org/ftp/pytho ...
- Django缓存,信号,序列化
缓存 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次的的后台操作 ...