ElastickSearch集成Springboot代码

欢迎使用Markdown编辑器

pom配置

 <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.14.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

xml配置

server:
  port: 80

elasticsearch:
  rest:
    hosts:
      - ip: 127.0.0.1
        port: 9200
    username:
    password:

(config) es客户端配置:

@Data
@ConfigurationProperties(prefix = "elasticsearch.rest")
@Configuration
public class ElastickSearchConfig {
    private String username;

    private String password;

    private List<Host> hosts;

    /**
     * 超时时间设为5分钟
     */
    private static final int TIME_OUT = 5 * 60 * 1000;
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

        HttpHost[] httpHosts = new HttpHost[hosts.size()];
        for (int i = 0; i < httpHosts.length; i++) {
            Host host = hosts.get(i);
            httpHosts[i] = new HttpHost(host.getIp(), host.getPort(), host.getSchema());
        }
        RestClientBuilder builder = RestClient.builder(httpHosts)
                .setHttpClientConfigCallback(
                        httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        RestHighLevelClient restHighLevelClient=new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
        return restHighLevelClient;
      //   return new RestHighLevelClient(builder);
    }
    @Data
    public static class Host {
        private String ip;
        private int port;
        private String schema = "http";
    }

}

测试类,(es索引)

 @Qualifier("elasticsearchClient")
    @Autowired
    private RestHighLevelClient client;



    // 创建索引
    @Test
    void createIndex() throws IOException {

        CreateIndexRequest request = new CreateIndexRequest("jd_goods");
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

    //获取索引
    @Test
    void existIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("yudian");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    //删除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("yudian");
        AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

文档(测试类)

  //添加文档
    @Test
    void addDocument() throws IOException {
        User user = new User(2, "后的三个", 9);
        IndexRequest request = new IndexRequest("yudian");
        request.id("2");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        System.out.println(index.toString());
    }

    //判断文档是否存在
    @Test
    void existDocument() throws IOException {
        GetRequest request = new GetRequest("yudian", "1");
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");
        boolean exists = client.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);


    }

    //获取文档
    @Test
    void getDocument() throws IOException {
        GetRequest request = new GetRequest("yudian", "1");
        GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
        System.out.println(documentFields.getSourceAsString());
        System.out.println(documentFields);

    }

    //更新文档信息
    @Test
    void updateDocument() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("yudian", "1");
        updateRequest.timeout("1s");
        User user = new User(1, "yudian", 2);
        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(update.status());
    }

    //删除文档
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("yudian", "1");
        deleteRequest.timeout("1s");
        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.status());
    }

批量增加

//批量增加
    @Test
    void blukRequest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        ArrayList<User> users = new ArrayList<>();
        users.add(new User(1, "hhjd", 3));
        users.add(new User(1, "fdf", 3));
        users.add(new User(1, "c", 3));
        users.add(new User(1, "d", 3));
        users.add(new User(1, "e", 3));
        users.add(new User(1, "f", 3));
        users.add(new User(1, "g", 3));

        for (int i = 0; i < users.size(); i++) {
            BulkRequest request = bulkRequest.add(new IndexRequest("yudian").id("" + (i + 1)).source(JSON.toJSONString(users.get(i)), XContentType.JSON));
            BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
            System.out.println(bulk.hasFailures());//返回false代表成功
        }
    }

分页查询(测试)

//查询
    @Test
    void search() throws IOException {
        SearchRequest searchRequest = new SearchRequest("yudian");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //精确查询
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "c");
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(search.getHits()));
        for (SearchHit hit : search.getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

controller

@RestController
public class EsControlelr {

    @Autowired
    private EsService esService;

    //http://localhost:8087/parse/vue
    @RequestMapping("parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) throws Exception {
        return esService.parseContent(keyword);
    }

    //http://localhost:8087/search/vue/1/10
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String, Object>> searchPage(@PathVariable("keyword") String keyword, @PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize) throws IOException {
        return esService.searchPage(keyword, pageNo, pageSize);
    }

}

service

@Service
public class EsService {
    @Qualifier("elasticsearchClient")
    @Autowired
    private RestHighLevelClient client;

    //解析数据到es中
    public  Boolean parseContent(String keyword) throws Exception {

        List<Content> contentList = new HtmlParseUtil().parseJD(keyword);
        BulkRequest request = new BulkRequest();
        request.timeout("2m");
        for (int i = 0; i < contentList.size(); i++) {
            request.add(new IndexRequest("jd_goods").source(JSON.toJSONString(contentList.get(i)), XContentType.JSON));
        }
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }


    //获取数据实现搜索功能
    public List<Map<String,Object>> searchPage(String keyword,int pageNo,int pageSize) throws IOException {
        if (pageNo<1){
            pageNo=1;
        }
        SearchRequest searchRequest = new SearchRequest("jd_goods");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.requireFieldMatch(false);//多个高亮显示
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        searchSourceBuilder.highlighter(highlightBuilder);

//精准查询
        TermQueryBuilder title = QueryBuilders.termQuery("tital", keyword);
        searchSourceBuilder.query(title);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {

            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField title1 = highlightFields.get("title");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            if (title1!=null) {
                Text[] fragments = title1.fragments();
                String new_title = "";
                for (Text text : fragments) {
                    new_title += text;
                }
                sourceAsMap.put("title", new_title);
            }
            list.add(hit.getSourceAsMap());
        }
        return list;
    }
}

util(工具类)

@Component
public class HtmlParseUtil {
    public static void main(String[] args) throws Exception {
       new HtmlParseUtil().parseJD("java").forEach(System.out::println);
    }

    public List<Content> parseJD(String keyword) throws Exception {
        String url="https://search.jd.com/Search?keyword="+keyword;
        Document document = Jsoup.parse(new URL(url), 30000);
        Element elementById = document.getElementById("J_goodsList");
        Elements elementsByTag = document.getElementsByTag("li");

        ArrayList<Content> contentList = new ArrayList<>();

        for (Element element : elementsByTag) {
            String img = element.getElementsByTag("img").eq(0).attr("src");
            String price = element.getElementsByClass("p-price").eq(0).text();
            String title = element.getElementsByClass("p-name").eq(0).text();

            Content content = new Content();
            content.setImg(img);
            content.setPrice(price);
            content.setTital(title);
            contentList.add(content);

        }
        return contentList;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值