1. 先导入pom依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.0.0</version>
</dependency>
<!-- 需要单独指定log4j,没有就报错-->
<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>
</dependencies>
2. 创建索引 prepareIndex
2.1 创建Client
private TransportClient client;
@Before
public void initClient() throws UnknownHostException {
client = new PreBuiltTransportClient(Settings.builder().put("cluster.name", "myes").build())
.addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("node02"), 9300))
.addTransportAddress(new TransportAddress(InetAddress.getByName("node03"), 9300));
}
2.2 打印并关闭Clinet
private IndexResponse indexResponse;
@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();
}
```
## 2.3 自己拼装json插入文档到es集群
```java
@Test
public void index1() throws Exception {
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"travelying out Elasticsearch\"" +
"}";
indexResponse = client.prepareIndex("news", "article", "1")
.setSource(json, XContentType.JSON)
.get();
}
2.4 使用map插入文档到es集群
@Test
public void index2() throws Exception {
HashMap<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put("name", "zhangsan");
jsonMap.put("sex", "1");
jsonMap.put("age", "18");
jsonMap.put("address", "bj");
indexResponse = client.prepareIndex("news", "article", "2")
.setSource(jsonMap)
.get();
}
2.5 XcontentBuilder转化成json数据插入文档到es集群
@Test
public void index3() throws IOException {
indexResponse = client.prepareIndex("news", "article", "3")
.setSource(new XContentFactory().jsonBuilder()
.startObject()
.field("name", "lisi")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject())
.get();
}
2.6 批量插入文档
/**
* 批量插入文档,可以提高创建索引的速度,主要减少网络请求。
* 如果正常情况,创建一个文档就会发送一次网络请求,其实就是发起一次http请求。
* bulkIndex就可以将多个文档合并在一起之后,发送一次请求。
* @throws IOException
*/
@Test
public void index4() throws IOException {
BulkRequestBuilder bulk = client.prepareBulk();
bulk.add(client.prepareIndex("news", "article", "4")
.setSource(new XContentFactory().jsonBuilder()
.startObject()
.field("name", "wangwu")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject()));
bulk.add(client.prepareIndex("news", "article", "5")
.setSource(new XContentFactory().jsonBuilder()
.startObject()
.field("name", "zhaoliu")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject()));
BulkResponse bulkResponse = bulk.get();
System.out.println(bulkResponse);
}
2.7 导入Gson依赖插入文档
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
@Test
public void index5(){
Gson gson = new Gson();
User user = new User("6", "itcast", "18", "1");
String json = gson.toJson(user);
System.out.println(json);
indexResponse = client.prepareIndex("news","article",user.getId())
.setSource(json,XContentType.JSON)
.get();
}
3. 查询索引
3.1 使用prepareGet查询数据
@Test
public void query1(){
GetResponse response = client.prepareGet().setIndex("news").setType("article").setId("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();
}
3.2 使用prepareMultiGet减少网络请求查询数据
@Test
public void query2(){
MultiGetResponse responses = client.prepareMultiGet().add("news", "article", "1")
.add("news", "article", "2")
.add("news", "article", "3")
.add("news", "article", "4")
.get();
ArrayList<User> users = new ArrayList<User>();
for (MultiGetItemResponse response : responses) {
String json = response.getResponse().getSourceAsString();
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
users.add(user);
}
System.out.println(users);
}
3.3 使用search查询数据并分页
3.3.1 分页前的配置文件
由于分页前需要排序,所以需要配置id字段的fielddata属性等于true。
DELETE blog2
PUT /blog2/?pretty
PUT blog2/_mapping/article
{
"properties": {
"id": {
"type": "text",
"fielddata": true
},
"title": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
3.3.2 分页的java代码
@Test
public void query3(){
SearchResponse response = client.prepareSearch("blog2").setTypes("article")
//所用的查询方式都可以直接new出来,
.setQuery(new MatchAllQueryBuilder())
//fieldData=true
.addSort("id", SortOrder.DESC)
//浅分页
.setFrom(5).setSize(5)
.get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
// //docid没有值,旧版本有值
// int docId = hit.docId();
// System.out.println(docId);
}
}
3.3.3 分页的数据准备
@Test
public void initPageData() throws UnknownHostException {
// 创建Client连接对象
Settings settings = Settings.builder().put("cluster.name", "myes").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));
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("blog22222", "article", article.getId())
.setSource(json,XContentType.JSON)
.get();
}
//释放资源
client.close();
}
3.3.4 分页配置IK分词器
PUT /blog22222?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"
}
}
}
}
}
4 删除索引
package cn.itcast.es.api;
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;
public class DeleteIndex {
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("node01"), 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("twitter", "doc", "100").get();
}
@Test
/**
* 根据根据查询结果删除数据,并触发相关事件
*/
public void delete2() throws UnknownHostException {
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("gender", "male"))
.source("twitter")
.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("------------错误了");
}
});
}
}
5 查询结果高亮操作
5.1 什么是高亮显示
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
-
百度搜索关键字"啊策策"
插播个小广告,百度搜索啊策策可以搜到博主呦~ -
京东商城搜索"笔记本"
5.2 高亮显示的html分析
通过开发者工具查看高亮数据的html代码实现:
ElasticSearch可以对查询出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ElasticSearch使用什么标签对高亮关键字进行包裹
5.3 高亮显示代码实现
@Test
//高亮查询
public void test11() throws Exception{
// 创建Client连接对象
Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 搜索数据
SearchRequestBuilder searchRequestBuilder = client
.prepareSearch("blog2").setTypes("article")
.setQuery(QueryBuilders.termQuery("title", "搜索"));
//设置高亮数据
HighlightBuilder hiBuilder=new HighlightBuilder();
hiBuilder.preTags("<font style='color:red'>");
hiBuilder.postTags("</font>");
hiBuilder.field("title");
searchRequestBuilder.highlighter(hiBuilder);
//获得查询结果数据
SearchResponse searchResponse = searchRequestBuilder.get();
//获取查询结果集
SearchHits searchHits = searchResponse.getHits();
System.out.println("共搜到:"+searchHits.getTotalHits()+"条结果!");
//遍历结果
for(SearchHit hit:searchHits){
System.out.println("String方式打印文档搜索内容:");
System.out.println(hit.getSourceAsString());
System.out.println("Map方式打印高亮内容");
System.out.println(hit.getHighlightFields());
System.out.println("遍历高亮集合,打印高亮片段:");
Text[] text = hit.getHighlightFields().get("title").getFragments();
for (Text str : text) {
System.out.println(str);
}
}
//释放资源
client.close();
}