1.引入dependencyjava
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>1.4.0.M1</version> </dependency>
2:建立\添加索引web
{ "_index": "yananindex", "_type": "yanantype", "_id": "4", "_score": 1, "_source": { "name": "zhangyanan4", "gender": "male", "id": 4 } } , { "_index": "yananindex", "_type": "yanantype", "_id": "5", "_score": 1, "_source": { "name": "zhangyanan5", "gender": "male", "id": 5 } } , { "_index": "yananindex", "_type": "yanantype", "_id": "6", "_score": 1, "_source": { "name": "zhangyanan6", "gender": "male", "id": 6 } } , { "_index": "yananindex", "_type": "yanantype", "_id": "1", "_score": 1, "_source": { "name": "zhangyanan1", "gender": "female", "id": 1 } } , { "_index": "yananindex", "_type": "yanantype", "_id": "2", "_score": 1, "_source": { "name": "zhangyanan2", "gender": "female", "id": 2 } } , { "_index": "yananindex", "_type": "yanantype", "_id": "7", "_score": 1, "_source": { "name": "zhangyanan7", "gender": "male", "id": 7 } } , { "_index": "yananindex", "_type": "yanantype", "_id": "3", "_score": 1, "_source": { "name": "zhangyanan3", "gender": "female", "id": 3 } }
3.建立實體類spring
package testweb; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName="yananindex",type="yanantype") public class Yanan { private int id; private String name; private String gender; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
準備工做作完了,下面開始測試elasticsearch
4.queryForObject\queryForList\聚合查詢\統計數量\判斷索引是否存在\刪除指定id的索引
tcp
package testweb; import java.util.List; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ResultsExtractor; import org.springframework.data.elasticsearch.core.query.GetQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; public class TestES { static TransportClient transportClient; static ElasticsearchTemplate elasticsearchTemplate; public static void main(String[] args) { elasticsearchTemplate=getElasticsearchTemplate("localhost","elasticsearch_bak",9303); //queryForObject();//根據索引id查詢文檔 //queryForList(); //aggregationQuery();//聚合查詢 //count(); //indexExists(Yanan.class); deleteById(); } private static void deleteById() { // IndexQueryBuilder indexquery=new IndexQueryBuilder().withId("1").withObject(Yanan.class); //String delete = elasticsearchTemplate.delete("yananindex", "yanantype", "10");//索引的id String delete = elasticsearchTemplate.delete(Yanan.class, "10");//索引的id System.out.println(delete);//刪除的索引的id } private static void indexExists(Class<Yanan> clazz) { if(elasticsearchTemplate.indexExists(clazz)){ System.out.println("true"); }else{ System.out.println(false); } } private static void count() { NativeSearchQueryBuilder searchQuery=new NativeSearchQueryBuilder(); BoolQueryBuilder bqb = QueryBuilders.boolQuery(); bqb.must(QueryBuilders.termQuery("gender","female")); searchQuery.withIndices("yananindex").withQuery(bqb); long count = elasticsearchTemplate.count(searchQuery.build()); System.out.println(count); } private static void aggregationQuery() { TermsBuilder termsBuilder = AggregationBuilders.terms("maleCount").field("name").size(10); NativeSearchQueryBuilder searchQuery=new NativeSearchQueryBuilder(); BoolQueryBuilder bqb = QueryBuilders.boolQuery(); bqb.must(QueryBuilders.termQuery("gender","male")); searchQuery.withIndices("yananindex").withQuery(bqb).addAggregation(termsBuilder); Aggregations aggregations = elasticsearchTemplate.query(searchQuery.build(), new ResultsExtractor<Aggregations>() { @Override public Aggregations extract(SearchResponse response) { return response.getAggregations(); } }); StringTerms modelTerms = (StringTerms)aggregations.asMap().get("maleCount"); List<Bucket> buckets = modelTerms.getBuckets(); for(int i=0;i<buckets.size();i++){ System.out.println(buckets.get(i).getKey()+"\t"+buckets.get(i).getDocCount()); } } private static void queryForList() { NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder(); BoolQueryBuilder bqb = QueryBuilders.boolQuery(); bqb.must(QueryBuilders.termQuery("gender","male")); searchQuery.withIndices("yananindex").withQuery(bqb); List<Yanan> list = elasticsearchTemplate.queryForList(searchQuery.build(), Yanan.class); for(int i=0;i<list.size();i++){ Yanan yanan = list.get(i); System.out.println(yanan.getName()+"\t"+yanan.getGender()+"\t"+yanan.getId()); } } private static void queryForObject() { GetQuery query = new GetQuery(); query.setId("1");// 索引的id Yanan yanan = elasticsearchTemplate.queryForObject(query, Yanan.class); System.out.println(yanan.getGender()); System.out.println(yanan.getName()); System.out.println(yanan.getId());// 實體的id } static ElasticsearchTemplate getElasticsearchTemplate(String ip,String clusterName,int port){//<a href="http://blog.csdn.net/ty4315/article/details/52434296" target="_blank">@參考文章1</a>,<a href="http://blog.csdn.net/ljc2008110/article/details/48630609" target="_blank">@參考文章2</a> Settings setting = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName)// 指定集羣名稱 .put("client.transport.sniff", true)// 啓動嗅探功能,客戶端去嗅探整個集羣的狀態,把集羣中其它機器的ip地址加到客戶端中,這樣作的好處是通常你不用手動設置集羣裏全部集羣的ip到鏈接客戶端,它會自動幫你添加,而且自動發現新加入集羣的機器。 .build(); // 建立客戶端 經過setting來建立,若不指定則默認連接的集羣名爲elasticsearch 連接使用tcp協議即9303 transportClient = new TransportClient(setting); TransportAddress transportAddress = new InetSocketTransportAddress(ip, port); transportClient.addTransportAddresses(transportAddress); elasticsearchTemplate = new ElasticsearchTemplate(transportClient); return elasticsearchTemplate; } }