精确匹配查询

精确查询demo

首先使用 TermQueryBuilder 进行字段匹配

多个条件匹配 BoolQueryBuilder must匹配

package com.kgo.elasticsearch.client.resthighlevelclient.search;

import com.kgo.elasticsearch.client.resthighlevelclient.ElasticSearchBuilder;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * @author lizhenming
 * @desc
 */
@Slf4j
public class TermSearchDemo {
    public static void main(String[] args) {
        ElasticSearchBuilder elasticSearchBuilder = new ElasticSearchBuilder();
        RestHighLevelClient highClient = elasticSearchBuilder.getHighLevelClient();
        try {

            // 查询资源构造器
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            // bool类型查询构造器
            BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
            // 匹配第一个
            TermQueryBuilder termTerminalBuilder1 = QueryBuilders.termQuery("appName", "guodun-contract");
            // 匹配第二个
            TermQueryBuilder termTerminalBuilder2 = QueryBuilders.termQuery("traceId", "U5l5Vfpn");

            boolBuilder.must(termTerminalBuilder1);

            boolBuilder.must(termTerminalBuilder2);

            // 应用bool构造器
            sourceBuilder.query(boolBuilder);

            // 搜索请求 (构造参数 索引名)
            SearchRequest searchRequest = new SearchRequest("plume_log_run_*");

            searchRequest.source(sourceBuilder);

            log.info("【请求参数】  {} ", sourceBuilder.toString());

            SearchResponse searchResponse = highClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits searchHits = searchResponse.getHits();
            long total = searchHits.getTotalHits().value;
            SearchHit[] searchHitsArray = searchHits.getHits();
            log.info("【请求总数量】 {} ", total);
            for (int i = 0; i < searchHitsArray.length; i++) {
                log.info("【请求数据-{}】 {} ", i + 1, searchHitsArray[i].getSourceAsString());
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                highClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

请求结果

14:40:41.278 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求参数】  {"query":{"bool":{"must":[{"term":{"appName":{"value":"guodun-contract","boost":1.0}}},{"term":{"traceId":{"value":"U5l5Vfpn","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}} 
14:40:41.436 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求总数量】 10 
14:40:41.440 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-1】 {"traceId":"U5l5Vfpn","dtTime":1616825331830,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.service.function.JoinUserFunction","content":"【筛选当前节点和当前参与方-1】 参与方数量 1 "} 
14:40:41.440 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-2】 {"traceId":"U5l5Vfpn","dtTime":1616825331831,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.strategy.contract.query.QuerySignDetailByContractIdStrategy","content":"【获取登录人签署信息-5】 当前参与方  : {\"companyId\":\"20210204144428322011962\",\"contractId\":\"20210326163647479475312_C\",\"contractJoinUserId\":\"20210326163647489475316_JU\",\"contractStatus\":\"wait_for_me\",\"createDate\":\"2021-03-26 16:36:34\",\"id\":\"20210326163647489475316_JU\",\"joinFunction\":\"seal-and-sign\",\"nodeId\":\"20210326163647489475315_NODE\",\"phone\":\"13920260581\",\"signRole\":\"approver\",\"signStatus\":\"0\",\"updateDate\":\"2021-03-26 16:36:34\",\"userId\":\"20201208110142952121862\",\"userName\":\"王冉\"} "} 
14:40:41.440 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-3】 {"traceId":"U5l5Vfpn","dtTime":1616825331689,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.controller.ContractSignDetailsController","content":"【请求入口】查询合同签署详情 【 用户:13920260581,ID: 20201208110142952121862,终端:[android] 身份:[企业] , 公司ID 20210204144428322011962 】 [入参 : String : \"20210326163647479475312_C\" "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-4】 {"traceId":"U5l5Vfpn","dtTime":1616825331830,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.strategy.contract.query.QuerySignDetailByContractIdStrategy","content":"【获取登录人签署信息-4】 当前参与方列表  1  个: [{\"companyId\":\"20210204144428322011962\",\"contractId\":\"20210326163647479475312_C\",\"contractJoinUserId\":\"20210326163647489475316_JU\",\"contractStatus\":\"wait_for_me\",\"createDate\":\"2021-03-26 16:36:34\",\"id\":\"20210326163647489475316_JU\",\"joinFunction\":\"seal-and-sign\",\"nodeId\":\"20210326163647489475315_NODE\",\"phone\":\"13920260581\",\"signRole\":\"approver\",\"signStatus\":\"0\",\"updateDate\":\"2021-03-26 16:36:34\",\"userId\":\"20201208110142952121862\",\"userName\":\"王冉\"}] "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-5】 {"traceId":"U5l5Vfpn","dtTime":1616825332204,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.strategy.contract.query.QuerySignDetailByContractIdStrategy","content":"【获取登录人签署信息-6】 当前参与方签署信息  : [{\"contractDetailId\":\"20210326163647496475317_DETAIL\",\"contractId\":\"20210326163647479475312_C\",\"id\":\"20210326163647496475317_DETAIL\",\"ifApply\":false,\"isLock\":1,\"nodeId\":\"20210326163647489475315_NODE\",\"pageList\":{\"13664ba1a4e2408bbfc01678789bbab9\":[0]},\"roleType\":\"approver\",\"rotationAngle\":0.0,\"signHeight\":110,\"signLeft\":73,\"signTop\":64,\"signType\":\"seal\",\"signWidth\":110},{\"contractDetailId\":\"20210326163647496475318_DETAIL\",\"contractId\":\"20210326163647479475312_C\",\"id\":\"20210326163647496475318_DETAIL\",\"ifApply\":false,\"isLock\":1,\"nodeId\":\"20210326163647489475315_NODE\",\"pageList\":{\"13664ba1a4e2408bbfc01678789bbab9\":[0,1,2,3,4,5]},\"roleType\":\"approver\",\"rotationAngle\":0.0,\"signHeight\":154,\"signLeft\":502,\"signTop\":566,\"signType\":\"perforationSeal\",\"signWidth\":88}] "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-6】 {"traceId":"U5l5Vfpn","dtTime":1616825331690,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.strategy.contract.query.QuerySignDetailByContractIdStrategy","content":"【获取登录人签署信息-1】 入参 合同ID 20210326163647479475312_C "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-7】 {"traceId":"U5l5Vfpn","dtTime":1616825331826,"logLevel":"DEBUG","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.strategy.contract.query.QuerySignDetailByContractIdStrategy","content":"【获取登录人签署信息-2】获取合同节点 [{\"agentSign\":false,\"completeTime\":\"2021-03-26 16:36:47\",\"contractId\":\"20210326163647479475312_C\",\"contractStatus\":\"wait_for_other\",\"createTime\":\"2021-03-26 16:36:47\",\"departmentId\":\"20210204144428327011963\",\"id\":\"20210326163647488475313_NODE\",\"legalPersonSign\":false,\"nodeIndex\":1,\"nodeName\":\"发起节点\",\"nodeSort\":\"internal\",\"nodeStatus\":\"completed\",\"nodeType\":\"issue\",\"subjectId\":\"20210204144428322011962\",\"subjectType\":\"company\",\"updateTime\":\"2021-03-26 16:36:47\"},{\"agentSign\":false,\"contractId\":\"20210326163647479475312_C\",\"contractStatus\":\"wait_for_me\",\"createTime\":\"2021-03-26 16:36:47\",\"id\":\"20210326163647489475315_NODE\",\"legalPersonSign\":false,\"nodeIndex\":2,\"nodeName\":\"审批节点\",\"nodeSort\":\"internal\",\"nodeStatus\":\"processing\",\"nodeType\":\"seal-and-sign\",\"subjectId\":\"20210204144428322011962\",\"subjectType\":\"company\",\"updateTime\":\"2021-03-26 16:36:47\"}] "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-8】 {"traceId":"U5l5Vfpn","dtTime":1616825331827,"logLevel":"DEBUG","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.strategy.contract.query.QuerySignDetailByContractIdStrategy","content":"【获取登录人签署信息-3】 根据合同ID【 20210326163647479475312_C 】 用户ID 【20201208110142952121862 】获取当前签署方 "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-9】 {"traceId":"U5l5Vfpn","dtTime":1616825331831,"logLevel":"INFO","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.service.function.JoinUserFunction","content":"【筛选当前节点和当前参与方-2】企业类型 internal_2 节点类型【company】| 登录人 2 "} 
14:40:41.441 [main] INFO com.kgo.elasticsearch.client.resthighlevelclient.search.TermSearchDemo - 【请求数据-10】 {"traceId":"U5l5Vfpn","dtTime":1616825331689,"logLevel":"DEBUG","method":"http-nio-7021-exec-4","appName":"guodun-contract","serverName":"192.168.1.233","className":"com.guodun.subservice.contract.controller.ContractSignDetailsController","content":"【1】查询合同签署详情 参数 \"20210326163647479475312_C\" "} 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