返回客户端类(基于TransportClient)
@Configuration("meta")
public class ConfigBeans {
@Value("${es_name}")
private String es_name;
@Value("#{'${es_ip}'.split(',')}")
private List<String> es_ip;
@Value("${es_port}")
private Integer es_port;
private TransportClient client = null;
@Bean
@Lazy
public TransportClient esClient(){
if(es_name != null && !"".equals(es_name)){
Settings settings = Settings.builder().put("cluster.name", es_name).build();
client = new PreBuiltTransportClient(settings);
}else{
client = new PreBuiltTransportClient(Settings.EMPTY);
}
try {
for (String ip : es_ip) {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), es_port));
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
return client;
}
}
增删改查类
import com.alibaba.druid.util.Histogram;
import com.alibaba.fastjson.JSONObject;
import com.y2game.dubbo.service.EsService;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
import org.elasticsearch.search.aggregations.bucket.nested.ReverseNested;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Auther: chenjiale
* @Date: 2018/9/11 11:07
* @Description:
*/
@Service
public class EsServiceImpl implements EsService {
@Autowired
private TransportClient transportClient;
@Override
public List<Map<String, Object>> searchNews(String keyword) {
SearchResponse searchResponse = null;
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("lianjia").setTypes("house_data");
if (keyword != null && !"".equals(keyword)) {//关键字
boolQueryBuilder = boolQueryBuilder.should(QueryBuilders.matchQuery("title", keyword).boost(5.0f)).
should(QueryBuilders.termsQuery("houseType", keyword).boost(5.0f))
.should(QueryBuilders.termsQuery("rentType", keyword).boost(5.0f))
.should(QueryBuilders.matchQuery("subway", keyword).boost(2.0f));
}
//过滤
if (keyword != null && !"".equals(keyword)) {//关键字
searchResponse = searchRequestBuilder
.setQuery(boolQueryBuilder)
.setSearchType(SearchType.QUERY_THEN_FETCH)//设置查询类型
.setExplain(true)// 设置是否按查询匹配度排序
.setFrom(1).setSize(10)//分页
.execute().actionGet();
} else {
searchResponse = searchRequestBuilder
.setQuery(boolQueryBuilder)
.addSort(SortBuilders.fieldSort("publish_time.keyword").order(SortOrder.DESC))
.setSearchType(SearchType.QUERY_THEN_FETCH)//设置查询类型
.setExplain(true)// 设置是否按查询匹配度排序
.setFrom(1).setSize(10)//分页
.execute().actionGet();
}
long totalHits = searchResponse.getHits().getTotalHits();
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
SearchHit[] hits = searchResponse.getHits().getHits();
System.out.println("size " + totalHits);
for (SearchHit s : hits) {
results.add(s.getSource());
System.out.println(s.getHighlightFields());
Map<String, HighlightField> highlightFields = s.getHighlightFields();
HighlightField highlightField = highlightFields.get("about");
System.out.println("高亮字段:" + highlightField.getName() + "\n 高亮部分内容:" + highlightField.getFragments()[0].string());
}
Nested nested = searchResponse.getAggregations().get("negsted_path");
System.out.println(nested.getDocCount());
ReverseNested reverseNested = nested.getAggregations().get("blogposts");
System.out.println(reverseNested);
/* Terms by_key = reverseNested.getAggregations().get("tags");
List<Terms.Bucket> buckets2 = (List<Terms.Bucket>) by_key.getBuckets();
for (Terms.Bucket bucket : buckets2) {
String id = bucket.getKeyAsString();
Long count = bucket.getDocCount();
System.out.println("id " + id + " count" + count);
}*/
/*Terms by_key = searchResponse.getAggregations().get("sales");
System.out.println(by_key);
System.out.println("sum+=" + sum + " max" + by_key.getMax());
List<Terms.Bucket> buckets = (List<Terms.Bucket>) by_key.getBuckets();
for (Terms.Bucket bucket : buckets) {
String id = bucket.getKeyAsString();
Long count = bucket.getDocCount();
}*/
String a = searchResponse.getScrollId();
int count = 0;
while (true) {
SearchResponse response2 = transportClient.prepareSearchScroll(a).setScroll(new TimeValue(60000)).execute().actionGet();
SearchHit[] hits2 = response2.getHits().getHits();
System.out.println(hits2.length);
if (hits2.length == 0) {
break;
}
for (SearchHit s : hits2) {
System.out.println("count:" + count + " " + s.getSourceAsString() + "index:" + s.getIndex() + "type:" + s.getType());
}
count++;
a = response2.getScrollId();
System.out.println(a);
}
return results;
}
@Override
public Boolean add() {
List<Map<String, Object>> infoMap = new ArrayList<>();
BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
try {
IndexRequestBuilder indexRequestBuilder = transportClient.prepareIndex("test", "require");
int i = 0;
for (Map<String, Object> map : infoMap) {
indexRequestBuilder.setSource(map).execute().actionGet();
bulkRequest.add(transportClient.prepareIndex("hut_paper", "paper").setSource(map));
indexRequestBuilder.setSource(map).execute().actionGet();
if (i % 500 == 0) {
bulkRequest.execute().actionGet();
System.out.println("执行。。");
}
}
bulkRequest.execute().actionGet();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public Boolean addMapping(String index, String type) { //先创建空索引库
transportClient.admin().indices().prepareCreate(index).execute().actionGet();
try {
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject(index).startObject("properties").startObject("title").field("type", "string").field("store", "yes").endObject().startObject("description").field("type", "string").field("index", "not_analyzed").endObject().startObject("price").field("type", "double").endObject().startObject("onSale").field("type", "boolean").endObject().startObject("type").field("type", "integer").endObject().startObject("createDate").field("type", "date").endObject().endObject().endObject().endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest(index).type(type).source(mapping);
transportClient.admin().indices().putMapping(mappingRequest).actionGet();
} catch (IOException e) {
return false;
}
return true;
}
@Override
public Boolean deleteDocument(String id) {
try {
DeleteResponse response = transportClient.prepareDelete("chenjiale", "chenjiale", id).get();
String index = response.getIndex();
String type = response.getType();
String id2 = response.getId();
long version = response.getVersion();
System.out.println("index:" + index + " type: " + type + " id:" + id2 + " version:" + version + " status:" + response.status());
return true;
} catch (Exception e) {
return false;
}
}
@Override
public Boolean updateDocument(String id) {
try {
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject();
JSONObject obj = new JSONObject();
for (String key : obj.keySet()) {
xContentBuilder.field(key, obj.getJSONObject(key)); //便利map,多行更新
}
xContentBuilder.endObject();
UpdateResponse response = transportClient.update(new UpdateRequest("chenjiale", "chenjiale", id).doc(xContentBuilder)).get();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}