ElasticSearch - SpringBoot整合ES之查询所有 match_all
文章目录
- 1. 数据准备
- 2. 全量查询 match_all
- 3. 使用 boost 参数更改 _score
官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/structured-search.html
1. 数据准备
官方测试数据下载地址:https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip ,数据量很大,我们自己构造数据吧。
PUT /user/_doc/1
{"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests": [ "sports"]
}PUT /user/_doc/2
{"first_name" : "zhangsan","last_name" : "Smith","age" : 19,"about" : "我是一个安静的人","interests": [ "read" ]
}PUT /user/_doc/3
{"first_name" : "lisi","last_name" : "Alice","age" : 29,"about" : "我喜欢规划自己的生活","interests": [ "sports", "read","music" ]
}
2. 全量查询 match_all
最简单的查询,它匹配所有文档,默认给它们一个_score 1.0。
GET /user/_search
{"query": {"match_all": {}}
}
为此对应的java实现如下:
@Slf4j
@Service
public class ElasticSearchImpl {private static final String KNOWLEDGE_INDEX = "knowledge";@Autowiredprivate RestHighLevelClient restHighLevelClient;public void searchUser() throws IOException {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();searchSourceBuilder.query(matchAllQueryBuilder);SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(searchResponse);}
}
{"took" : 21,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "user","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports"]}},{"_index" : "user","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"first_name" : "zhangsan","last_name" : "Smith","age" : 19,"about" : "我是一个安静的人","interests" : ["read"]}},{"_index" : "user","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"first_name" : "lisi","last_name" : "Alice","age" : 29,"about" : "我喜欢规划自己的生活","interests" : ["sports","read","music"]}}]}
}
3. 使用 boost 参数更改 _score
默认情况下,Elasticsearch 按相关性分数对匹配的搜索结果进行排序,相关性分数衡量每个文档与查询的匹配程度。相关性分数是一个正浮点数,在 搜索_score
API的元字段中返回。越高 ,文档越相关。
GET /user/_search
{"query": {"match_all": { "boost" : 1.2 }}
}
为此对应的java实现如下:
@Slf4j
@Service
public class ElasticSearchImpl {@Autowiredprivate RestHighLevelClient restHighLevelClient;public void searchUser() throws IOException {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();matchAllQueryBuilder.boost(1.2f);searchSourceBuilder.query(matchAllQueryBuilder);SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(searchResponse);}
}
查询结果:
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.2,"hits" : [{"_index" : "user","_type" : "_doc","_id" : "1","_score" : 1.2,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports"]}},{"_index" : "user","_type" : "_doc","_id" : "2","_score" : 1.2,"_source" : {"first_name" : "zhangsan","last_name" : "Smith","age" : 19,"about" : "我是一个安静的人","interests" : ["read"]}},{"_index" : "user","_type" : "_doc","_id" : "3","_score" : 1.2,"_source" : {"first_name" : "lisi","last_name" : "Alice","age" : 29,"about" : "我喜欢规划自己的生活","interests" : ["sports","read","music"]}}]}
}