ESHelper.java 6.26 KB
Newer Older
张辰's avatar
张辰 committed
1 2
package com.tanpu.community.service.base;

刘基明's avatar
刘基明 committed
3
import com.tanpu.common.util.JsonUtil;
张辰's avatar
张辰 committed
4 5 6 7 8 9 10 11 12
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;
刘基明's avatar
刘基明 committed
13 14
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
张辰's avatar
张辰 committed
15 16 17 18
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
刘基明's avatar
刘基明 committed
19 20 21 22
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
张辰's avatar
张辰 committed
23 24
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
张辰's avatar
张辰 committed
25
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
张辰's avatar
张辰 committed
26 27 28 29 30
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;
张辰's avatar
张辰 committed
31
import org.springframework.beans.factory.annotation.Value;
张辰's avatar
张辰 committed
32 33 34 35 36 37 38 39 40
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Service
public class ESHelper {

张辰's avatar
张辰 committed
41 42 43
    @Value("${es.index}")
    private String esIndex;

张辰's avatar
张辰 committed
44 45 46 47
    @Autowired
    private RestHighLevelClient client;


张辰's avatar
张辰 committed
48
    public void insert(String type, String id, String json) {
张辰's avatar
张辰 committed
49
        try {
张辰's avatar
张辰 committed
50
            IndexRequest req = new IndexRequest(esIndex);
张辰's avatar
张辰 committed
51 52 53 54 55 56
            if (StringUtils.isNotBlank(id)) {
                req.id(type + "_" + id);
            }

            req.source(json, XContentType.JSON);
            IndexResponse resp = client.index(req, RequestOptions.DEFAULT);
张辰's avatar
张辰 committed
57
            validStatus(resp.status(), RestStatus.CREATED, RestStatus.OK);
张辰's avatar
张辰 committed
58 59 60 61 62
        } catch (IOException e) {
            log.error("ES Helper error:{}", ExceptionUtils.getStackTrace(e));
        }
    }

张辰's avatar
张辰 committed
63
    public void insert(String type, String id, Map<String, Object> data) {
张辰's avatar
张辰 committed
64
        try {
张辰's avatar
张辰 committed
65
            IndexRequest req = new IndexRequest(esIndex);
张辰's avatar
张辰 committed
66 67 68 69 70 71
            if (StringUtils.isNotBlank(id)) {
                req.id(type + "_" + id);
            }

            req.source(data);
            IndexResponse resp = client.index(req, RequestOptions.DEFAULT);
张辰's avatar
张辰 committed
72
            validStatus(resp.status(), RestStatus.OK);
张辰's avatar
张辰 committed
73 74 75 76 77
        } catch (IOException e) {
            log.error("ES Helper error:{}", ExceptionUtils.getStackTrace(e));
        }
    }

张辰's avatar
张辰 committed
78
    public SearchHit[] selectLike(SearchSourceBuilder builder) {
张辰's avatar
张辰 committed
79
        try {
张辰's avatar
张辰 committed
80
            SearchRequest req = new SearchRequest(esIndex);
张辰's avatar
张辰 committed
81 82 83 84 85 86 87 88 89 90 91 92 93
            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[]{};
    }


张辰's avatar
张辰 committed
94 95 96
    private void validStatus(RestStatus status, RestStatus... expect) {
        for (RestStatus ex : expect) {
            if (ex == status) return;
张辰's avatar
张辰 committed
97
        }
张辰's avatar
张辰 committed
98 99
        log.error("ES Helper fail! status:{}", status.toString());
        throw new RuntimeException("ES fail");
张辰's avatar
张辰 committed
100 101 102
    }


刘基明's avatar
刘基明 committed
103 104 105 106 107 108 109 110 111
    public void delete(String type, String id) {
        try {
            DeleteRequest req = new DeleteRequest(esIndex, type + "_" + id);
            DeleteResponse resp = client.delete(req, RequestOptions.DEFAULT);
            validStatus(resp.status(), RestStatus.OK);
        } catch (IOException e) {
            log.error("ES Helper error:{}", ExceptionUtils.getStackTrace(e));
        }
    }
张辰's avatar
张辰 committed
112 113 114


    public static void main(String[] args) {
张辰's avatar
张辰 committed
115 116 117
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("1", "2"));

张辰's avatar
张辰 committed
118 119 120 121
        RestClientBuilder builder = RestClient.builder(new HttpHost("42.194.224.208", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
张辰's avatar
张辰 committed
122
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
张辰's avatar
张辰 committed
123 124 125 126 127 128 129 130 131 132 133 134
                    }
                });

        RestHighLevelClient highClient = new RestHighLevelClient(builder);

        ESHelper helper = new ESHelper();
        helper.client = highClient;


        SearchSourceBuilder search = new SearchSourceBuilder();

        BoolQueryBuilder boolQb = QueryBuilders.boolQuery();
张辰's avatar
张辰 committed
135 136 137 138 139 140 141 142 143

        String[] ks = new String[]{"左侧", "五粮液"};
        for (String k : ks) {
            MatchPhraseQueryBuilder contentQb = QueryBuilders.matchPhraseQuery("textContent", k);
            MatchPhraseQueryBuilder titleQb = QueryBuilders.matchPhraseQuery("title", k);
            boolQb.should(contentQb);
            boolQb.should(titleQb);
        }

张辰's avatar
张辰 committed
144

张辰's avatar
张辰 committed
145 146 147
//        String[] includes = new String[]{"id"};
//        String[] excludes = new String[]{};
        search.query(boolQb).from(0).size(50);
张辰's avatar
张辰 committed
148 149


刘基明's avatar
刘基明 committed
150 151 152 153 154
        try {

            DeleteRequest req1 = new DeleteRequest("new-community", "2_88505497717006377");
            DeleteResponse resp1 = helper.client.delete(req1, RequestOptions.DEFAULT);
            System.out.println(JsonUtil.toJson(resp1));
张辰's avatar
张辰 committed
155

刘基明's avatar
刘基明 committed
156 157 158
            SearchRequest req = new SearchRequest("new-community");
            req.source(search);
            SearchResponse resp = helper.client.search(req, RequestOptions.DEFAULT);
张辰's avatar
张辰 committed
159

刘基明's avatar
刘基明 committed
160 161 162 163 164 165 166
            SearchHit[] hits = resp.getHits().getHits();
            System.out.println(hits.length);
            for (SearchHit hit : hits) {
                System.out.println(hit.toString());
                System.out.println(hit.getFields());
            }
        } catch (Exception e) {
167
//            e.printStackTrace();
刘基明's avatar
刘基明 committed
168
        }
张辰's avatar
张辰 committed
169

刘基明's avatar
刘基明 committed
170
        System.out.println("done");
张辰's avatar
张辰 committed
171 172 173 174
    }


}