eg1:基本查询 "nickname" 中带有 "学习" 的(分页查询)
GET xhs_person_full/_search
{
"query": {
"match": {
"nickname": "学习"
}
},
"_source": ["nickname", "fans", "user_id"],
"from": 0,
"size": 3
}
eg2:按照粉丝数降序排列
GET xhs_person_full/_search
{
"query": {
"match": {
"nickname": "学习"
}
},
"sort": [
{
"fans": {
"order": "desc"
}
}
]
}
eg3:bool查询,多条件查询 must(and),should(or),must_not(not)
GET xhs_person_full/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"nickname": "学习"
}
},
{
"match": {
"fans": "185"
}
}
]
}
}
}
eg4:过滤查询 filter,范围查询 range
GET xhs_person_full/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"nickname": "学习"
}
}
],
"filter": {
"range": {
"fans": {
"gte": 100,
"lte": 200
}
}
}
}
}
}
eg5:term 和 match 区别
term 精确查询 直接通过倒排索引进行精确查找(效率更高)
match 通过分词器解析
GET xhs_person_full/_search
{
"query": {
"term": {
"nickname": "男"
}
}
}
eg6 两种字段(field)类型(type) text 和 keyword 区别
text 允许被分词
keyword 不能被分词
eg7 多个值匹配的精确查询
GET xhs_person_full/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"nickname": "男"
}
},
{
"term": {
"fans": "0"
}
}
]
}
}
}
eg8 高亮查询
1、集成SpringBoot
1)es配置文件中:配置es客户端
@Bean("client")
public RestHighLevelClient restClient1() {
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(initRestClient());
this.restHighLevelClient = restHighLevelClient;
return restHighLevelClient;
}
2)在业务类中注入 RestHighLevelClient 对象
3)索引操作
a、创建索引
// 1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest("kuang_index");
// 2、客户端执行请求,请求后获得响应 createIndexResponse,里面有很多信息
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request,RequestOptions.DEFAULT);
b、获取索引
GetIndexRequest request = new GetIndexRequest("kuang_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
c、删除索引
GetIndexRequest request = new GetIndexRequest("kuang_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
4)文档操作(将信息和实体类相对应)
a、添加文档
// 创建请求
IndexRequest request = new IndexRequest("kuang_index");
// 规则 put /kuang_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1)); // 超时时间 1s
// 将数据放入请求
request.source(JSON.toJSONString(entity), XContentType.JSON);
// 客户端发送请求,获取相应响应信息
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
b、判断文档是否存在
GetRequest getRequest = new GetRequest("kuang_index", "1"); // 1 是 id
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
c、获取文档信息 ( GetRequest )
GetRequest getRequest = new GetRequest("kuang_index", "1"); // 1 是 id
// 所有需要获取的信息都从 getResponse 中获取
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
// eg:字符串形式打印文档内容
String source = getResponse.getSourceAsString();
d、更新文档信息 ( UpdateRequest )
e、删除文档记录 ( DeleteRequest )
f、批量插入数据 ( BulkRequest )
代码示例:
// 1、获取 client 对象
RestHighLevelClient client = param.getEsClient();
// 2、实例化 searchRequest 对象,同时注明操作的索引名称
SearchRequest searchRequest = new SearchRequest("index_name");
// 3、实例化 sourceBuilder 对象,相当于最外层
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 4、定义起始查询位置和分页大小
sourceBuilder.from(0).size(10000);
// 5、实例化 boolQueryBuilder 对象,相当于次外的 query 层
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 6、范围查询(注意:range 可以在 query 层中 bool 层中的 must/must_not/should 下作为对象封装,也可以在 query 层中 bool 层中的 filter 层下作为一个键值对,放在 filter 下就相当于在 must 中指明 range)
boolQueryBuilder.must(QueryBuilders.rangeQuery("create_time").from(quarterAgo).to(day));
boolQueryBuilder.must(QueryBuilders.rangeQuery("goods_num").gt(0));
// 7、多条件中的精确查询
boolQueryBuilder.must(QueryBuilders.termQuery("user_id",id));
// 8、指明需要返回的字段(相当于 sql 中的 select 后面的字段)
sourceBuilder.fetchSource(new String[]{"sales","sales_volume","create_time"},null);
// 9、对查询条件进行封装
sourceBuilder.query(boolQueryBuilder);
searchRequest.source(sourceBuilder);
// 10、实例化 searchResponse 对象,发送查询请求,并接收返回值
SearchResponse searchResponse = new SearchResponse();
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
11、该段代码对应命令
最外层相当于 sourceBuilder
query 层相当于 boolQueryBuilder,要让该 bool 生效需要 sourceBuilder.query(boolQueryBuilder)
其他类似
返回信息在
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
中