Elasticsearch在idea中实现基础功能(跳过SSL证书认证发送HTTPS版)

Elasticsearch在idea中实现基础功能

一、引入所需依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.15.2</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.15.2</version> <!-- 根据你的Elasticsearch版本进行修改 -->
    </dependency>

二、创建elasticsearchUtil工具类

import com.alibaba.fastjson.JSON;
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.reactor.IOReactorConfig;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.poi.ss.formula.functions.T;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
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.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class ElasticsearchUtil {

private static final String ELASTICSEARCH_HOST = "172.16.93.174";
private static final int ELASTICSEARCH_PORT = 9200;
private static final String USERNAME = "elastic";
private static final String PASSWORD = "123456";

public static RestHighLevelClient storeElasticsearch() {
    try {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USERNAME, PASSWORD));

        // 创建SSLContext以跳过SSL证书验证
        SSLContext sslContext = SSLContextBuilder.create()
                .loadTrustMaterial((chain, authType) -> true)
                .build();

        // 配置HTTP客户端以使用SSLContext和跳过SSL主机名验证
        RestClientBuilder builder = RestClient.builder(
                        new HttpHost(ELASTICSEARCH_HOST, ELASTICSEARCH_PORT, "https"))
                .setHttpClientConfigCallback(httpClientBuilder ->
                        httpClientBuilder
                                .setSSLContext(sslContext)
                                .setDefaultCredentialsProvider(credentialsProvider)
                                .setDefaultIOReactorConfig(
                                        IOReactorConfig.custom()
                                                .setIoThreadCount(1)
                                                .build()));

        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
/**
 * 判断索引是否存在
 */
public boolean existsIndex(String index,RestHighLevelClient client) throws IOException {
    try {
        GetIndexRequest request = new GetIndexRequest(index);
        boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
        return exists;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *创建索引
 */
public boolean createIndex(String index,RestHighLevelClient client) throws IOException {
    try {
        CreateIndexRequest request = new CreateIndexRequest(index);
        CreateIndexResponse createIndexResponse=client.indices().create(request,RequestOptions.DEFAULT);
        return createIndexResponse.isAcknowledged();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *删除索引
 */
public boolean deleteIndex(String index,RestHighLevelClient client) throws IOException {
    try {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
        AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        return response.isAcknowledged();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *判断某索引下文档id是否存在
 */
public boolean docExists(String index, String id,RestHighLevelClient client) throws IOException {
    try {
        GetRequest getRequest = new GetRequest(index,id);
        //只判断索引是否存在不需要获取_source
        //getRequest.fetchSourceContext(new FetchSourceContext(false));
        //getRequest.storedFields("_none_");
        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        return exists;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *添加文档记录
 */
public boolean addDoc(String index, String id, Map<String,String> t,RestHighLevelClient client) throws IOException {
    try {
        IndexRequest request = new IndexRequest(index);
        request.id(id);
        //设置超时时间
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        //将前端获取来的数据封装成一个对象转换成JSON格式放入请求中
        request.source(JSON.toJSONString(t), XContentType.JSON);
        IndexResponse indexResponse = client.index(request,RequestOptions.DEFAULT);
        RestStatus Status = indexResponse.status();
        System.out.println("Index Response Status: " + Status);
        return Status==RestStatus.OK||Status== RestStatus.CREATED;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *根据id来获取记录
 */
public GetResponse getDoc(String index, String id,RestHighLevelClient client) throws IOException {
    try {
        GetRequest request = new GetRequest(index,id);
        GetResponse getResponse = client.get(request,RequestOptions.DEFAULT);
        return getResponse;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *批量添加文档记录
 */
public boolean bulkAdd(String index, List<T> list,RestHighLevelClient client) throws IOException {
    try {
        BulkRequest bulkRequest = new BulkRequest();
        //设置超时时间
        bulkRequest.timeout(TimeValue.timeValueMinutes(2));
        bulkRequest.timeout("2m");
        for (int i =0;i<list.size();i++){
            bulkRequest.add(new IndexRequest(index).source(JSON.toJSONString(list.get(i))));
        }
        BulkResponse bulkResponse = client.bulk(bulkRequest,
                RequestOptions.DEFAULT);
        return !bulkResponse.hasFailures();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *更新文档记录
 */
public boolean updateDoc(String index,String id,T t,RestHighLevelClient client) throws IOException {
    try {
        UpdateRequest request = new UpdateRequest(index,id);
        request.doc(JSON.toJSONString(t));
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        return updateResponse.status()==RestStatus.OK;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *删除文档记录
 */
public boolean deleteDoc(String index,String id,RestHighLevelClient client) throws IOException {
    try {
        DeleteRequest request = new DeleteRequest(index,id);
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
        return deleteResponse.status()== RestStatus.OK;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

/**
 *根据某字段来搜索
 */
/**
 * @param index  要查询的索引名称。这是您要在其中执行搜索的Elasticsearch索引
 * @param field  要查询的字段名称。这是您要在其中搜索的字段。
 * @param key    搜索关键词。这是您要在指定字段中查找的关键词或值。
 * @param from   结果集的起始位置。它用于分页,指定从搜索结果集的哪个位置开始返回结果。
 * @param size   每页的结果数量。它指定了每个分页的结果数目。
 * @param client RestHighLevelClient 客户端对象,用于与Elasticsearch进行通信。
 * @return
 * @throws IOException
 */
public String search(String index, String field , String key, Integer from, Integer size, RestHighLevelClient client) throws IOException {
    try {
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery(field, key));
        //控制搜素
        sourceBuilder.from(from);
        sourceBuilder.size(size);
        //最大搜索时间。
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        return JSON.toJSONString(searchResponse.getHits());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}
  /**
 * 根据字段模糊查询
 * @param index
 * @param field
 * @param keyword
 * @param client
 * @return
 * @throws IOException
 */
public SearchHit[] fuzzySearch(String index, String field, String keyword,RestHighLevelClient client) throws IOException {
    try {
        // 构建查询条件
        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(field, "*" + keyword + "*");

        // 构建搜索请求
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(queryBuilder);
        SearchRequest searchRequest = new SearchRequest(index).source(sourceBuilder);

        // 执行查询
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 获取命中的文档
        SearchHits hits = searchResponse.getHits();
        return hits.getHits();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}

}
其中ELASTICSEARCH_HOST、ELASTICSEARCH_PORT、USERNAME、PASSWORD的值修改为你自己的参数(本方法是默认的https格式请求包含跳过SSL证书认证)

三、编写测试类测试方法

import javax.annotation.Resource;

import com.example.util.ElasticsearchUtil;
import com.example.util.PdfReader;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@SpringBootTest
@RunWith(SpringRunner.class)
class GodsAndDemonsApplicationTests {
@Resource
private ElasticsearchUtil elasticsearchUtil;
// @Resource
// private PdfReader pdfReader;
@Test
public void testCreateIndex() throws IOException {
RestHighLevelClient client = ElasticsearchUtil.storeElasticsearch();
boolean ceshi = elasticsearchUtil.createIndex(“youlike1”, client);
// File file=new File(“I:\于泽洋\Documents\WeChat Files\WeChat Files\wxid_nwn8vsou2une22\FileStorage\File\2023-09\《钢渣处理技术的评价标准》-提交20221220.pdf”);
// String s = pdfReader.storePDF(file);
// Map<String,String> map= new HashMap<>();
// map.put(“text”,s);
// elasticsearchUtil.addDoc(“ceshi”,“2”,map,client);
// String search = elasticsearchUtil.search(“ceshi”, “text”, “于”, 0, 10, client);
// elasticsearchUtil.fuzzySearch(“ceshi”, “text”,“于”, 0, 10, client);
// System.out.println(search);

// elasticsearchUtil.addDoc(“ceshi”,“1”,“deadt”,client);
// SearchHit[] searchHits = elasticsearchUtil.fuzzySearch(“ceshi”, “text”, “于”, client);
// System.out.println(searchHits[0].getSourceAsString());
}
其中包含三个方法,没注释的为创建索引方法,youlike1为索引名称,client为es程序方法。第二个方法为在索引名中新增文档内容(此方法为pdf导入,可因人而异改成自己需要的)。第三个方法为根据字段模糊查询文档记录

四、创建可视化层添加调用方法

import com.example.util.ElasticsearchUtil;
import com.example.util.PdfReader;
import com.example.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(“/pdf”)
public class PdfController {
@Resource
ElasticsearchUtil elasticsearchUtil;
@Resource
PdfReader pdfReader;

@PostMapping("importOutPDF")
public Result importOut(@RequestParam("file") MultipartFile multipartFile) throws IOException {
    try {
        // 'multipartFile' 是你的 MultipartFile 对象
        byte[] bytes = multipartFile.getBytes();
        InputStream inputStream = multipartFile.getInputStream();
        // 使用这些字节或流创建一个新的 File 对象
        File file = new File(multipartFile.getOriginalFilename());
        try (OutputStream out = new FileOutputStream(file)) {
            out.write(bytes);
        } catch (IOException e) {
        log.error(e.getMessage());
        }
            RestHighLevelClient client = ElasticsearchUtil.storeElasticsearch();
            if (!elasticsearchUtil.existsIndex("youlike", client)) {
                client = ElasticsearchUtil.storeElasticsearch();
                elasticsearchUtil.createIndex("youlike", client);
            }
            String s = pdfReader.storePDF(file);
            Map<String, String> map = new HashMap<>();
            map.put("text", s);
        client = ElasticsearchUtil.storeElasticsearch();
            elasticsearchUtil.addDoc("youlike", file.getName(), map, client);
            return Result.success("添加成功");
        } catch (IOException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据id查询档案
     * @param id
     * @return Result
     * @throws IOException
     */
    @GetMapping("idSearch")
    public Result idSearch (@RequestParam("id") String id) throws IOException {
        RestHighLevelClient client = ElasticsearchUtil.storeElasticsearch();
        if (!elasticsearchUtil.existsIndex("youlike", client)) {
            return Result.error(new NullPointerException().getMessage());
        }
        client = ElasticsearchUtil.storeElasticsearch();
        GetResponse doc = elasticsearchUtil.getDoc("youlike", id, client);
        return Result.success(doc.getSource());
    }

    /**
     * 根据关键字查询档案
     * @param name
     * @return Result
     * @throws IOException
     */
    @GetMapping("fieldsSearch")
    public Result fieldsSearch (@RequestParam("name") String name) throws IOException {
        RestHighLevelClient client = ElasticsearchUtil.storeElasticsearch();
        if (!elasticsearchUtil.existsIndex("youlike", client)) {
            return Result.error(new NullPointerException().getMessage());
        }
        client = ElasticsearchUtil.storeElasticsearch();
        SearchHit[] searchHits = elasticsearchUtil.fuzzySearch("youlike", "text", name, client);
        return Result.success(searchHits);
    }
    }

这三个方法为插入pdf文件的文章,根据索引查询文档,根据关键字查询文档。(具体需求自己编写不过多赘述)

项目部分图片

根据索引查询es图片:
在这里插入图片描述
pdf新增索引图片:
在这里插入图片描述
该错误为没有写请求实体报的格式不正确不影响功能

根据关键字模糊查询图片:
在这里插入图片描述
文章结束,编写不易麻烦关注将持续更新。。。
有问题可+Q3086463191

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱逛dn的小于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值