es常见操作
Posted 陈如水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es常见操作相关的知识,希望对你有一定的参考价值。
#1)分页,排序,查询统一层级 #2)使用回车键进行智能提示 #3)如何搜索两个汉字 #4)在某个字段中搜索关键字
GET search_fund_open/_search
"from":1,
"size": 20,
"query":
"term":
"search": "华"
,
"sort": [
"sort":
"order": "asc"
,
"_id":
"order": "asc"
]
GET _search
GET search_fund_open/_analyze
"text": [ "华夏"],
"field": "search"
GET search_fund_open/_mapping
/**
* @Author crs
* @Description TestController
* @Date 2022/09/27/14:27
* @Version 1.0
*/
@RestController
public class TestController
@Autowired
private PostRepository postRepository;
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@GetMapping("/get")
public void get()
//单字符串模糊查询,默认排序。将从所有字段中查找包含传来的word分词后字符串的数据集.
//共13个结果,按照默认的排序方式,即匹配相关度排序,前10个匹配度最高,都是完全带“浣溪沙”三个字的。
//第10个、11个都是题目和正文都包含“溪”字而且出现了2次“溪”,最后一个是正文带一个“溪”。
QueryBuilder query = QueryBuilders.queryStringQuery("浣溪沙");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get1")
public void get1()
//定义排序方式,按照weight从大到小排序。按照某个字段的值进行排序,为某个字段写入值。
QueryBuilder query = QueryBuilders.queryStringQuery("浣溪沙");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withSort(SortBuilders.fieldSort("weight").order(SortOrder.DESC))
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get2")
public void get2()
//针对落日溶金进行分词,然后按照默认的匹配相关性进行排序。
//QueryBuilder query = QueryBuilders.matchQuery("content", "落日熔金");
QueryBuilder query = QueryBuilders.matchQuery("userId", "1");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get3")
public void get3()
//只能搜索出来一条
//QueryBuilder query = QueryBuilders.matchPhraseQuery("content", "中华共和国");
//分词后,中间能间隔几个位置的也能查出来,可以使用slop参数。
QueryBuilder query = QueryBuilders.matchPhraseQuery("content", "中华共和国").slop(2);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get4")
public void get4()
// 匹配多列的情况
// 无论是title还是content中,包含“我”“是”字样的都被查询了出来。
QueryBuilder query = QueryBuilders.multiMatchQuery("我是", "content","title");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get5")
public void get5()
//完全包含查询 .operator(MatchQueryBuilder.Operator.AND)
//ES会把分词后所有包含“我”和“天”的都查询出来,如果我们希望必须是包含了两个字的才能被查询出来,那么我们就需要设置一下Operator。
//无论是matchQuery,multiMatchQuery,queryStringQuery等,都可以设置operator。默认为Or,设置为And后,就会把符合包含所有输入的才查出来。
QueryBuilder query = QueryBuilders.matchQuery("content", "我是").operator(MatchQueryBuilder.Operator.AND);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get6")
public void get6()
//单字段包含所有输入(按比例包含)
//如果是and的话,譬如用户输入了5个词,但包含了4个,也是显示不出来的。我们可以通过设置精度来控制。
//minimumShouldMatch可以用在match查询中,设置最少匹配了多少百分比的能查询出来。
QueryBuilder query = QueryBuilders.matchQuery("content", "我是")
.operator(MatchQueryBuilder.Operator.AND)
.minimumShouldMatch("75%");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(query)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
@GetMapping("/get7")
public void get7()
//组合插叙:查询title包含“XXX”,且userId=“1”,且weight最好小于5
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("userId",1));
boolQueryBuilder.must(QueryBuilders.matchQuery("title","xxx"));
boolQueryBuilder.should(QueryBuilders.rangeQuery("weight").lte(5));
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.withPageable(PageRequest.of(0,20))
.build();
List<Post> posts = elasticsearchTemplate.queryForList(searchQuery, Post.class);
System.out.println(JacksonUtil.toJSONString(posts));
以上是关于es常见操作的主要内容,如果未能解决你的问题,请参考以下文章