Java使用ElasticSearch 7.x以上客户端 常用API

依赖
		<dependency>
            <groupId>co.elastic.clientsgroupId>
            <artifactId>elasticsearch-javaartifactId>
            <version>8.1.2version>
            <exclusions>
                <exclusion>
                    <groupId>jakarta.jsongroupId>
                    <artifactId>jakarta.json-apiartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
        dependency>
        <dependency>
            <groupId>jakarta.jsongroupId>
            <artifactId>jakarta.json-apiartifactId>
            <version>2.0.1version>
        dependency>
创建客户端
try {
            RestClientTransport restClientTransport;
            if (elasticConfig==null){
                RestClient build = RestClient.builder(new HttpHost("localhost",9200)).build();
                restClientTransport = new RestClientTransport(build, new JacksonJsonpMapper());
            }else{
                if (StringUtils.hasText(elasticConfig.getUsername())&&StringUtils.hasText(elasticConfig.getPassword())){
                    // 创建用户名密码认证
                    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                    credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(elasticConfig.getUsername(), elasticConfig.getPassword()));
                    RestClient build = RestClient.builder(new HttpHost(elasticConfig.getHost(),elasticConfig.getPort())).setHttpClientConfigCallback(k->k.setDefaultCredentialsProvider(credentialsProvider)).build();
                    restClientTransport = new RestClientTransport(build, new JacksonJsonpMapper());
                }else{
                    RestClient build = RestClient.builder(new HttpHost(elasticConfig.getHost(),elasticConfig.getPort())).build();
                    restClientTransport = new RestClientTransport(build, new JacksonJsonpMapper());
                }
            }
            return new ElasticsearchClient(restClientTransport);
        }catch (Exception e){
            log.error("ElasticUtils-elasticJavaClient",e);
            return null;
        }

elasticConfig 是我自定义的一个类

