标签:har shard rms next net -- ast size pos
一、引入包
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.4</version> </dependency>
二、创建索引
@Test public void creatIndex() throws UnknownHostException { //1、创建一个Setting对象,相当于是一个配置信息,主要配置集群的名称, Settings settings = Settings.builder() .put("cluster.name", "my-elasticsearch").build(); //2、创建一个客户端client对象,内部通过tcp协议,所以使用93 TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9301)); client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9301)); client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9303)); //3、使用client对象创建一个索引 client.admin().indices().prepareCreate("index_hello").get(); //4、关闭client对象 client.close(); }
三、创建mapping(创建一个Mappping信息,应该是一个json数据,可以是字符串,也可以是XContentBuilder对象)
@Test
public void creatMapping() throws IOException {
TransportClient transportClient=ElasticSearchClientTest.creatClient();
IndicesAdminClient client= ElasticSearchClientTest.creatClient().admin().indices();
client.prepareCreate("twitter")
.setSettings(Settings.builder()
.put("index.number_of_shards", 5)
.put("index.number_of_replicas", 1)
)
.get();
/* client.preparePutMapping("twitter").setType("user")
.setSource("{\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();*/
XContentBuilder xc=XContentFactory.jsonBuilder();
xc.startObject()
.startObject("properties")
.startObject("id")
.field("type","long")
.field("store",true)
.endObject()
.startObject("name")
.field("type","text")
.field("store",true)
.field("index",true)
.field("analyzer","ik_smart")
.endObject()
.startObject("content")
.field("type","text")
.field("store",true)
.field("index",true)
.field("analyzer","ik_smart")
.endObject()
.endObject()
.endObject();
client.preparePutMapping("twitter").setType("article").setSource(xc).get();
transportClient.close();
}
四、创建document
/** * 使用Map添加文档 * IndexResponse response = client.prepareIndex("twitter", "tweet") * .setSource(json, XContentType.JSON) * .get(); */ @Test public void creatDocument() throws UnknownHostException { Map<String, Object> map = new HashMap<String, Object>(); map.put("id",100); map.put("name","cli_java"); map.put("content","Map is a key:values pair collection. It represents a JSON structure分布式搜索elasticsearch配置文件详解 - 关于代码的那"); TransportClient transportClient=ElasticSearchClientTest.creatClient(); //IndicesAdminClient client= ElasticSearchClientTest.creatClient().admin().indices();
IndexResponse response=transportClient.prepareIndex("twitter", "article") .setSource(map, XContentType.JSON) .get();
transportClient.close();
}
五、查询
@Test public void getIndex() throws UnknownHostException,IOException { TransportClient transportClient=ElasticSearchClientTest.creatClient(); //<!--索引库查询-根据id查询--> /*GetResponse response =transportClient.prepareGet("twitter","article","ER_Lpm4Bob3tI_qjogjc").get(); Map<String,Object> map=response.getSource(); System.out.println(map.get("content"));*/ //<!--结束索引库查询-根据id查询--> //第二种创建一个查询对象 IdsQueryBuilder queryBuilder=QueryBuilders.idsQuery().addIds("ER_Lpm4Bob3tI_qjogjc","Eh_gpm4Bob3tI_qjggjb"); //执行查询 SearchResponse searchResponse=transportClient.prepareSearch("twitter") .setTypes("article") .setQuery(queryBuilder) .get(); SearchHits searchHits=searchResponse.getHits(); Iterator<SearchHit> iterator= searchHits.iterator(); while(iterator.hasNext()){ SearchHit searchHit=iterator.next(); System.out.println(searchHit.getSourceAsString()); Map<String, Object> map=searchHit.getSourceAsMap(); System.out.println(map.get("content")); } transportClient.close(); //结束第二种方式创建查询对象 }
//条件查询
@Test public void getIndex() throws UnknownHostException,IOException { TermQueryBuilder qb=QueryBuilders.termQuery("name","cli_java"); ElasticSearchClientTest.useTermGetIndex(qb); }
@Test
public void testQueryString() throws UnknownHostException {
QueryStringQueryBuilder qsq=QueryBuilders.queryStringQuery("独立寒秋,湘江北去,万里雪飘").defaultField("content");
ElasticSearchClientTest.useTermGetIndex(qsq);
}
private static void useTermGetIndex(TermQueryBuilder queryBuilder) throws UnknownHostException {
TransportClient transportClient=ElasticSearchClientTest.creatClient();
//执行条件查询
SearchResponse searchResponse=transportClient.prepareSearch("twitter")
.setTypes("article")
.setQuery((org.elasticsearch.index.query.QueryBuilder) queryBuilder)
//分页信息
//.setFrom(start)
//.setSize(pageSize)
.get();
SearchHits searchHits=searchResponse.getHits();
Iterator<SearchHit> iterator= searchHits.iterator();
while(iterator.hasNext()){
SearchHit searchHit=iterator.next();
System.out.println(searchHit.getSourceAsString());
Map<String, Object> map=searchHit.getSourceAsMap();
System.out.println(map.get("content"));
}
transportClient.close();
}
//高亮查询
@Test public void testQueryFormSize() throws UnknownHostException { QueryStringQueryBuilder qsq=QueryBuilders.queryStringQuery("庆余年").defaultField("name"); for(int i=1;i<111;i+=10){ ElasticSearchClientTest.useTermGetIndex(qsq,i,10); System.out.println("-------------------------------------------------------------------"); } } private static void useTermGetIndex(QueryStringQueryBuilder queryBuilder,int start,int pageSize) throws UnknownHostException { //高亮设置 HighlightBuilder hb=new HighlightBuilder(); hb.field("name"); hb.preTags("<em>"); hb.postTags("</em>"); TransportClient transportClient=ElasticSearchClientTest.creatClient(); //第二种创建一个查询对象 //执行查询 SearchResponse searchResponse=transportClient.prepareSearch("twitter") .setTypes("article") .setQuery((org.elasticsearch.index.query.QueryBuilder) queryBuilder) //分页信息 .setFrom(start) .setSize(pageSize) //高亮查询 .highlighter(hb) .get(); SearchHits searchHits=searchResponse.getHits(); Iterator<SearchHit> iterator= searchHits.iterator(); while(iterator.hasNext()){ SearchHit searchHit=iterator.next(); System.out.println(searchHit.getSourceAsString()); Map<String, Object> map=searchHit.getSourceAsMap(); System.out.println(map.get("content")); System.out.println("**************************************************"); Map<String, HighlightField> m=searchHit.getHighlightFields(); System.out.println(m.get("name")); }
标签:har shard rms next net -- ast size pos
原文地址:https://www.cnblogs.com/ffzzcommsoft/p/11937091.html