【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}}]}}

你可能感兴趣的:(SpringBoot,elasticsearch,spring,boot,搜索引擎)