@Data
public class ElasticConfig {
    private String host;
    private Integer port;
    private String username;
    private String password;
    private String index;
}
创建索引
 /**
     * 创建索引
     * @param client ES客户端
     * @param indexName 索引名称
     */
    public static boolean createIndex(ElasticsearchClient client,String indexName){
        //定义文档属性
        Map<String, Property> propertyMap = new HashMap<>();
        propertyMap.put("id",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("name",new Property(new TextProperty.Builder().analyzer("ik_max_word").searchAnalyzer("ik_smart").build()));
        propertyMap.put("path",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("url",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("md5",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("size",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("dir",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("extendName",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("source",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("isDelete",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("createTime",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("updateTime",new Property(new KeywordProperty.Builder().build()));
        propertyMap.put("content",new Property(new TextProperty.Builder().build()));
        propertyMap.put("hitTime",new Property(new TextProperty.Builder().build()));
        // 设置索引的文档类型映射
        TypeMapping typeMapping = new TypeMapping.Builder()
                .properties(propertyMap)
                .build();
        CreateIndexRequest request = new CreateIndexRequest.Builder()
                .index(indexName)
                .mappings(typeMapping)
                .build();
        try {
            CreateIndexResponse createIndexResponse = client.indices().create(request);
            Boolean acknowledged = createIndexResponse.acknowledged();
            return acknowledged != null && acknowledged;
        }catch (IOException e){
            log.error("ElasticUtils-createIndex::{}",e.getMessage(),e);
            return false;
        }
    }

Map propertyMap 定义文档属性 对应的是以前的Mapping
TypeMapping typeMapping = new TypeMapping.Builder() 设置Mapping
CreateIndexRequest request = new CreateIndexRequest.Builder() 新建一个 “创建索引”的请求构造器
index: 索引名称
client.indices().create(request): 发送创建索引请求

删除索引
    /**
     * 删除索引
     * @param client ES客户端
     * @param indexName 索引名称
     */
    public static boolean delIndex(ElasticsearchClient client,String indexName){
        try {
            DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index(indexName));
            return deleteIndexResponse.acknowledged();
        }catch (IOException e){
            log.error("ElasticUtils-delIndex::{}",e.getMessage(),e);
            return false;
        }
    }
添加文档
    /**
     * 添加文档
     * @param client ES客户端
     * @param document 文档
     * @return boolean
     */
    public static boolean addDoc(ElasticsearchClient client, FileDocument document,ElasticConfig elasticConfig){
        IndexRequest<FileDocument> request = new IndexRequest.Builder<FileDocument>()
                .index(elasticConfig.getIndex())
                // 设置文档id
                .id(document.getId()+"")
                // 设置文档
                .document(document)
                // 刷新
                .refresh(Refresh.True)
                .build();
        try {
            IndexResponse response = client.index(request);
            System.out.println(response.result().jsonValue());
            return response.result()!=null;
        }catch (IOException e){
            log.error("ElasticUtils-addDoc::{}",e.getMessage(),e);
            return false;
        }
    }
批量添加文档
public static boolean addBatchDoc(ElasticsearchClient client,String indexName, List<FileDocument> list){
        List<BulkOperation> operations = new ArrayList<>();
        for (FileDocument item : list) {
            IndexOperation<FileDocument> indexOperation = new IndexOperation.Builder<FileDocument>()
                    .index(indexName)
                    .id(item.getId().toString())
                    .document(item)
                    .build();
            BulkOperation bulk = new BulkOperation.Builder()
                    .index(indexOperation)
                    .build();
            operations.add(bulk);
        }
        BulkRequest request = new BulkRequest.Builder()
                .index(indexName)
                .operations(operations)
                .build();
        try {
            BulkResponse bulk = client.bulk(request);
            for (BulkResponseItem item : bulk.items()) {
                System.out.println(item.result());
            }
            return bulk.items().size()>0;
        }catch (IOException e){
            log.error("ElasticUtils-addDoc::{}",e.getMessage(),e);
            return false;
        }
    }
判断文档是否存在
    public static boolean existDoc(ElasticsearchClient client,String indexName,String id){
        ExistsRequest request = new ExistsRequest.Builder()
                .index(indexName)
                .id(id)
                .build();
        try {
            BooleanResponse exists = client.exists(request);
            return exists.value();
        }catch (IOException e){
            log.error("ElasticUtils-existDoc::{}",e.getMessage(),e);
            return false;
        }
    }
根据id查询文档
    public static FileDocument searchDoc(ElasticsearchClient client,String indexName,String id){
        GetRequest request = new GetRequest.Builder()
                .index(indexName)
                .id(id)
                .build();
        try {
            GetResponse<FileDocument> fileDocumentGetResponse = client.get(request, FileDocument.class);
            return fileDocumentGetResponse.source();
        }catch (IOException e){
            log.error("ElasticUtils-searchDoc::{}",e.getMessage(),e);
            return null;
        }
    }
复杂查询
public static PageResult<FileDocument> searchDoc(Integer pageNum, Integer pageSize,ElasticsearchClient client,String indexName,String keyword,String path,Boolean isDelete,String startTime,String endTime){
        //检查es是否有这个索引
        if (!ElasticUtils.existsIndex(client,indexName)) {
            return null;
        }
        PageResult<FileDocument> pageResult = new PageResult<>();
        List<FileDocument> list = new ArrayList<>();
        BoolQuery.Builder boolQueryBuilder = new BoolQuery.Builder();
        if (StringUtils.hasText(keyword)){
            Query fuzzyQuery1 = FuzzyQuery.of(f -> f.field("name").value(keyword))._toQuery();
            Query matchQuery1 = MatchQuery.of(m -> m.field("name").query(keyword).boost(2f))._toQuery();
            BoolQuery boolQuery1 = new BoolQuery.Builder()
                    .should(fuzzyQuery1)
                    .should(matchQuery1)
                    .build();
            Query fuzzyQuery2 = FuzzyQuery.of(f -> f.field("content").value(keyword))._toQuery();
            Query matchQuery2 = MatchQuery.of(m -> m.field("content").query(keyword).boost(2f))._toQuery();
            BoolQuery boolQuery2 = new BoolQuery.Builder()
                    .should(fuzzyQuery2)
                    .should(matchQuery2)
                    .build();
            BoolQuery boolQuery3 = new BoolQuery.Builder()
                    .should(boolQuery1._toQuery())
                    .should(boolQuery2._toQuery())
                    .build();
            boolQueryBuilder.must(boolQuery3._toQuery());
        }else{
            boolQueryBuilder.must(q->q.matchAll(new MatchAllQuery.Builder().build()));
        }
        BoolQuery.Builder boolQuery4Builder = new BoolQuery.Builder();
        boolQuery4Builder.must(q->q.term(t->t.field("isDelete").value(isDelete)));
        if (StringUtils.hasText(path)){
            boolQuery4Builder.must(q->q.term(t->t.field("dir").value(path)));
        }
        if (StringUtils.hasText(startTime)){
            RangeQuery rangeQuery = new RangeQuery.Builder()
                    .field("updateTime")
                    .gte(JsonData.of(startTime))
                    .build();
            boolQuery4Builder.must(rangeQuery._toQuery());
        }
        if (StringUtils.hasText(endTime)){
            RangeQuery rangeQuery = new RangeQuery.Builder()
                    .field("updateTime")
                    .lte(JsonData.of(endTime))
                    .build();
            boolQuery4Builder.must(rangeQuery._toQuery());
        }
        BoolQuery boolQuery4 = boolQuery4Builder.build();
        boolQueryBuilder.must(boolQuery4._toQuery());
        BoolQuery boolQuery = boolQueryBuilder.build();
        Query query = boolQuery._toQuery();
        SearchRequest.Builder requestBuilder = new SearchRequest.Builder();
        requestBuilder.index(indexName);
        //设置排序
        SortOptions fieldSort = new SortOptions.Builder().field(k->k.field("updateTime").order(SortOrder.Desc)).build();
        requestBuilder.sort(fieldSort);
        if (pageNum!=null&&pageSize!=null&&pageNum>0&&pageSize>0){
            requestBuilder.from((pageNum-1)*pageSize).size(pageSize);
        }
        requestBuilder.query(query);
        SearchRequest request = requestBuilder.source(k->k.filter(f->f.excludes("content"))).build();
        try {
            SearchResponse<FileDocument> search = client.search(request, FileDocument.class);
            if (search!=null&&search.hits()!=null&&search.hits().total()!=null){
                if (search.hits().hits().size()>0) {
                    for (Hit<FileDocument> hit : search.hits().hits()) {
                        list.add(hit.source());
                    }
                    pageResult.setData(list);
                    pageResult.setTotal(search.hits().total().value());
                }
            }
            pageResult.setCode(200);
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
        return pageResult;

PageResult: 是自定义的分页结果类
这里使用到了许多查询的条件 主要就是一个个查询构造器
具体怎么使用我这里不赘述 主要是给一个参考。
该方法的功能就是:根据关键字模糊查询文档的“name”属性和“content”属性并且根据isDelete请求查询文档的“isDelete”属性并且根据“dir”精确查询文档的”dir“属性,区间查询createTime和updateTime之间的数据 lt表示小于 gt表示大于,然后根据updateTime排序,最后的结果去掉content属性。
文档类

public class FileDocument {
/**文档主键*/
private Long id;
/**文件名称*/
private String name;
/**文件路径*/
private String path;
/**文件url*/
private String url;
/**文件md5*/
private String md5;
/**文件大小*/
private Long size;
/**文件夹id*/
private Long dir
/**文件后缀名*/
private String extendName;
/**文件服务器*/
private String source;
/**文件是否已删除*/
private Boolean isDelete;
/**文件创建时间*/
private String createTime;
/**文件更新时间*/
private String updateTime;
/**文件内容*/
private String content;
/**文件查询命中时间*/
private String hitTime;
}
删除文档
    /**
     * 删除文档
     * @param client ES客户端
     * @param index 索引名称
     * @param id 文档id
     * @return boolean
     */
    public static boolean delDoc(ElasticsearchClient client,String index,String id){
        DeleteRequest request = new DeleteRequest.Builder()
                .index(index)
                .id(id)
                .build();
        try {
            DeleteResponse delete = client.delete(request);
            System.out.println(delete.result().jsonValue());
            return delete.result()!=null;
        }catch (IOException e){
            log.error("ElasticUtils-delDoc::{}",e.getMessage(),e);
            return false;
        }
    }
批量删除文档
/**
     * 批量删除文档
     * @param client ES客户端
     * @param ids 文档id列表
     * @param elasticConfig ES配置信息
     * @return 操作结果
     */
    public static boolean delDoc(ElasticsearchClient client,List<String> ids,ElasticConfig elasticConfig){
        try {
            List<BulkOperation> operations = new ArrayList<>();
            if (!CollectionUtils.isEmpty(ids)){
                for (String id : ids) {
                    DeleteOperation deleteOperation = new DeleteOperation.Builder()
                            .index(elasticConfig.getIndex())
                            .id(id)
                            .build();
                    BulkOperation operation = new BulkOperation.Builder()
                            .delete(deleteOperation)
                            .build();
                    operations.add(operation);
                }
                BulkRequest request = new BulkRequest.Builder()
                        .index(elasticConfig.getIndex())
                        .operations(operations)
                        .refresh(Refresh.True)
                        .build();
                BulkResponse bulkResponse = client.bulk(request);
                return !CollectionUtils.isEmpty(bulkResponse.items());
            }
            return true;
        }catch (IOException e){
            log.error("ElasticUtils-delDoc::{}",e.getMessage(),e);
            return false;
        }
    }
批量更新文档
/**
     * 更新文档
     * @param client ES客户端
     * @param ids 列表ids
     * @param documents 用于替换的结果
     * @return 修改结果
     */
    public static boolean updateDoc(ElasticsearchClient client,
                                    List<String> ids,
                                    List<FileDocument> documents,
                                    ElasticConfig elasticConfig){
        try {
            List<BulkOperation> operations = new ArrayList<>();
            Map<String, List<FileDocument>> documentsMap = documents.stream().collect(Collectors.groupingBy(k->k.getId().toString()));
            for (String id : ids) {
                List<FileDocument> update = documentsMap.get(id);
                if (!CollectionUtils.isEmpty(update)){
                    UpdateAction<FileDocument,FileDocument> updateAction = new UpdateAction.Builder<FileDocument,FileDocument>()
                            .doc(update.get(0))
                            .build();
                    UpdateOperation<FileDocument,FileDocument> updateOperation = new UpdateOperation.Builder<FileDocument,FileDocument>()
                            .index(elasticConfig.getIndex())
                            .action(updateAction)
                            .id(id)
                            .build();
                    BulkOperation bulk = new BulkOperation.Builder()
                            .update(updateOperation)
                            .build();
                    operations.add(bulk);
                }
            }
            BulkRequest request = new BulkRequest.Builder()
                    .index(elasticConfig.getIndex())
                    .operations(operations)
                    .build();
            BulkResponse bulkResponse = client.bulk(request);
            return !CollectionUtils.isEmpty(bulkResponse.items());
        } catch (IOException e){
            log.error("ElasticUtils-updateDoc::{}",e.getMessage(),e);
            throw new ServiceException("修改ES文档失败!");
        }
    }

更新单个文档 和之前的差不多 这里忽略了。这里是全部替换,下面的是选择属性修改

批量更新文档
/**
     * 更新文档
     * @param client ES客户端
     * @param ids 列表ids
     * @param map 要修改的map
     * @return 修改结果
     */
    public static boolean updateDoc(ElasticsearchClient client,
                                    List<String> ids,
                                    Map<String,Map<String,Object>> map,
                                    ElasticConfig elasticConfig){
        try {
            List<BulkOperation> operations = new ArrayList<>();
            for (String id : ids) {
                Map<String,Object> updateMap = map.get(id);
                if (MapUtils.isNotEmpty(updateMap)){
                    UpdateAction<FileDocument,Map<String,Object>> updateAction = new UpdateAction.Builder<FileDocument,Map<String,Object>>()
                            .doc(updateMap)
                            .build();
                    UpdateOperation<FileDocument,Map<String,Object>> updateOperation = new UpdateOperation.Builder<FileDocument,Map<String,Object>>()
                            .index(elasticConfig.getIndex())
                            .action(updateAction)
                            .id(id)
                            .build();
                    BulkOperation bulk = new BulkOperation.Builder()
                            .update(updateOperation)
                            .build();
                    operations.add(bulk);
                }
            }
            BulkRequest request = new BulkRequest.Builder()
                    .index(elasticConfig.getIndex())
                    .operations(operations)
                    .refresh(Refresh.True)
                    .build();
            BulkResponse bulkResponse = client.bulk(request);
            return !CollectionUtils.isEmpty(bulkResponse.items());
        } catch (IOException e){
            log.error("ElasticUtils-updateDoc::{}",e.getMessage(),e);
            throw new ServiceException("修改ES文档失败!");
        }
    }

你可能感兴趣的:(elasticsearch,java,搜索引擎)