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 + '\'' +
'}';
}
}