【SpringBoot搜索】4.ElasticSearch高级查询与SpringBoot整合

前言

ES中两种查询方式,第一种是简易查询,第二种是DSL(JSON结构化查询)。

简易查询

根据id获取查询语句

# GET /索引名/类型名/id
GET /terry/goods/1

查询结果

{
  "_index" : "terry",
  "_type" : "goods",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "iphone",
    "money" : 9000
  }
}

查询所有

# GET /索引名/类型名/_search
GET /terry/goods/_search

查询结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "terry",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaomi",
          "money" : 3000
        }
      },
      {
        "_index" : "terry",
        "_type" : "goods",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "iphone",
          "money" : 9000
        }
      }
    ]
  }
}

批量查询id

GET /terry/goods/_mget
{
  "ids":["1","2"]
}

查询结果

{
  "docs" : [
    {
      "_index" : "terry",
      "_type" : "goods",
      "_id" : "1",
      "_version" : 2,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "iphone",
        "money" : 9000
      }
    },
    {
      "_index" : "terry",
      "_type" : "goods",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 4,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "xiaomi",
        "money" : 3000
      }
    }
  ]
}

复杂查询

查询名称是小米的

GET /terry/goods/_search?q=name:xiaomi

查询金额在3000~4000之间

GET /terry/goods/_search?q=money[3000 TO 4000]

查询金额在3000~4000之间、按金额排序、并且分页

GET /terry/goods/_search?q=money[3000 TO 4000]&sort=money:desc&from=0&size=1

DSL查询

DSL:传递JSON的结构化查询,相对简易更加灵活。

term

term是分词精准查询,如下根据名字匹配。

GET /terry/goods/_search
{
  "query": {
    "term": {
      "name": "xiaomi"
    } 
  }
}
match

match是分词模糊匹配,如下根据名字模糊匹配。

GET /terry/goods/_search
{
  "query": {
    "match": {
      "name": "xiaomi"
    } 
  }
}
filter

filter是根据条件过滤,按条件过滤 金额 2000 ~ 5000。

GET /terry/goods/_search
{
  "query": {
    "bool": {
      "filter": {
      	"range": {
          "money": {
            "gt": 2000,
            "lte": 5000
          }
        }
      }
    }
  }
}

SpringBoot ES查询构造器

如下是SpringBoot整合ES term查询:

对应的restful是:

GET /terry/goods/_search
{
  "query": {
    "match": {
      "name": "xiaomi"
    } 
  }
}

Java代码如下

import com.terry.App;
import com.terry.dao.GoodsDao;
import com.terry.entry.Goods;
import lombok.extern.java.Log;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Optional;

@SpringBootTest(classes = App.class)
@Log
public class TestEs {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    public void queryBuild() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termQuery("name", "xiaomi"));
        //构建查询请求对象,入参为索引
        SearchRequest searchRequest = new SearchRequest("terry");
        //向搜索请求对象中配置搜索源
        searchRequest.source(searchSourceBuilder);
        // 执行搜索,向ES发起http请求
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}

打印结果:

{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.6931472,"hits":[{"_index":"terry","_type":"goods","_id":"2","_score":0.6931472,"_source":{"name":"xiaomi","money":3000}}]}}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

terrybg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值