包含当前es所有的查询,
需要什么代码直接照搬,改个参数就行!
用的好请务必给我点赞!!!感谢爱你们!!!
(周末更 筒)
为啥写这篇文章呢:
大概是因为目前公司用的api跟以前的不太一样,
以前我们是基于高标准客户端直接做的,
但是目前这边同事是基于ElasticsearchRestTemplate跟NativeSearchQuery做的。
import lombok.extern.slf4j.Slf4j;import org.elasticsearch.common.lucene.search.function.CombineFunction;import org.elasticsearch.index.query.BoolQueryBuilder;import org.elasticsearch.index.query.MatchAllQueryBuilder;import org.elasticsearch.index.query.MatchQueryBuilder;import org.elasticsearch.index.query.MultiMatchQueryBuilder;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.index.query.RangeQueryBuilder;import org.elasticsearch.index.query.TermsQueryBuilder;import org.elasticsearch.index.query.WildcardQueryBuilder;import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;import org.elasticsearch.search.builder.SearchSourceBuilder;import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;import org.elasticsearch.search.sort.SortBuilder;import org.elasticsearch.search.sort.SortBuilders;import org.elasticsearch.search.sort.SortOrder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;import org.springframework.data.elasticsearch.core.SearchHit;import org.springframework.data.elasticsearch.core.SearchHits;import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@Slf4j@RestController@RequestMapping("/v/k/t/query")public class EsTestController {/** * 测试es各种查询方法* */@AutowiredElasticsearchRestTemplate elasticsearchRestTemplate;//通配符查询@PostMapping("/es/wildcard")public void wildcard() {//*:匹配任意数量的字符(包括零个字符)。 *x*: 对x做前后N位做模糊查询,前后有多少值都可以查到(也可以左模糊或者右模糊)//?:用于匹配单个字符.user?a: user?a 匹配user1a,但不匹配user123a//^:必须以某个字符开头,如^user*:必须以user开头//$:必须以某个字符结尾,如user$:必须以user结尾BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//wildcardQuery:通配符查询WildcardQueryBuilder bsVersion = QueryBuilders.wildcardQuery("bsVersion", "*1*");WildcardQueryBuilder bsVersion2 = QueryBuilders.wildcardQuery("type", "*j*");boolQueryBuilder.must(bsVersion).should(bsVersion2);NativeSearchQuery dsl = new NativeSearchQueryBuilder()}//高亮//默认情况下:只能对查询字段高亮!!!//加了.requireFieldMatch(false) 可以对非查询字段高亮,具体看下文@PostMapping("/es/highlight")public void highlight() {BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//wildcardQuery:通配符查询WildcardQueryBuilder bsVersion = QueryBuilders.wildcardQuery("bsVersion", "*1*");WildcardQueryBuilder bsVersion2 = QueryBuilders.wildcardQuery("type", "*j*");boolQueryBuilder.must(bsVersion).should(bsVersion2);NativeSearchQuery dsl = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withHighlightFields(//.requireFieldMatch(false) : 加了可以对非查询字段高亮new HighlightBuilder.Field("bsVersion").preTags("").postTags("").requireFieldMatch(false),new HighlightBuilder.Field("provinceNameCn").preTags("").postTags("").requireFieldMatch(false),new HighlightBuilder.Field("isp").preTags("").postTags("").requireFieldMatch(false),new HighlightBuilder.Field("type").preTags("").postTags("").requireFieldMatch(false),new HighlightBuilder.Field("agentId").preTags("").postTags("").requireFieldMatch(false)).build();SearchHits
————————————————————————————————————