package com.tanpu.community.service.base; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.elasticsearch.client.*; import javax.annotation.PostConstruct; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Slf4j @Service public class ESHelper { @Value("${es.index}") private String esIndex; @Autowired private RestHighLevelClient client; public void insert(String type, String id, String json) { try { IndexRequest req = new IndexRequest(esIndex); if (StringUtils.isNotBlank(id)) { req.id(type + "_" + id); } req.source(json, XContentType.JSON); IndexResponse resp = client.index(req, RequestOptions.DEFAULT); validStatus(resp.status(), RestStatus.CREATED, RestStatus.OK); } catch (IOException e) { log.error("ES Helper error:{}", ExceptionUtils.getStackTrace(e)); } } public void insert(String type, String id, Map<String, Object> data) { try { IndexRequest req = new IndexRequest(esIndex); if (StringUtils.isNotBlank(id)) { req.id(type + "_" + id); } req.source(data); IndexResponse resp = client.index(req, RequestOptions.DEFAULT); validStatus(resp.status(), RestStatus.OK); } catch (IOException e) { log.error("ES Helper error:{}", ExceptionUtils.getStackTrace(e)); } } public SearchHit[] selectLike(SearchSourceBuilder builder) { try { SearchRequest req = new SearchRequest(esIndex); req.source(builder); SearchResponse resp = client.search(req, RequestOptions.DEFAULT); validStatus(resp.status(), RestStatus.OK); return resp.getHits().getHits(); } catch (IOException e) { log.error("ES Helper error:{}", ExceptionUtils.getStackTrace(e)); } return new SearchHit[]{}; } private void validStatus(RestStatus status, RestStatus... expect) { for (RestStatus ex : expect) { if (ex == status) return; } log.error("ES Helper fail! status:{}", status.toString()); throw new RuntimeException("ES fail"); } public static void main(String[] args) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("1", "2")); RestClientBuilder builder = RestClient.builder(new HttpHost("42.194.224.208", 9200)) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); RestHighLevelClient highClient = new RestHighLevelClient(builder); ESHelper helper = new ESHelper(); helper.client = highClient; System.out.println("insert"); Map<String, Object> map = new HashMap<>(); map.put("name", "太阳44444444444444"); map.put("context", "这里有一个小太阳444444444"); // helper.insert("test_index", "", "2", map); SearchSourceBuilder search = new SearchSourceBuilder(); BoolQueryBuilder boolQb = QueryBuilders.boolQuery(); MatchQueryBuilder matchQb = QueryBuilders.matchQuery("textContent", "小星星"); boolQb.must(matchQb); // String[] includes = new String[]{"id"}; // String[] excludes = new String[]{}; search.query(boolQb).from(0).size(50); SearchHit[] hits = helper.selectLike(search); System.out.println(hits.length); for (SearchHit hit : hits) { System.out.println(hit.toString()); System.out.println(hit.getFields()); } System.out.println("done"); } }