Java High Level REST Client
基本查询
请求
GET /person/_search
{
"query": {
"term": {
"name.keyword": {
"value": "张无忌",
"boost": 1.0
}
}
}
}
响应
{
"took" : 0,
"timed_out" : false,
"_shards" : { // 分片信息
"total" : 1, // 总计分片数
"successful" : 1, // 查询成功的分片数
"skipped" : 0, // 跳过查询的分片数
"failed" : 0 // 查询失败的分片数
},
"hits" : { // 命中结果
"total" : {
"value" : 1, // 数量
"relation" : "eq" // 关系:等于
},
"max_score" : 2.8526313, // 最高分数
"hits" : [
{
"_index" : "person", // 索引
"_type" : "_doc", // 类型
"_id" : "1",
"_score" : 2.8526313,
"_source" : {
"address" : "光明顶",
"modifyTime" : "2021-06-29 16:48:56",
"createTime" : "2021-05-14 16:50:33",
"sect" : "明教",
"sex" : "男",
"skill" : "九阳神功",
"name" : "张无忌",
"id" : 1,
"power" : 99,
"age" : 18
}
}
]
}
}
Term查询
基本
/**
* term精确查询
*
* @throws IOException
*/
@Autowired
private RestHighLevelClient client;
@Test
public void queryTerm() throws IOException {
// 根据索引创建查询请求
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.termQuery("name.keyword", "张无忌"));
System.out.println("searchSourceBuilder=====================" + searchSourceBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));
}
优化
- 查询结果中会带有_score这一项,ES会根据结果匹配程度进行评分。打分是会耗费性能的,如果确认自己的查询不需要评分,就设置查询语句关闭评分
- 可以通过Constant Score将查询转换为Filtering,避免算分,并利用缓存,提高性能 ```json
GET /person/_search { “query”: { “constant_score”: { “filter”: { “term”: { “sect.keyword”: { “value”: “张无忌”, “boost”: 1.0 } } }, “boost”: 1.0 } } }
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 这样构造的查询条件,将不进行score计算,从而提高查询效率 searchSourceBuilder.query(QueryBuilders .constantScoreQuery(QueryBuilders .termQuery(“sect.keyword”, “明教”)));
<a name="ZTfNr"></a>
### 多值查询
```json
GET /person/_search
{
"query": {
"terms": {
"sect.keyword": [
"明教",
"武当派"
],
"boost": 1.0
}
}
}
java实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders
.termsQuery("sect.keyword",
Arrays.asList("明教", "武当派")));
范围查询
GET /person/_search
{
"query":{
"range":{
"age":{
"from":10,
"to":20,
"include_lower":true,
"include_upper":true,
"boost":1
}
}
}
}
Java实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.rangeQuery("age").gte(10).lte(30));
前缀查询
{
"query": {
"prefix": {
"sect.keyword": {
"value": "武当",
"boost": 1.0
}
}
}
}
Java实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.prefixQuery("sect.keyword","武当"));
通配符查询
{
"query": {
"wildcard": {
"sect.keyword": {
"wildcard": "张*忌",
"boost": 1.0
}
}
}
}
Java实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.wildcardQuery("sect.keyword","张*忌"));
复合查询
- must:所有的语句都必须匹配,与 ‘=’ 等价。
- must_not:所有的语句都不能匹配,与 ‘!=’ 或 not in 等价。
- should:至少有n个语句要匹配,n由参数控制。
{
"query": {
"bool": {
"must": [
{
"term": {
"sex": {
"value": "女",
"boost": 1.0
}
}
},
{
"term": {
"sect.keywords": {
"value": "明教",
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
}
Java实现
```json 示例1: SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 构建查询语句 searchSourceBuilder.query(QueryBuilders.boolQuery()
); 示例2: SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 构建查询语句 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("sex", "女"))
.must(QueryBuilders.termQuery("sect.keyword", "明教"))
.must(QueryBuilders.termQuery("sex", "女"))
.must(QueryBuilders.rangeQuery("age").gte(30).lte(40))
.mustNot(QueryBuilders.termQuery("sect.keyword", "明教"))
.should(QueryBuilders.termQuery("address.keyword", "峨眉山"))
.should(QueryBuilders.rangeQuery("power.keyword").gte(50).lte(80))
.minimumShouldMatch(1); // 设置should至少需要满足几个条件
// 将BoolQueryBuilder构建到SearchSourceBuilder中 searchSourceBuilder.query(boolQueryBuilder);
<a name="SGjop"></a>
### Filter查询
- query和filter的区别:query查询的时候,会先比较查询条件,然后计算分值,最后返回文档结果;而filter是先判断是否满足查询条件,如果不满足会缓存查询结果(记录该文档不满足结果),满足的话,就直接缓存结果,filter不会对结果进行评分,能够提高查询效率。
```json
{
"query":{
"bool":{
"filter":[
{
"term":{
"sex":{
"value":"男",
"boost":1
}
}
}
],
"adjust_pure_negative":true,
"boost":1
}
}
}
Java实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery("sex", "男"))
);
将must、must_not置于filter下,这种方式是最常用的
{
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"term": {
"sect.keyword": {
"value": "明教",
"boost": 1.0
}
}
},
{
"range": {
"age": {
"from": 20,
"to": 35,
"include_lower": true,
"include_upper": true,
"boost": 1.0
}
}
}
],
"must_not": [
{
"term": {
"sex.keyword": {
"value": "女",
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
}
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.filter(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sect.keyword", "明教"))
.must(QueryBuilders.rangeQuery("age").gte(20).lte(35))
.mustNot(QueryBuilders.termQuery("sex.keyword", "女")))
);
聚合查询
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);
// 按sect分组
AggregationBuilder aggBuilder = AggregationBuilders.terms("sect_count").field("sect.keyword");
searchSourceBuilder.aggregation(aggBuilder);