java elasticsearch7.17.14版本 resthighlevelclient 如何复合查询

亲测有效教程https://blog.youkuaiyun.com/weixin_44205136/article/details/133675919?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f973e46ee400f9d2409d473b2259088c%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=f973e46ee400f9d2409d473b2259088c&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-133675919-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187https://blog.youkuaiyun.com/weixin_44205136/article/details/133675919?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f973e46ee400f9d2409d473b2259088c%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=f973e46ee400f9d2409d473b2259088c&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-133675919-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187https://blog.youkuaiyun.com/weixin_44205136/article/details/133675919?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f973e46ee400f9d2409d473b2259088c%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=f973e46ee400f9d2409d473b2259088c&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-133675919-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187https://blog.youkuaiyun.com/weixin_44205136/article/details/133675919?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f973e46ee400f9d2409d473b2259088c%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=f973e46ee400f9d2409d473b2259088c&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-133675919-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187https://blog.youkuaiyun.com/weixin_44205136/article/details/133675919?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f973e46ee400f9d2409d473b2259088c%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=f973e46ee400f9d2409d473b2259088c&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-133675919-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187

复合+分组 求和 平均数https://blog.youkuaiyun.com/m0_73927143/article/details/142501549https://blog.youkuaiyun.com/m0_73927143/article/details/142501549https://blog.youkuaiyun.com/m0_73927143/article/details/142501549https://blog.youkuaiyun.com/m0_73927143/article/details/142501549

es常用查询语句 

GET _search
{
  "query": {
    "match_all": {}
  }
}

DELETE /_all

