es语法 rest api 模拟query 根据中文姓名搜索demo
es语法 rest api 模拟query 根据中文姓名搜索demo order_info_es/_doc/40094182abc GET order_info_es/_settings?pretty GET { "order_info_es": { "settings": { "index": { "mapping": { "total_fields": { "limit": "2000" } }, "number_of_shards": "5", "provided_name": "order_info_es", "creation_date": "1614763664247", "number_of_replicas": "1", "uuid": "2AmFKQ4SQLOfRVM1H5M7hw", "version": { "created": "6081399" } } } } } order_info_es/_mappings?pretty GET "orderInfoDTO": { "properties": { "operatorName": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, order_info_es/_doc/40094182abc GET "orderInfoDTO": { "orderNo": "40094182abc", "statusDesc": "分配操作", "createTime": "2023-03-21T17:05:37", "isDelete": 0, "operatorName": "小明同学", "updateTime": "2023-03-28T11:05:39", "id": 2, "version": 1, "status": 1, "createOp": "cbcb0e41df29594b03d53f94eae0db3b" order_info_es/_search POST { "query": { "term": { "order.orderNo": { "value": "40094182abc", "boost": 1 } } } } ##带多个条件的情况查询: order_info_es/_search POST { "query": { "bool": { "must": [ { "term": { "orderInfoDTO.orderNo": { "value": "40094182abc", "boost": 1 } } }, { "term": { "orderInfoDTO.createOp": { "value": "cbcb0e41df29594b03d53f94eae0db3b", "boost": 1 } } } ] } } } ##根据订单号搜索 order_info_es/_search POST { "query": { "term": { "order.orderNo": { "value": "40094182abc", "boost": 1 } } } } ##精确匹配:terms order_info_es/_search POST { "query": { "terms": { "orderInfoDTO.createOp": [ "cbcb0e41df29594b03d53f94eae0db3b", "298af1d335ecf13e7492395c8203fe79" ], "boost": 1 } } } #完整 order_info_es/_search POST { "from": 0, "size": 20, "query": { "bool": { "must": [ { "term": { "isNew": { "value": "1", "boost": 1 } } }, { "term": { "category": { "value": "2", "boost": 1 } } }, { "term": { "status": { "value": "8", "boost": 1 } } }, { "term": { "order.orderNo": { "value": "40094182abc", "boost": 1 } } }, { "terms": { "orderInfoDTO.operatorName": [ "小明同学" ], "boost": 1 } } ], "adjust_pure_negative": true, "boost": 1 } }, "sort": [ { "order.reqTime": { "order": "desc" } } ] } ##可以搜索出来结果: order_info_es/_search POST { "query": { "match_phrase": { "orderInfoDTO.operatorName": { "query": "小明同学", "slop": 0, "zero_terms_query": "NONE", "boost": 1 } } } } order_info_es/_search POST { "query": { "match_phrase": { "orderInfoDTO.operatorName": { "query": "小明同学", "slop": 0, "zero_terms_query": "NONE", "boost": 1 } } } } ##搜索关键字: order_info_es/_search POST { "from": 0, "size": 20, "query": { "bool": { "must": [ { "term": { "isNew": { "value": "1", "boost": 1 } } }, { "term": { "category": { "value": "2", "boost": 1 } } }, { "term": { "status": { "value": "8", "boost": 1 } } }, { "match_phrase": { "orderInfoDTO.operatorName": { "query": "李小龙", "slop": 0, "zero_terms_query": "NONE", "boost": 1 } } } ], "adjust_pure_negative": true, "boost": 1 } }, "sort": [ { "order.reqTime": { "order": "desc" } } ] } JAVA代码: boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("orderInfoDTO.operatorName",reqVO.getOrderOperatorNameList().get(0))); order_info_es/_search POST { "query": { "match_phrase": { "orderInfoDTO.operatorName": "李小龙" } } } JAVA代码: boolQueryBuilder.must(QueryBuilders.matchQuery("orderInfoDTO.operatorName",reqVO.getOrderOperatorNameList().get(0))); order_info_es/_search POST { "query": { "match": { "orderInfoDTO.operatorName": "李小龙" } } } order_info_es/_search POST { "query": { "match": { "orderInfoDTO.operatorName": { "query": "李小龙", "operator": "OR", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "lenient": false, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "boost": 1 } } } } ##可以搜索结果: order_info_es/_search POST { "query": { "match": { "orderInfoDTO.operatorName": { "query": "李小龙", "operator": "OR", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "lenient": false, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "boost": 1 } } } } ##无法搜索出来结果:multi_match order_info_es/_search POST { "query": { "multi_match": { "query": "orderInfoDTO.operatorName", "fields": [ "李小龙,小明同学^1.0" ], "type": "best_fields", "operator": "OR", "slop": 0, "prefix_length": 0, "max_expansions": 50, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "fuzzy_transpositions": true, "boost": 1 } } } ##无法查询出来结果:term order_info_es/_search POST { "query": { "term": { "orderInfoDTO.operatorName": "李小龙" } } } JAVA代码: //多个条件 if(reqVO.getOrderOperatorNameList()!=null && reqVO.getOrderOperatorNameList().size()>0) { BoolQueryBuilder subQueryBuilder = new BoolQueryBuilder(); for (int i=0; i<reqVO.getOrderOperatorNameList().size(); i++) { QueryBuilder queryBuilder = QueryBuilders.matchQuery("orderInfoDTO.operatorName", reqVO.getOrderOperatorNameList().get(i)); subQueryBuilder.should(queryBuilder); } boolQueryBuilder.must(subQueryBuilder); } ##通过或的关系查询: order_info_es/_search POST { "query": { "bool": { "should": [ { "match": { "orderInfoDTO.operatorName": { "query": "李小龙", "operator": "OR", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "lenient": false, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "boost": 1 } } }, { "match": { "orderInfoDTO.operatorName": { "query": "小明同学", "operator": "OR", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "lenient": false, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "boost": 1 } } } ] } } } ##带多个条件的情况: order_info_es/_search POST { "query": { "bool": { "must": [ { "term": { "isNew": { "value": "1", "boost": 1 } } }, { "bool": { "should": [ { "match": { "orderInfoDTO.operatorName": { "query": "李小龙", "operator": "OR", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "lenient": false, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "boost": 1 } } }, { "match": { "orderInfoDTO.operatorName": { "query": "小明同学", "operator": "OR", "prefix_length": 0, "max_expansions": 50, "fuzzy_transpositions": true, "lenient": false, "zero_terms_query": "NONE", "auto_generate_synonyms_phrase_query": true, "boost": 1 } } } ] } } ] } } }