import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
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.elasticsearch.client.transport.TransportClient;
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;
/**
* @Author day_ue
* @Date 2019/1/23 22:23
* @Description TODO
**/
public class TransClient {
private TransportClient client;
@Before
public void getClient() throws UnknownHostException {
//Setting setting = new Settings(new TreeMap<>().put("cluster.name", "myes"), new SetOnce());
//上面的方法不可取,因为这个方法是非公共的方法,
Settings setting = Settings.builder().put("cluster.name", "myes").build();
PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(setting);
InetAddress node01 = InetAddress.getByName("node01");
TransportAddress transportAddress = new TransportAddress(node01,9300);
client = preBuiltTransportClient.addTransportAddress(transportAddress);
}
//添加索引方式一,自己组装jsoN格式
@Test
public void addIndex(){
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"travelying out Elasticsearch\"" +
"}";
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("myindex1", "article", "1");
indexRequestBuilder.setSource(json, XContentType.JSON).get();
}
//map方式添加索引
@Test
public void addIndex2(){
HashMap<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put("name", "zhangsan");
jsonMap.put("sex", "1");
jsonMap.put("age", "18");
jsonMap.put("address", "bj");
client.prepareIndex("myindex1","article","2").setSource(jsonMap).get();
}
//xcontentbuilder创建索引
@Test
public void addIndex3() throws IOException {
client.prepareIndex("myindex1","article","3").setSource(
new XContentFactory().jsonBuilder()
.startObject()
.field("name", "lisi")
.field("age", "18")
.field("sex", "0")
.field("address", "bj")
.endObject()
).get();
}
//将对象转化为json字符串创建索引
@Test
public void addIndex4(){
Person person = new Person();
person.setAddress("北京");
person.setAge(18);
person.setEmail("hello@163.com");
person.setName("zhangsanfeng");
person.setId(5);
String string = JSONObject.toJSONString(person);
client.prepareIndex("myindex1","article","4").setSource(string,XContentType.JSON).get();
}
//批量添加索引
@Test
public void addAllIndex5(){
HashMap<String, String> jsonMap1 = new HashMap<String, String>();
jsonMap1.put("name", "zhangsan");
jsonMap1.put("sex", "1");
jsonMap1.put("age", "18");
jsonMap1.put("address", "bj");
HashMap<String, String> jsonMap2 = new HashMap<String, String>();
jsonMap2.put("name", "lisi");
jsonMap2.put("sex", "1");
jsonMap2.put("age", "18");
jsonMap2.put("address", "bj");
HashMap<String, String> jsonMap3 = new HashMap<String, String>();
jsonMap3.put("name", "wangwu");
jsonMap3.put("sex", "1");
jsonMap3.put("age", "18");
jsonMap3.put("address", "bj");
BulkRequestBuilder bulk = client.prepareBulk();
IndexRequestBuilder source1 = client.prepareIndex("myindex1", "article", "5").setSource(jsonMap1);
IndexRequestBuilder source2 = client.prepareIndex("myindex1", "article", "6").setSource(jsonMap2);
IndexRequestBuilder source3 = client.prepareIndex("myindex1", "article", "7").setSource(jsonMap3);
bulk.add(source1).add(source2).add(source3).get();
}
//==============================================以上是插入数据======================================================================
@Test
public void createIndexBatch() throws IOException {
TransportClient client = new PreBuiltTransportClient(Settings.builder()
.put("cluster.name", "myes")
.put("client.transport.sniff", "true")
.build()
).addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));
//创建映射
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
// .startObject("m_id").field("type","keyword").endObject()
.startObject("id").field("type", "integer").endObject()
.startObject("name").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("age").field("type", "integer").endObject()
.startObject("sex").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("address").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("phone").field("type", "text").endObject()
.startObject("email").field("type", "text").endObject()
.startObject("say").field("type", "text").field("analyzer", "ik_max_word").endObject()
.endObject()
.endObject();
PutMappingRequest source = Requests.putMappingRequest("indexsearch").type("mysearch").source(mapping);
//创建索引
client.admin().indices().prepareCreate("indexsearch").execute().actionGet();
//为索引添加映射
client.admin().indices().putMapping(source).actionGet();
BulkRequestBuilder bulk = client.prepareBulk();
Person lujunyi = new Person(2, "玉麒麟卢俊义", 28, 1, "水泊梁山", "17666666666", "lujunyi@itcast.com","hello world今天天气还不错");
Person wuyong = new Person(3, "智多星吴用", 45, 1, "水泊梁山", "17666666666", "wuyong@itcast.com","行走四方,抱打不平");
Person gongsunsheng = new Person(4, "入云龙公孙胜", 30, 1, "水泊梁山", "17666666666", "gongsunsheng@itcast.com","走一个");
Person guansheng = new Person(5, "大刀关胜", 42, 1, "水泊梁山", "17666666666", "wusong@itcast.com","我的大刀已经饥渴难耐");
Person linchong = new Person(6, "豹子头林冲", 18, 1, "水泊梁山", "17666666666", "linchong@itcast.com","梁山好汉");
Person qinming = new Person(7, "霹雳火秦明", 28, 1, "水泊梁山", "17666666666", "qinming@itcast.com","不太了解");
Person huyanzhuo = new Person(8, "双鞭呼延灼", 25, 1, "水泊梁山", "17666666666", "huyanzhuo@itcast.com","不是很熟悉");
Person huarong = new Person(9, "小李广花荣", 50, 1, "水泊梁山", "17666666666", "huarong@itcast.com","打酱油的");
Person chaijin = new Person(10, "小旋风柴进", 32, 1, "水泊梁山", "17666666666", "chaijin@itcast.com","吓唬人的");
Person zhisheng = new Person(13, "花和尚鲁智深", 15, 1, "水泊梁山", "17666666666", "luzhisheng@itcast.com","倒拔杨垂柳");
Person wusong = new Person(14, "行者武松", 28, 1, "水泊梁山", "17666666666", "wusong@itcast.com","二营长。。。。。。");
bulk.add(client.prepareIndex("indexsearch", "mysearch", "1")
.setSource(JSONObject.toJSONString(lujunyi), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "2")
.setSource(JSONObject.toJSONString(wuyong), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "3")
.setSource(JSONObject.toJSONString(gongsunsheng), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "4")
.setSource(JSONObject.toJSONString(guansheng), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "5")
.setSource(JSONObject.toJSONString(linchong), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "6")
.setSource(JSONObject.toJSONString(qinming), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "7")
.setSource(JSONObject.toJSONString(huyanzhuo), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "8")
.setSource(JSONObject.toJSONString(huarong), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "9")
.setSource(JSONObject.toJSONString(chaijin), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "10")
.setSource(JSONObject.toJSONString(zhisheng), XContentType.JSON)
);
bulk.add(client.prepareIndex("indexsearch", "mysearch", "11")
.setSource(JSONObject.toJSONString(wusong), XContentType.JSON)
);
bulk.get();
client.close();
}
//通过系统id进行精确查询
@Test
public void searchById(){
GetResponse response = client.prepareGet("indexsearch", "mysearch", "2").get();
String string = response.getSourceAsString();
System.out.println(string);
}
//查询所有数据
@Test
public void searchAllData(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(new MatchAllQueryBuilder()).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
}
}
//分词查询
@Test
public void searchData(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(new MatchQueryBuilder("say", "大刀饥渴")).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
}
}
//词条查询
@Test
public void termQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(new TermQueryBuilder("say", "营长")).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
}
}
//按照范围查询
@Test
public void rangeQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(new RangeQueryBuilder("age").gt(15).lt(30)).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
}
}
//通配符查询
@Test
public void wildCardQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.wildcardQuery("say", "hell*")).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
}
//模糊查询
@Test
public void fuzzyQuery(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.fuzzyQuery("say", "hel").fuzziness(Fuzziness.TWO)).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
}
/**
* 多条件组合查询
* 查询年龄是18到28范围内且性别是男性的,或者id范围在10到13范围内的 nvxing
* @param
*/
@Test
public void conditionQuery(){
RangeQueryBuilder age = QueryBuilders.rangeQuery("age").gte(18).lte(30);
TermQueryBuilder sex = QueryBuilders.termQuery("sex", 1);
RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gte(5).lte(12);
BoolQueryBuilder should = QueryBuilders.boolQuery().must(age).must(sex).should(id);
// BoolQueryBuilder filter = should.filter(QueryBuilders.matchQuery("say", "天"));
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(should).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
}
//分页查询
@Test
public void fenye(){
int pageSize = 5;
int pageNum = 3;
int starNum = (pageNum -1)*pageSize;
SearchResponse searchResponse = client.prepareSearch("indexsearch")
.setTypes("mysearch")
.setQuery(QueryBuilders.matchAllQuery())
.addSort("id", SortOrder.DESC)
.setFrom(starNum)
.setSize(pageSize).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
System.out.println(documentFields.getSourceAsString());
}
}
//高亮展示
@Test
public void highLighting(){
SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch")
.setQuery(QueryBuilders.termQuery("say", "hello"))
.highlighter(new HighlightBuilder()
.field("say")
.preTags("<font style='color:red'>")
.postTags("</font>")
).get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
Text[] says = documentFields.getHighlightFields().get("say").getFragments();
for (Text say : says) {
System.out.println(say.toString());
}
}
}
/**
* 更新索引的操作,我们只能通过系统的id,然后找到我们的数据进行更细或者插入的操作
* @param
*/
@Test
public void updateIndex(){
Person songjiang = new Person(5, "宋江", 88, 0, "水泊梁山", "17666666666", "wusong@itcast.com","及时雨宋江");
String jsonString = JSONObject.toJSONString(songjiang);
client.prepareUpdate("indexsearch","mysearch","3").setDoc(jsonString,XContentType.JSON).get();
}
/**
* 删除数据 按照系统的id进行删除
* @param
*/
@Test
public void deleteData(){
client.prepareDelete("indexsearch","mysearch","3").get();
}
/**
* 按照我们查询条件离开进行删除
* @param
*/
@Test
public void deleteByCond(){
BulkByScrollResponse bulkDelete = DeleteByQueryAction.INSTANCE
.newRequestBuilder(client)
.filter(QueryBuilders.rangeQuery("id").gt(1).lt(3))
.source("indexsearch").get();
long deleted = bulkDelete.getDeleted();
System.out.println(deleted);
}
/**
* 删除索引
* 删除整个索引库
*/
@Test
public void deleteIndex(){
DeleteIndexResponse indexsearch = client.admin().indices()
.prepareDelete("indexsearch")
.execute().actionGet();
client.close();
}
@After
public void closeClient(){
client.close();
}
}