PUT imgsearch
{
  "mappings": {
    "properties": {
      "feature": {
        "type": "dense_vector",
        "dims": 2048
      },
      "url": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}


GET imgsearch

GET imgsearch/_search


POST imgsearch/_search
{
    "size": 30,
    "min_score": 1.6,
    "query": {
        "script_score": {
            "query": {
                "match_all": {}
            },
            "script": {
                "source": "cosineSimilarity(params.queryVector, params.field)+1.0",
                "params": {
                    "queryVector": [0.144, 0.555, 0.888],
                     "field": "feature" 
                }
            }
        }
    }
}


POST imgsearch/_search
{
  "script_score": {
    "query": {
      "match_all": {}
    },
    "script": {
      "source": "cosineSimilarity(params.query_vector, params.field) + 1.0",
      "params": {
        "query_vector": [0.144, 0.555, 0.888],
        "field": "feature" 
      }
    }
  }
}


DELETE /ceshi


# 查询索引状态
GET /_all/_mapping

GET /_all/_settings

# 指定索引名称ceshi
GET /ceshi/_settings

GET ftzljj3b/_search

GET megfwvkb/_search

GET ftzljj3b/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "fileContentCN": {
                    "query": "开发部",
                    "slop": 0,
                    "zero_terms_query": "NONE",
                    "boost": 1.0
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "fileContentCN": {
                    "query": "1",
                    "slop": 0,
                    "zero_terms_query": "NONE",
                    "boost": 1.0
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "sort": [
    {
      "fonds_no.keyword": {
        "order": "desc"
      }
    },
    {
      "filing_year.keyword": {
        "order": "asc"
      }
    },
    {
      "archive_no_code.keyword": {
        "order": "asc"
      }
    }
  ],
  "aggregations": {
    "dept": {
      "terms": {
        "field": "depart_name_code",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "fonds": {
      "terms": {
        "field": "fonds_no_code",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "year": {
      "terms": {
        "field": "filing_year",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "retention": {
      "terms": {
        "field": "retention_code",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "archive": {
      "terms": {
        "field": "archive_ctg_no_code",
        "size": 10,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    }
  },
  "highlight": {
    "pre_tags": [
      "<span color='red'>"
    ],
    "post_tags": [
      "</span>"
    ],
    "fragment_size": 40,
    "require_field_match": false,
    "fields": {
      "fileContentCN": {
        
      }
    }
  }
}

java代码

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.jeecgframework.boot</groupId>
        <artifactId>jeecg-boot-parent</artifactId>
        <version>3.6.2</version>
    </parent>
    <artifactId>jeecg-module-es</artifactId>
    <name>elasticSearch</name>

    <dependencies>
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-module-das</artifactId>
            <version>3.6.2</version>
        </dependency>
        <!-- 排除springboot中内置的es依赖,以防和easy-es中的依赖冲突-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.17.14</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.17.14</version>
        </dependency>

        <dependency>
            <groupId>org.dromara.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>


    </dependencies>
</project>
package org.jeecg.es.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
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.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.xcontent.XContentType;
//import org.elasticsearch.common.xcontent.XContentType;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.es.service.IndexUtilService;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.mapper.CesIndexInfoMapper;
import org.jeecg.modules.system.service.*;
import org.jeecg.modules.system.util.StrUtil;
import org.jeecg.modules.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;
// 索引操作 https://blog.youkuaiyun.com/J_bean/article/details/134750047?ops_request_misc=&request_id=&biz_id=102&utm_term=RestHighLevelClient%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E5%B9%B6%E4%B8%94%E8%AE%BE%E7%BD%AEset&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-6-134750047.nonecase&spm=1018.2226.3001.4187
//数据增删改查参考https://blog.youkuaiyun.com/zhuocailing3390/article/details/126318849?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522b9af4a493b57c1ea16328a998fb32b43%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=b9af4a493b57c1ea16328a998fb32b43&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-126318849-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E6%89%B9%E9%87%8F%E6%B7%BB%E5%8A%A0%E7%B4%A2%E5%BC%95%E6%95%B0%E6%8D%AE&spm=1018.2226.3001.4187
// 复合查询 https://blog.youkuaiyun.com/weixin_44205136/article/details/133675919?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f973e46ee400f9d2409d473b2259088c%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=f973e46ee400f9d2409d473b2259088c&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-133675919-null-null.142^v101^pc_search_result_base1&utm_term=RestHighLevelClient%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187

/**
 * @ClassName IndexUtilServiceImpl
 * @Description TODO
 * @Author wzh
 * @Date 2024/12/24 11:17
 */
@Slf4j
@Service
public class IndexUtilServiceImpl implements IndexUtilService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private ICesEntityTypeService cesEntityTypeService;
    @Autowired
    private ICesIndexTableColumnService cesIndexTableColumnService;
    @Autowired
    private ICesIndexInfoService cesIndexInfoService;
    @Autowired
    private CesIndexInfoMapper cesIndexInfoMapper;
    @Autowired
    private ICesIndexLibraryService cesIndexLibraryService;
    @Autowired
    private ICesUserFieldService cesUserFieldService;

    @Override
    public boolean indexCreate(String indexName) {
        boolean result = false;
        String entityId = indexName;
        indexName = indexName.toLowerCase();
        try {
            //先判断索引是否存在
            if (!indexExists(indexName)) {
                // 1、创建 创建索引request 参数:索引名mess
                CreateIndexRequest indexRequest = new CreateIndexRequest(indexName);
                // 5.1 同步方式发送请求
                IndicesClient indicesClient = restHighLevelClient.indices();

                //查询实体信息 indexName
                //通过实体查询结构 组装mapping

                // 3、设置索引的mappings
//                ObjectMapper objectMapper1 = new ObjectMapper();
//                JsonNode jsonNode1 = objectMapper1.readTree(mapping);
//                System.out.println("mapping:" + jsonNode1.toString());
                indexRequest.mapping("_doc", GetMappingString(entityId), XContentType.JSON);
                // 2、设置索引的settings
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode jsonNode = objectMapper.readTree(settings);
                System.out.println("settings:" + jsonNode.toString());
                indexRequest.settings(jsonNode.toString(), XContentType.JSON);

                // 请求服务器
                CreateIndexResponse response = indicesClient.create(indexRequest, RequestOptions.DEFAULT);
                result = response.isAcknowledged();
            }
        } catch (Exception e) {
            result = false;
            log.error("创建索引失败", e);
        }
        return result;
    }

    public String GetMappingString(String entityId) {
        QueryWrapper<CesIndexTableColumn> query = new QueryWrapper<>();
        query.eq("entity_id", entityId);
        List<CesIndexTableColumn> listIndexTableColumn = cesIndexTableColumnService.list(query);//获取门类索引字段
        JSONObject jsonMapping = new JSONObject();
        JSONObject properties = new JSONObject();
        JSONObject fileContentCN = new JSONObject();
        JSONObject fileContentCNYw = new JSONObject();
        JSONObject fields = new JSONObject();
        JSONObject keyword = new JSONObject();

        fileContentCN.put("type", "text");
        fileContentCN.put("analyzer", "my_ngram_analyzer");
        properties.put("fileContentCN", fileContentCN);

        fileContentCNYw.put("type", "text");
        fileContentCNYw.put("analyzer", "my_ngram_analyzer");
        properties.put("fileContentYw", fileContentCNYw);
            /*fileContentCNYw.put("type","text");
            keyword.put("type","keyword");
            keyword.put("ignore_above","1024");
            fields.put("keyword",keyword);
            fileContentCNYw.put("fields",fields);
            properties.put("fileContentYw",fileContentCNYw);*/
        for (CesIndexTableColumn indexTableColumn : listIndexTableColumn) {
            fileContentCNYw = new JSONObject();
            fields = new JSONObject();
            keyword = new JSONObject();

            fileContentCNYw.put("type", "text");
            if ("depart_name_code".equals(indexTableColumn.getColumnName())|| "fonds_no_code".equals(indexTableColumn.getColumnName()) ||
                    "filing_year".equals(indexTableColumn.getColumnName()) || "retention_code".equals(indexTableColumn.getColumnName()) ||
                    "archive_ctg_no_code".equals(indexTableColumn.getColumnName())){
                fileContentCNYw.put("fielddata", true);
            }
            keyword.put("type", "keyword");
            keyword.put("ignore_above", "256");
            fields.put("keyword", keyword);
            fileContentCNYw.put("fields", fields);
            properties.put(indexTableColumn.getColumnName(), fileContentCNYw);


        }
        jsonMapping.put("properties", properties);
        return jsonMapping.toJSONString();
    }

    @Override
    public Map<String, Object> getMapping(String indexName) {
        indexName = indexName.toLowerCase();
        Map<String, Object> sourceAsMap = new HashMap<>();
        try {
            IndicesClient indicesClient = restHighLevelClient.indices();
            // 创建get请求
            GetIndexRequest request = new GetIndexRequest(indexName);
            // 发送get请求
            GetIndexResponse response = indicesClient.get(request, RequestOptions.DEFAULT);
            // 获取表结构
            Map<String, MappingMetadata> mappings = response.getMappings();
            sourceAsMap = mappings.get(indexName).getSourceAsMap();
        } catch (Exception e) {
            log.error("获取索引结构失败", e);
        }
        return sourceAsMap;
    }

    @Override
    public boolean indexDelete(String indexName) {
        boolean result = false;
        indexName = indexName.toLowerCase();
        try {
            IndicesClient indicesClient = restHighLevelClient.indices();
            // 创建delete请求方式
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
            // 发送delete请求
            AcknowledgedResponse response = indicesClient.delete(deleteIndexRequest, RequestOptions.DEFAULT);
            result = response.isAcknowledged();
        } catch (Exception e) {
            result = false;
            log.error("删除索引失败", e);
        }
        return result;
    }

    @Override
    public boolean indexExists(String indexName) {
        boolean result = false;
        indexName = indexName.toLowerCase();
        try {
            IndicesClient indicesClient = restHighLevelClient.indices();
            // 创建get请求
            GetIndexRequest request = new GetIndexRequest(indexName);
            // 判断索引库是否存在
            result = indicesClient.exists(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("判断索引库是否存在失败", e);
        }
        return result;
    }

    @Override
    public boolean add(Map<String, Object> data, String indexName) {
        indexName = indexName.toLowerCase();
        try {
            //获取id
            String id = data.get("id") + "";
            // 创建添加文档的索引对象,索引必须先存在,索引名称为
            IndexRequest request = new IndexRequest().index(indexName);
            // 设置ID,可以不设置,不设置时es默认创建
            request.id(id);
            // 添加文档数据
            request.source(JSON.toJSONString(data), XContentType.JSON);
            // 请求es
            IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("添加文档失败", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean addBatch(List<Map<String, Object>> data, String indexName) {
        indexName = indexName.toLowerCase();
        try {
            // 批量插入数据
            BulkRequest request = new BulkRequest();
            String finalIndexName = indexName;
            data.stream().forEach(item -> {
                String id = item.get("id") + "";
                // 添加文档数据
                IndexRequest source = new IndexRequest().index(finalIndexName).id(id);
                source.source(JSON.toJSONString(item), XContentType.JSON);
                request.add(source);
            });
            // 请求es
            BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("批量添加文档失败", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean update(Map<String, Object> data, String indexName) {
        indexName = indexName.toLowerCase();
        try {
            // 修改文档时索引必须先存在,索引名称为
            UpdateRequest request = new UpdateRequest().index(indexName);
            request.id(data.get("id") + "");
            // 添加文档数据
            request.doc(JSON.toJSONString(data), XContentType.JSON);
            // 请求es
            UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("修改文档失败", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean exists(String indexName, String id) {
        indexName = indexName.toLowerCase();
        boolean exists = false;
        try {
            GetRequest request = new GetRequest();
            // 查看文档的id
            request.index(indexName).id(id);
            exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("判断文档是否存在失败", e);
            return false;
        }
        return exists;
    }

    @Override
    public boolean delete(String indexName, String id) {
        indexName = indexName.toLowerCase();
        try {
            DeleteRequest request = new DeleteRequest();
            // 删除文档的id,id必须存在
            request.index(indexName).id(id);
            DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("删除文档失败", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteBatch(String indexName, List<String> ids) {
        indexName = indexName.toLowerCase();
        try {
            // 批量删除数据
            BulkRequest request = new BulkRequest();
            String finalIndexName = indexName;
            ids.stream().forEach(id -> {
                DeleteRequest request2 = new DeleteRequest().index(finalIndexName).id(id);
                request.add(request2);
            });
            BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("批量删除文档失败", e);
            return false;
        }
        return true;
    }

    @Override
    public Object select(String indexName, String id) {
        indexName = indexName.toLowerCase();
        GetResponse response = null;
        try {
            GetRequest request = new GetRequest();
            // 查看文档的id
            request.index(indexName).id(id);
            response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("查询文档失败", e);
            return null;
        }
        return response;
    }

    @Override
    public CesIndexInfo clearIndexColumn(String entityId) {
        CesIndexInfo obj =new CesIndexInfo();
        CesIndexInfo saveOrUpObj =new CesIndexInfo();
        String dateStr = DateUtil.getDateStr("yyyy-MM-dd");
        Date indexTime = DateUtil.parseYMDDate(dateStr);
        obj =cesIndexInfoService.getOne(new QueryWrapper<CesIndexInfo>().eq("entity_id", entityId));//获取主表详情

        //处理主表
        if(null!=obj&& StrUtil.isNotNull(obj.getId())){//已存在
            saveOrUpObj.setIndexTime(indexTime);
            saveOrUpObj.setIndexFlag(1);
            saveOrUpObj.setId(obj.getId());
            cesIndexInfoService.updateById(saveOrUpObj);//修改
            obj.setIndexTime(indexTime);
            obj.setIndexFlag(1);
        }
        cesIndexTableColumnService.updateIndexFlag(obj.getIndexTable());//更新实体表中索引标识为NUll
        indexDelete(entityId);
        return obj;
    }

    @Override
    public Page<Map<String, Object>> getFullTextSearch(JSONObject paramJson) {
        String searchKey = paramJson.getString("searchKey");//查询内容
        String searchIndexLib = paramJson.getString("searchIndexLib");//索引库id
        String searchMode = paramJson.getString("searchMode");//方式精确模糊 true精确 false模糊
        String searchType = paramJson.getString("searchType");//门类类型
        String searchContent = paramJson.getString("searchContent");//条目原文 查询字段
        //String searchSort = paramJson.getString("searchSort");//排序
        String showList = paramJson.getString("showList");//true显示列表false显示表格
        int pageNo = paramJson.getIntValue("pageNo"); //分页
        int pageSize = paramJson.getIntValue("pageSize");
        Page<Map<String, Object>> map = new Page<>(pageNo, pageSize);
        try {
            //查询检索门类
            CesIndexLibrary cesIndexLibrary = cesIndexLibraryService.getById(searchIndexLib);
            String libraryEntitys = cesIndexLibrary.getLibraryEntitys().toLowerCase();
            String[] indexLib = libraryEntitys.split(",");
            //查询 门类
            SearchRequest request = new SearchRequest(indexLib);
            //组装查询 条件
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //精确查询
            if (searchMode.equals("true")){
                String[] values = searchKey.split(" ");
                for (String value : values) {
                    boolQueryBuilder.must(QueryBuilders.boolQuery().should(QueryBuilders.matchPhraseQuery(searchContent, value)));
                }
            } else {
                //模糊查询
                boolQueryBuilder.must(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery(searchContent, searchKey)));
            }
            //保存查询条件
            request.source().query(boolQueryBuilder);
            //组装高亮
            // 2.1 高亮
            request.source().highlighter(new HighlightBuilder().field(searchContent).requireFieldMatch(false).fragmentSize(40).preTags("<span color='red'>").postTags("</span>"));
            //获取排序条件
            //2.通过shiro获取登录用户信息
            LoginUser sysUser = null;
            try {//后台获取用户信息
                sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            } catch (Exception e) {
                log.warn("SecurityUtils.getSubject() 获取用户信息异常:" + e.getMessage());
            }
            //组装排序
            List<CesUserField> list = cesUserFieldService.list(new LambdaQueryWrapper<CesUserField>().eq(CesUserField::getUserName, sysUser.getUsername())
                    .eq(CesUserField::getEntityId, searchIndexLib).orderByDesc(CesUserField::getFieldSeq));
            list.forEach(cesUserField -> {
               if ("档号".equals(cesUserField.getFieldNameZh())){
                   if ("asc".equals(cesUserField.getFieldAction())){
                       request.source().sort("archive_no_code.keyword", SortOrder.ASC);// SortOrder.DESC
                   } else {
                       request.source().sort("archive_no_code.keyword", SortOrder.DESC);
                   }
               } else {
                   if ("asc".equals(cesUserField.getFieldAction())){
                       request.source().sort(cesUserField.getFieldName() + ".keyword", SortOrder.ASC);
                   } else {
                       request.source().sort(cesUserField.getFieldName()+ ".keyword", SortOrder.DESC);
                   }

               }

            });
            // 3.聚合参数
            /*request.source().aggregation(
                    AggregationBuilders.terms("brand_agg").field("depart_name_code").field("fonds_no_code")
                            .field("filing_year").field("retention_code").field("archive_ctg_no_code").size(5)
            );*/
            request.source().aggregation(AggregationBuilders.terms("dept").field("depart_name_code").order(BucketOrder.aggregation("_count", false)));//SortOrder.DESC
            request.source().aggregation(AggregationBuilders.terms("fonds").field("fonds_no_code").order(BucketOrder.aggregation("_count", false)));
            request.source().aggregation(AggregationBuilders.terms("year").field("filing_year").order(BucketOrder.aggregation("_count", false)));
            request.source().aggregation(AggregationBuilders.terms("retention").field("retention_code").order(BucketOrder.aggregation("_count", false)));
            request.source().aggregation(AggregationBuilders.terms("archive").field("archive_ctg_no_code").order(BucketOrder.aggregation("_count", false)));
            //分页
            request.source().size(pageSize);
            int pages = (Integer.parseInt(pageNo + "") - 1) * pageSize;
            request.source().from(pages);
            log.error("查询语法:"+request.source().toString());
            //查询
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            //解析结果
            map = handleResponse(response,pages,pageSize);
        } catch (Exception e) {
            log.error("全文检索失败", e);
            return null;
        }
        return map;
    }

    @Override
    public void updateIndexFlags(String ids, String entityId) {
        try {
            //获取门类数据
            CesEntityType entityType = cesEntityTypeService.getOne(new QueryWrapper<CesEntityType>().eq("entity_id", entityId));//获取实体信息
            //获取表名称
            String tableName = entityType.getTableName();

            String columnName = "id";
            QueryWrapper<CesIndexTableColumn> query = new QueryWrapper<>();
            query.eq("entity_id", entityId);
            List<CesIndexTableColumn> listIndexTableColumn = cesIndexTableColumnService.list(query);
            int columnLen = listIndexTableColumn.size();
            for (int j = 0; j < columnLen; j++) {
                CesIndexTableColumn cesIndexTableColumn = listIndexTableColumn.get(j);
                columnName += "," + cesIndexTableColumn.getColumnName();
            }
            String[] columnNames = columnName.split(",");
            int leng = columnNames.length;
            ArrayList idList = null;
            try {
                idList = StrUtil.stringToArray(ids);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //查询需要创建索引得数据
            List<Map> listArchiveData = cesIndexInfoMapper.selectIndexFlagBatch(tableName,idList);
            int length = listArchiveData.size();

            List<Map<String, Object>> list = new ArrayList<>();

            for (int k = 0; k < length; k++) {
                Map<String, Object> json = new HashMap<>();
                Map map = listArchiveData.get(k);
                String fileContentCN = "";
//            String fileContentYw = readRecordFiles(map.get("id")==null?"":map.get("id").toString(), indexTable, "");
                String fileContentYw = "";//原文暂未处理
                json.put("entityId", entityId);
                json.put("dataType", entityType.getEntityType());//门类类型
                json.put("indexTable", tableName);//表名
                json.put("fileContentYw", clearSpecialStr(fileContentYw));//原文
                for (int m = 0; m < leng; m++) {
                    json.put(columnNames[m], map.get(columnNames[m]) == null ? "" : map.get(columnNames[m]));
                    if (columnNames[m].toString().equals("id")) {
                        continue;
                    }
                    if (map.get(columnNames[m]) != null) {
                        String code = map.get(columnNames[m]) == null ? "" : map.get(columnNames[m]).toString();
                        //保存数据
                        json.put(columnNames[m], code);
                        if (StrUtil.isNotNull(fileContentCN)) {
                            String value = "," + code;
                            fileContentCN += value;
                        } else {
                            fileContentCN = code;
                        }
                    }
                }
                String id = tableName + "@" + map.get("id");
                json.put("id", id);//id
                json.put("fileContentCN", fileContentCN);//条目
                String archive_no_code = "";
                if (map.get("archive_no") != null) {
                    archive_no_code = map.get("archive_no").toString();
                } else if (map.get("folder_no") != null) {
                    archive_no_code = map.get("folder_no").toString();
                }
                json.put("archive_no_code", archive_no_code);//档号
                list.add(json);
            }
            //调用添加索引
            //判断索引是否存在
            boolean b = indexExists(entityId);
            if (!b){
                //创建索引
                indexCreate(entityId);
            }
            if (!list.isEmpty()){
                addBatch(list, entityId);
                //修改索引状态
                cesIndexInfoService.updateDataIndexFlagBatch(tableName, idList);
            }
        } catch (Exception e){
            log.error("全文检索创建索引失败", e);
        }
    }


    private static final String[] TO_EMPTY_SPECIAL_STR_ARRAY = {"\r\n", "\n"};
    private static final String[] TO_NOT_EMPTY_SPECIAL_STR_ARRAY = {"{", "}"};
    /**
     * 替换换行和{}
     *
     * @param sourceStr
     * @return
     */
    public static String clearSpecialStr(String sourceStr) {
        if (StrUtil.isBlank(sourceStr)) return sourceStr;
        for (String specialStr : TO_EMPTY_SPECIAL_STR_ARRAY) {
            sourceStr = sourceStr.replace(specialStr, "");
        }
        for (String notEmptySpecialStr : TO_NOT_EMPTY_SPECIAL_STR_ARRAY) {
            sourceStr = sourceStr.replace(notEmptySpecialStr, "\\" + notEmptySpecialStr);
        }
        return sourceStr;
    }


    private Page<Map<String, Object>> handleResponse(SearchResponse response, int pageNo, int pageSize) {

        Page<Map<String, Object>> page = new Page<>(pageNo, pageSize);
        List<Map<String, Object>> records = new ArrayList<>();
        long total = 0;
        try {
            SearchHits searchHits = response.getHits();
            // 4.1 查询总条数
            total = searchHits.getTotalHits().value;
            log.error("共搜索到" + total + "数据。");
            // 4.2 查询结果数组
            SearchHit[] hits = searchHits.getHits();
            // 遍历结果数组
            for (SearchHit hit : hits) {
                // 4.3 读取文档source
                String json = hit.getSourceAsString();
                //结果
                // 反序列化
                Map<String, Object> res = JSON.parseObject(json, Map.class);
                // 处理高亮
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                if (!CollectionUtils.isEmpty(new Map[]{highlightFields})) {
                    // 获取高亮字段结果
                    HighlightField highlightField = highlightFields.get("fileContentCN");
                    if (highlightField != null) {
                        // 取出高亮结果数组中的第一个,就是酒店名称
                        String name = highlightField.getFragments()[0].string();
//                        System.out.println("高亮:" + name);
                    }
                }
                records.add(res);
            }
        } catch (Exception e) {
            log.error("全文检索数据分析异常", e);
        }
        //设置数据
        page.setRecords(records);
        //设置分页
        page.setTotal(total);
        if (pageSize < total) {
            page.setSize(total);
        }
        return page;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值