package com.wjh.demo.elasticsearch;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.net.InetAddress;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
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.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Elasticsearch JAVA API
* version:5.5.1
* @author wujianh
*
*/
public class Elasticsearch {
private static final Logger log=LoggerFactory.getLogger(Elasticsearch.class);
public static void main( String[] args )
{
log.info("-------------------------app main begin");
try {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.61.136"), 9300));
//jsonBuilder() --> import static org.elasticsearch.common.xcontent.XContentFactory.*;
//1.创建索引
// IndexResponse response1 = client.prepareIndex("index10", "x10", "1")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小明")
// .field("pinyin", "xiaoMing")
// .field("age", "10")
// .field("class", "四年级二班")
// .endObject()
// )
// .get();
//
// IndexResponse response2 = client.prepareIndex("index10", "x10", "2")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小红")
// .field("pinyin", "xiaoHong")
// .field("age", "8")
// .field("class", "二年级三班")
// .endObject()
// )
// .get();
// IndexResponse response3 = client.prepareIndex("index10", "x10", "3")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小田")
// .field("pinyin", "xiaoTian")
// .field("age", "9")
// .field("class", "三年级一班")
// .endObject()
// )
// .get();
// IndexResponse response4 = client.prepareIndex("index10", "x10", "4")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小花")
// .field("pinyin", "xiaoHua")
// .field("age", "9")
// .field("class", "三年级二班")
// .endObject()
// )
// .get();
//查询结果
GetResponse get1 = client.prepareGet("index10", "x10", "1").execute().actionGet();
GetResponse get2 = client.prepareGet("index10", "x10", "2").execute().actionGet();
GetResponse get3 = client.prepareGet("index10", "x10", "3").execute().actionGet();
System.out.println(get1.getSourceAsString());
System.out.println(get2.getSourceAsString());
System.out.println(get3.getSourceAsString());
//{"name":"小明","pinyin":"xiaoMing","age":"10","class":"四年级二班"}
//{"name":"小红","pinyin":"xiaoHong","age":"8","class":"二年级三班"}
//{"name":"小田","pinyin":"xiaoTian","age":"9","class":"三年级一班"}
//2.更新索引
UpdateRequest uRequest = new UpdateRequest();
uRequest.index("index10");
uRequest.type("x10");
uRequest.id("1");
uRequest.doc(jsonBuilder().startObject()
.field("name", "小张")
.field("pinyin", "xiaoZhang")
.field("age", "8")
.field("class", "二年级三班")
.endObject());
client.update(uRequest).get();
//查询结果
GetResponse get4 = client.prepareGet("index10", "x10", "1").execute().actionGet();
System.out.println(get4.getSourceAsString());
//3.删除索引
DeleteResponse dResponse = client.prepareDelete("index10", "x10", "4").execute().actionGet();
System.out.println("---------------"+dResponse);
//4.检索数据
QueryBuilder qb1= QueryBuilders.multiMatchQuery("年级","class");
QueryBuilder qb2= QueryBuilders.multiMatchQuery("小","name");
HighlightBuilder hiBuilder=new HighlightBuilder();
hiBuilder.preTags("<h2>");
hiBuilder.postTags("</h2>");
hiBuilder.field("name");
SearchResponse response = client.prepareSearch("index10").setTypes("x10")
.setQuery(qb2).highlighter(hiBuilder).execute()
.actionGet();
//获取查询结果集
SearchHits hits = response.getHits();
if (hits.totalHits() > 0) {
for (SearchHit hit : hits) {
System.out.println("------score:"+hit.getScore()+":\t"+hit.getSource());
//高亮
Text[] text = hit.getHighlightFields().get("name").getFragments();
for (Text str : text) {
System.out.println(str.string());
}
}
} else {
System.out.println("搜到0条结果");
}
client.close();
} catch (Exception e) {
e.printStackTrace();
}
log.info("-------------------------app main end");
}
}
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.net.InetAddress;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
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.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Elasticsearch JAVA API
* version:5.5.1
* @author wujianh
*
*/
public class Elasticsearch {
private static final Logger log=LoggerFactory.getLogger(Elasticsearch.class);
public static void main( String[] args )
{
log.info("-------------------------app main begin");
try {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.61.136"), 9300));
//jsonBuilder() --> import static org.elasticsearch.common.xcontent.XContentFactory.*;
//1.创建索引
// IndexResponse response1 = client.prepareIndex("index10", "x10", "1")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小明")
// .field("pinyin", "xiaoMing")
// .field("age", "10")
// .field("class", "四年级二班")
// .endObject()
// )
// .get();
//
// IndexResponse response2 = client.prepareIndex("index10", "x10", "2")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小红")
// .field("pinyin", "xiaoHong")
// .field("age", "8")
// .field("class", "二年级三班")
// .endObject()
// )
// .get();
// IndexResponse response3 = client.prepareIndex("index10", "x10", "3")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小田")
// .field("pinyin", "xiaoTian")
// .field("age", "9")
// .field("class", "三年级一班")
// .endObject()
// )
// .get();
// IndexResponse response4 = client.prepareIndex("index10", "x10", "4")
// .setSource(jsonBuilder()
// .startObject()
// .field("name", "小花")
// .field("pinyin", "xiaoHua")
// .field("age", "9")
// .field("class", "三年级二班")
// .endObject()
// )
// .get();
//查询结果
GetResponse get1 = client.prepareGet("index10", "x10", "1").execute().actionGet();
GetResponse get2 = client.prepareGet("index10", "x10", "2").execute().actionGet();
GetResponse get3 = client.prepareGet("index10", "x10", "3").execute().actionGet();
System.out.println(get1.getSourceAsString());
System.out.println(get2.getSourceAsString());
System.out.println(get3.getSourceAsString());
//{"name":"小明","pinyin":"xiaoMing","age":"10","class":"四年级二班"}
//{"name":"小红","pinyin":"xiaoHong","age":"8","class":"二年级三班"}
//{"name":"小田","pinyin":"xiaoTian","age":"9","class":"三年级一班"}
//2.更新索引
UpdateRequest uRequest = new UpdateRequest();
uRequest.index("index10");
uRequest.type("x10");
uRequest.id("1");
uRequest.doc(jsonBuilder().startObject()
.field("name", "小张")
.field("pinyin", "xiaoZhang")
.field("age", "8")
.field("class", "二年级三班")
.endObject());
client.update(uRequest).get();
//查询结果
GetResponse get4 = client.prepareGet("index10", "x10", "1").execute().actionGet();
System.out.println(get4.getSourceAsString());
//3.删除索引
DeleteResponse dResponse = client.prepareDelete("index10", "x10", "4").execute().actionGet();
System.out.println("---------------"+dResponse);
//4.检索数据
QueryBuilder qb1= QueryBuilders.multiMatchQuery("年级","class");
QueryBuilder qb2= QueryBuilders.multiMatchQuery("小","name");
HighlightBuilder hiBuilder=new HighlightBuilder();
hiBuilder.preTags("<h2>");
hiBuilder.postTags("</h2>");
hiBuilder.field("name");
SearchResponse response = client.prepareSearch("index10").setTypes("x10")
.setQuery(qb2).highlighter(hiBuilder).execute()
.actionGet();
//获取查询结果集
SearchHits hits = response.getHits();
if (hits.totalHits() > 0) {
for (SearchHit hit : hits) {
System.out.println("------score:"+hit.getScore()+":\t"+hit.getSource());
//高亮
Text[] text = hit.getHighlightFields().get("name").getFragments();
for (Text str : text) {
System.out.println(str.string());
}
}
} else {
System.out.println("搜到0条结果");
}
client.close();
} catch (Exception e) {
e.printStackTrace();
}
log.info("-------------------------app main end");
}
}